0

Im currently having trouble working on a recyclerview menu im working on. I have the recyclerview running as a side menu or minibar, just the click actions im currently getting stumped on. I understand that I have to set an onclicklistener on itemview and getadapter position but how do I write it so for instance

edit: Here is my code. What am i doing wrong? I feel the items position isn't being read correctly.

public class QuickPanel extends Fragment {

private Context context;
private RelativeLayout quick_frame;
private TextView quick_nickName;
private WallpaperColorInfo instance;
private int alpha;

private List<ActionItems> actionList = new ArrayList<>();
private RecyclerView miniBar;
private ActionItemAdapter mAdapter;



public QuickPanel() {
}

public static QuickPanel newInstance(int instance) {
    Bundle args = new Bundle();
    args.putInt(ARGS_INSTANCE, instance);
    QuickPanel fragment = new QuickPanel();
    fragment.setArguments(args);
    return fragment;
}

@SuppressLint("ClickableViewAccessibility")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    context = getActivity();

    instance = WallpaperColorInfo.getInstance(getContext());

    View rootView = inflater.inflate(R.layout.swift_panel, container, false);
    quick_frame = rootView.findViewById(R.id.quick_panel);
    quick_nickName = rootView.findViewById(R.id.panel_user_nm);
    miniBar = (RecyclerView) rootView.findViewById(R.id.miniBar);

    try {
        quick_frame.setBackgroundColor(primaryColor(instance, getContext(), alpha));
        quick_nickName.setTextColor(secondaryColor(instance, getContext(), alpha));
    } catch (Exception e) {
        Log.e("Context Pages Theme", "Couldn't get wallpaper color info");
    }

    mAdapter = new ActionItemAdapter(actionList);
    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getContext());
    miniBar.setLayoutManager(mLayoutManager);
    miniBar.setItemAnimator(new DefaultItemAnimator());
    miniBar.setAdapter(mAdapter);

    prepareActionData();

    return rootView;
}

private void prepareActionData(){

    ActionItems wifi = new ActionItems(getView(),"Wifi", R.drawable.ic_wifi);
    actionList.add(0, wifi);

    ActionItems bluetooth = new ActionItems(getView(),"Bluetooth", R.drawable.ic_bluetooth_connected);
    actionList.add(1, bluetooth);

    ActionItems hotspot = new ActionItems(getView(),"Hotspot", R.drawable.ic_bluetooth_connected);
    actionList.add(2, hotspot);

    mAdapter.notifyDataSetChanged();

}

}

public class ActionItemAdapter extends RecyclerView.Adapter<ActionItemAdapter.MyViewHolder> {

private List<ActionItems> actionList;

public ActionItemAdapter(List<ActionItems> actionList) {
    this.actionList = actionList;
}

public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    public TextView action;
    public ImageView image;
    public CardView actionFrame;

    public MyViewHolder(View view) {
        super(view);
        view.setOnClickListener(this);
        action = view.findViewById(R.id.action_title);
        image = view.findViewById(R.id.action_image);
        actionFrame = view.findViewById(R.id.action_frame);
    }

    @Override
    public void onClick(View v) {

        if(getAdapterPosition() == 0){
            Toast.makeText(v.getContext(), "wifi", Toast.LENGTH_SHORT).show();
        }

    }
}


@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.action_item_list, parent, false);

    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
    final ActionItems actions = actionList.get(position);
    holder.action.setText(actions.getAction());
    holder.image.setImageResource(actions.getImage());
}

@Override
public int getItemCount() {
    return actionList.size();
}

}

public class ActionItems {
private String action;
private int image;

public ActionItems(View v, String action, int image) {
    this.action = action;
    this.image = image;
}

public String getAction() {
    return action;
}

public void setAction(String name) {
    this.action = name;
}

public int getImage() {
    return image;
}

public void setImage(int image) {
    this.image = image;
}

}

Nx Biotic
  • 31
  • 5
  • 1
    call `setOnClickListener(this)` inside your custom `ViewHolder` constructor - your `ViewHolder` class needs to implement `View.OnClickListener` interface – pskink Apr 18 '18 at 10:04
  • if your are using Model class for it, then take a string member in bean class, Assign task to it, and put click on condition that matched with member value. – Farhana Naaz Ansari Apr 18 '18 at 10:05
  • Il update my question with code tonight with my code from my adapter class. I see multiple questions asking this question from different perspectives but not the answers im looking for or did not work for me. Its basically a menu or action mini bar. Each action does a different task. – Nx Biotic Apr 18 '18 at 12:09
  • updated with code. what am i doing wrong? some assistance would be appreciated. Never really used recyclerview before. – Nx Biotic Apr 19 '18 at 22:39
  • @farhana posted – Nx Biotic Apr 20 '18 at 15:25

0 Answers0