当我按下弹出菜单但我没有这样做时,我需要获得当前的recyclerview项目位置,请帮助我。
holder.buttonViewOption.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//creating a popup menu
PopupMenu popup = new PopupMenu(mCtx, holder.buttonViewOption);
//inflating menu from xml resource
popup.inflate(R.menu.options_menu);
//adding click listener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu1:
MyLis item =list.get(onBindViewHolder(getItemViewType(position)));
Intent i = new Intent(mCtx, NewClass.class);
i.putExtra("name",item .getName());
mContext.startActivity(i);
break;
case R.id.menu2:
//handle menu2 click
break;
case R.id.menu3:
//handle menu3 click
break;
}
return false;
}
});
//displaying the popup
popup.show();
}
});
整个代码 CustomAdapter.java :
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
private List<MyList> list;
private Context mCtx;
public CustomAdapter(List<MyList> list, Context mCtx) {
this.list = list;
this.mCtx = mCtx;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_items, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(final CustomAdapter.ViewHolder holder, int position) {
MyList myList = list.get(position);
holder.textViewHead.setText(myList.getHead());
holder.textViewDesc.setText(myList.getDesc());
holder.buttonViewOption.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//creating a popup menu
PopupMenu popup = new PopupMenu(mCtx, holder.buttonViewOption);
//inflating menu from xml resource
popup.inflate(R.menu.options_menu);
//adding click listener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu1:
MyLis item =list.get(onBindViewHolder(getItemViewType(position)));
Intent i = new Intent(mCtx, NewClass.class);
i.putExtra("name",item .getName());
mContext.startActivity(i);
break;
case R.id.menu2:
//handle menu2 click
break;
case R.id.menu3:
//handle menu3 click
break;
}
return false;
}
});
//displaying the popup
popup.show();
}
});
}
答案 0 :(得分:0)
只需替换此
case R.id.menu1:
Intent i = new Intent(mCtx, NewClass.class);
i.putExtra("name", myList.getName());
mContext.startActivity(i);
break;
当您在BindViewHoldee
中对此MyLis item =list.get(onBindViewHolder(getItemViewType(position)));
顶部进行decalre时,您不再需要MyList myList = list.get(position);