使用RecylcerView
,我正在尝试为列表中的每个项目设置一个弹出菜单,类似于:
创建弹出菜单很简单,但是如何获得onMenuItemClicked
中点击的项目的位置?
public class Activity extends AppCompatActivity implements PopupMenu.OnMenuItemClickListener {
public void showPopupMenu(View v) {
PopupMenu popupMenu = new PopupMenu(this, v);
MenuInflater inflater = popupMenu.getMenuInflater();
inflater.inflate(R.menu.edit_delete_menu, popupMenu.getMenu());
popupMenu.show();
}
@Override
public boolean onMenuItemClick(MenuItem item) {
//get position here from RecyclerView here?
switch (item.getItemId()) {
case R.id.edit:
//Do position specific action
break;
case R.id.delete:
//Do position specific action
break;
}
return false;
}
}
答案 0 :(得分:1)
好吧,所以我(令人惊讶地)在这里设法回答了我自己的问题。
为了使用RecylcerView
从onMenuItemClicked
PopupMenu
内的PopupMenu
适配器获取位置,我创建了PopupMenu
的自定义实现。
这样做可以在使用PopupMenu
时提供更大的灵活性,例如displaying icons in your menus。
查看Google的MyPopupMenu
源代码,并创建自己的类似OnClickListener
的内容,但是您可以修改类可以执行的操作的某些实例。
要解决我的问题,我在RecyclerView.Adapter
内的“更多”按钮中添加了MyPopupMenu
。单击时,该按钮调用一个接口方法,该方法既传递按钮视图,又传递适配器的当前位置。
在int position
的自定义实现中,为每个构造函数添加int值的变量需求。同时将onMenuItemClick(MenuItem item, int position)
添加到MyPopupMenu
中的接口方法public class MyActivity extends AppCompatActivity implements MyAdapter.OnItemEventListener, PopupMenu.OnMenuItemClickListener {
@Override
public void onMoreClicked(View v, int position) {
MyPopupMenu popupMenu = new MyPopupMenu(this, v, position);
MenuInflater inflater = popupMenu.getMenuInflater();
inflater.inflate(R.menu.edit_delete_menu, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(this);
popupMenu.show();
}
@Override
public boolean onMenuItemClick(MenuItem item, int position) {
switch (item.getItemId()) {
case R.id.edit:
//Do position specific action with int position
break;
case R.id.delete:
//Do position specific action with int position
break;
}
return false;
}
}
class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private final OnItemEventListener onItemEventListener;
static class ViewHolder extends RecyclerView.ViewHolder {
ImageButton more;
ViewHolder(View v) {
super(v);
more = (ImageButton) v.findViewById(R.id.list_item_more_button);
}
}
public NewGameAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
final ViewHolder viewHolder = new ViewHolder(v);
viewHolder.more.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onItemEventListener.onMoreClicked(viewHolder.more, viewHolder.getAdapterPosition());
}
});
return viewHolder;
}
interface OnItemEventListener {
void onMoreClicked(View v, int position);
}
}
。
最后,在活动类中汇总。
Employee[ ] getAllEmployees()
Employee getEmployee(String id)
Employ (Employee employee)
modify_Employee (Employee employee, String id)
让我知道你们的想法!
答案 1 :(得分:0)
您将需要:
Interface
,其中一种方法click position
为parameter
。
例如:
public interface OnPositionClickListener {
void onPositionClicked(int position);
}
Implement
Interface
activity
并RecyclerView
传递给@Override
public void onPositionClicked(int position) {
//show your popup here and do position based actions here
}
的适配器。所以,你的活动内部应该是这样的方法实现:
click listener
将适配器的click listener
引用传递给视图持有者,并从菜单视图的可点击项public class SampleViewHolder extends RecyclerView.ViewHolder {
public SampleViewHolder(View view, OnPositionClickListener listener) {
//***
//your view bind code here
// ***
clickableView.setOnClickListener( new OnClickListener {
@Override
public void onClick(View view) {
listener.onPositionClicked(getAdapterPosition());
}
});
}
}
调用它的方法。
示例:
position
答案 2 :(得分:0)
首先,在您显示的代码段中,它与可用图片无关。
请提供您的onBindViewHolder代码。这就是应该设置单击侦听器上的菜单项的位置。
答案 3 :(得分:0)
您好,亲爱的您需要在适配器中使用这种方式
@Override
public void onBindViewHolder(@NonNull final RecentPlayedAdapter.MyViewHolder holder, final int position) {
holder.menudotes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupMenu popupMenu=new PopupMenu(context, holder.menudotes, Gravity.START);
popupMenu.getMenuInflater().inflate(R.menu.popupmenu1,popupMenu.getMenu());
popupMenu.show();
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.download:
Toast.makeText(context, "Download", Toast.LENGTH_SHORT).show();
//Do position specific action with int position
break;
case R.id.playlist:
//Do position specific action with int position
Toast.makeText(context, "Playlist", Toast.LENGTH_SHORT).show();
break;
case R.id.share:
//Do position specific action with int position
Toast.makeText(context, "Share", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
});
}
});
}