我有一个自定义recyclerView
的适配器,我想在recyclerView
项目的长按事件上打开一个弹出菜单。
我怎么能这样做?
答案 0 :(得分:33)
已经回答here。无论如何,你可以这样做:
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
private Article article;
private TextView nameTextView;
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
nameTextView = (TextView) itemView.findViewById(R.id.grid_item_article_name_textView);
}
public void bind(Article article) {
this.article = article;
nameTextView.setText(article.getName());
}
@Override
public void onClick(View view) {
// Context context = view.getContext();
// article.getName()
}
@Override
public boolean onLongClick(View view) {
// Handle long click
// Return true to indicate the click was handled
return true;
}
}
答案 1 :(得分:11)
首先,您必须注册您的活动以收听recyclerView
中的longClick事件(因此您不必使用任何类型的onLongClickListener
...):
registerForContextMenu(recyclerView);
然后创建一个菜单资源(context_menu.xml):
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Mostra documento" android:id="@+id/context_menu_documents_fragment_view"></item>
<item android:title="Aggiungi ad un contenitore" android:id="@+id/context_menu_documents_fragment_add_to_box"></item>
<item android:title="Elimina documento" android:id="@+id/context_menu_documents_fragment_delete"></item>
</menu>
在您注册上下文菜单的活动中,您将覆盖此方法:
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
// Inflate Menu from xml resource
MenuInflater menuInflater = getActivity().getMenuInflater();
menuInflater.inflate(R.menu.context_menu_documents_fragment, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
Toast.makeText(getActivity(), " User selected something ", Toast.LENGTH_LONG).show();
return false;
}
这非常重要,您必须修改RecyclerView
适配器中的代码,如下所示:
@Override
public void onBindViewHolder(final DocumentViewHolder viewHolder, int position) {
...
viewHolder.itemView.setLongClickable(true);
...
}
现在您可以显示上下文菜单并拦截用户选择! 但是您无法知道用户点击了哪个项目,为此您必须使用这样的自定义RecyclerView(来自Renaud Cerrato的原始代码):
public class ContextMenuRecyclerView extends RecyclerView {
private RecyclerContextMenuInfo mContextMenuInfo;
public ContextMenuRecyclerView(Context context) {
super(context);
}
public ContextMenuRecyclerView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ContextMenuRecyclerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected ContextMenu.ContextMenuInfo getContextMenuInfo() {
return mContextMenuInfo;
}
@Override
public boolean showContextMenuForChild(View originalView) {
final int longPressPosition = getChildAdapterPosition(originalView);
if (longPressPosition >= 0) {
final long longPressId = getAdapter().getItemId(longPressPosition);
mContextMenuInfo = new RecyclerContextMenuInfo(longPressPosition, longPressId);
return super.showContextMenuForChild(originalView);
}
return false;
}
public static class RecyclerContextMenuInfo implements ContextMenu.ContextMenuInfo {
public RecyclerContextMenuInfo(int position, long id) {
this.position = position;
this.id = id;
}
final public int position;
final public long id;
}
}
在上一个onContextItemSelected()
方法中,您可以使用以下代码了解recyclerView
项ID和位置:
ContextMenuRecyclerView.RecyclerContextMenuInfo info = (ContextMenuRecyclerView.RecyclerContextMenuInfo) item.getMenuInfo();
最后,你必须修改recyclerView适配器和布局文件中的getItemId()方法,以确保使用recyclerView而不是android的!
答案 2 :(得分:7)
我这样做了:
static class ViewHolder extends RecyclerView.ViewHolder {
TextView tvName;
ViewHolder(View v) {
super(v);
tvName = (TextView) v.findViewById(R.id.textView_Username);
//Single Tapup
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Position is " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
}
});
//Long Press
v.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Toast.makeText(v.getContext(), "Position is " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
return false;
}
});
}
}
答案 3 :(得分:2)
我努力在点击时获取项目的位置,这对我有用:
public void onClick(View view) {
ViewHolder holder =(ViewHolder)view.getTag();
int position = holder.getLayoutPosition();
Log.d("testing ","pos" +position);
}