我目前在使用Recyclerview菜单时遇到问题。我将recyclerview作为侧边菜单或迷你吧运行,只是我当前难以接受的点击操作。我知道我必须在itemview和getadapter位置设置一个onclicklistener,但是如何编写它以便实例
编辑:这是我的代码。我究竟做错了什么?我觉得项目位置没有被正确读取。
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;
}
}