recycleview 支持context menu
由于正常recycleview注册context menu 后在 onContextItemSelected 里获取的 item.getMenuInfo() 为空, 不能正常使用长按menu,因此需要改造一下recycleview即可, 具体如下:
public class ContextMenuRecycleView extends RecyclerView {
private AdapterView.AdapterContextMenuInfo contextMenuInfo;
public ContextMenuRecycleView(Context context) {
super(context);
}
public ContextMenuRecycleView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public ContextMenuRecycleView(Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public AdapterView.AdapterContextMenuInfo getContextMenuInfo() {
return contextMenuInfo;
}
@Override
public boolean showContextMenuForChild(View originalView) {
int position = getChildAdapterPosition(originalView);
long id = getChildItemId(originalView);
contextMenuInfo = new AdapterView.AdapterContextMenuInfo(originalView, position, id);
return super.showContextMenuForChild(originalView);
}
}
具体原文参考 为RecycleView添加ContextMenu支持 - 爱码网 (likecs.com)