Android-长按RecyclerView项目和ContextMenu [英] Android - Long Click on RecyclerView item and ContextMenu
问题描述
问题:在回收站视图中长时间单击某个项目时,无法显示上下文菜单,其中显示删除"选项"
Problem: Cannot display a context menu showing a "delete" option" when longclicking on an item within a recyclerview
预期结果:请参见下图
我快到了,但是我缺少一些东西可以使contextMenu显示在longClick上.
I'm almost there, but I'm missing something to make the contextMenu displayed on a longClick.
这是我放入viewHolder中的内容.我不知道应在onLongClick事件中添加什么内容以及在哪里显示上下文菜单.
Here is what I put in the viewHolder. I don't know what I should add and where to display the context menu in the onLongClick event.
我跳过了一些代码行,并保留了与我的问题相关的代码.
I skipped some lines of code and kept the ones relevant to my question.
非常感谢您的帮助,
我的界面可以处理两种类型的点击
public interface OnItemClickListener{
void onItemClick(int position);
}
public interface OnItemLongClickListener{
void onItemLongClick(int position);
}
观看者代码
public void bindLongClick(final int position, final OnItemLongClickListener onItemLongClickListener) {
itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
onItemLongClickListener.onItemLongClick(position);
return true;
}
});
}
@Override
public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo){
//menuInfo is null
Log.v(LOG_TAG, "grrr");
contextMenu.setHeaderTitle("Select The Action");
contextMenu.add(0, view.getId(), 0, "Supprimer");//groupId, itemId, order, title
}
适配器代码
@Override
public void onBindViewHolder(CityListViewholder holder, int position) {
holder.cityName.setText(cityArrayList.get(position).getCityName());
holder.bindClick(position, onItemClickListener);
holder.bindLongClick(position, onItemLongClickListener);
}
然后,在活动中-我跳过了与我的问题无关的内容
mCityListAdapter = new CityListAdapter(mContext, cityArrayList, new CityListAdapter.OnItemClickListener() {
@Override
public void onItemClick(int position) {
mPager.setCurrentItem(position);
mDrawerLayout.closeDrawers();
}
}, new CityListAdapter.OnItemLongClickListener() {
@Override
public void onItemLongClick(int position) {
Log.v(LOG_TAG, "Position "+position);
}
});
registerForContextMenu(mRecyclerView);
推荐答案
您需要的是显示内部带有列表的Dialog.像这样:
What you need there is to show Dialog with list inside. Like that:
itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
final CharSequence[] items = {"Supprimer", "etc", "etc1"};
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle("Select The Action");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
}
});
builder.show();
return true;
}
});
这篇关于Android-长按RecyclerView项目和ContextMenu的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!