Android - 长期点击RecyclerView项目和ContextMenu

时间:2016-07-27 08:12:04

标签: android android-recyclerview contextmenu

问题:无法显示显示"删除"的上下文菜单选项"长时间点击recyclerview中的项目时

预期结果:见下图

我几乎就在那里,但是我错过了一些东西,以便在longClick上显示contextMenu。

这是我放在viewHolder中的内容。我不知道应该添加什么以及在onLongClick事件中显示上下文菜单的位置。

我跳过了一些代码并保留了与我的问题相关的代码。

非常感谢您的协助,

我处理这两种点击的界面

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);

Expected result

3 个答案:

答案 0 :(得分:9)

你需要的是在里面显示带有列表的Dialog。像那样:

    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;
        }
    });

答案 1 :(得分:0)

编辑1:要专门显示对话框,请使用

data-*

其中v指视图。和

 openContextMenu(v);

Menu文件夹中@Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.context_menu,menu); } @Override public boolean onContextItemSelected(MenuItem item) { int id = item.getItemId(); default: return super.onContextItemSelected(item); } } 文件中的代码应如下所示:

context_menu

过去,我在使用RecyclerView <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context="com.example.MainActivity"> <item android:id="@+id/item" android:orderInCategory="200" android:title="Item" android:icon="@drawable/faq" app:showAsAction="ifRoom" ></item> </menu> onClickListener时遇到了一些问题。所以这就是我正在使用的那个:

onLongClickListener

然后在活动中,使用以下内容:

public class ItemClickSupport {
    private final RecyclerView mRecyclerView;
    private OnItemClickListener mOnItemClickListener;
    private OnItemLongClickListener mOnItemLongClickListener;
    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mOnItemClickListener != null) {
                RecyclerView.ViewHolder holder = mRecyclerView.getChildViewHolder(v);
                mOnItemClickListener.onItemClicked(mRecyclerView, holder.getAdapterPosition(), v);
            }
        }
    };
    private View.OnLongClickListener mOnLongClickListener = new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            if (mOnItemLongClickListener != null) {
                RecyclerView.ViewHolder holder = mRecyclerView.getChildViewHolder(v);
                return mOnItemLongClickListener.onItemLongClicked(mRecyclerView, holder.getAdapterPosition(), v);
            }
            return false;
        }
    };
    private RecyclerView.OnChildAttachStateChangeListener mAttachListener
            = new RecyclerView.OnChildAttachStateChangeListener() {
        @Override
        public void onChildViewAttachedToWindow(View view) {
            if (mOnItemClickListener != null) {
                view.setOnClickListener(mOnClickListener);
            }
            if (mOnItemLongClickListener != null) {
                view.setOnLongClickListener(mOnLongClickListener);
            }
        }

        @Override
        public void onChildViewDetachedFromWindow(View view) {

        }
    };

    private ItemClickSupport(RecyclerView recyclerView) {
        mRecyclerView = recyclerView;
        mRecyclerView.setTag(R.id.item_click_support, this);
        mRecyclerView.addOnChildAttachStateChangeListener(mAttachListener);
    }

    public static ItemClickSupport addTo(RecyclerView view) {
        ItemClickSupport support = (ItemClickSupport) view.getTag(R.id.item_click_support);
        if (support == null) {
            support = new ItemClickSupport(view);
        }
        return support;
    }

    public static ItemClickSupport removeFrom(RecyclerView view) {
        ItemClickSupport support = (ItemClickSupport) view.getTag(R.id.item_click_support);
        if (support != null) {
            support.detach(view);
        }
        return support;
    }

    public ItemClickSupport setOnItemClickListener(OnItemClickListener listener) {
        mOnItemClickListener = listener;
        return this;
    }

    public ItemClickSupport setOnItemLongClickListener(OnItemLongClickListener listener) {
        mOnItemLongClickListener = listener;
        return this;
    }

    private void detach(RecyclerView view) {
        view.removeOnChildAttachStateChangeListener(mAttachListener);
        view.setTag(R.id.item_click_support, null);
    }

    public interface OnItemClickListener {

        void onItemClicked(RecyclerView recyclerView, int position, View v);
    }

    public interface OnItemLongClickListener {

        boolean onItemLongClicked(RecyclerView recyclerView, int position, View v);
    }
}

使用ItemClickSupport.addTo(recyclerView).setOnItemLongClickListener(new ItemClickSupport.OnItemLongClickListener() { @Override public boolean onItemLongClicked(RecyclerView recyclerView, int position, View v) { return true; } }); 指定项目。希望它有所帮助!

答案 2 :(得分:0)

从技术上讲,可接受的答案不是“答案”,而是一种聪明的解决方法,因为它创建了 body .woocommerce table.shop_table td { background-color: blue; } 而不是OP要求的AlertDialog,它确实是“解决了要求”但不会创建ContextMenu

这是我找到的最短的实现:

在“活动”中,照常覆盖:

ContextMenu

在RecyclerView的ViewHolder中:

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    menu.add("do this");
    menu.add("do that");
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    if (item.getTitle().equals("do this") {
        // do whatever this...
    }
    else if (item.getTitle().equals("do that") {
        // do whatever that...
    }
    return super.onContextItemSelected(item);
}

在活动中onCreate:

private class ItemViewHolder extends RecyclerView.ViewHolder {
    ...
    private ItemViewHolder(@NonNull View pItemView) {
        super(pItemView);
        pItemView.setLongClickable(true); // <-- make long clickable
        ...
    }
}