Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Result expected: see image below

I'm almost there, but I'm missing something to make the contextMenu displayed on a longClick.

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.

Thanks a lot for your assistance,

My interface to handle both types of clicks

public interface OnItemClickListener{
    void onItemClick(int position);
}

public interface OnItemLongClickListener{
    void onItemLongClick(int position);
}

Viewholder code

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
    }

Adapter code

@Override
    public void onBindViewHolder(CityListViewholder holder, int position) {
        holder.cityName.setText(cityArrayList.get(position).getCityName());
        holder.bindClick(position, onItemClickListener);
        holder.bindLongClick(position, onItemLongClickListener);
    }

Then, in the activity - I skipped what is not relevant for my question

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

like image 582
Isabelle Avatar asked Jul 27 '16 08:07

Isabelle


2 Answers

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;
        }
    });
like image 200
Divers Avatar answered Oct 08 '22 06:10

Divers


Accepted answer isn't technically "the answer", it is rather a clever workaround since it creates an AlertDialog instead of a ContextMenu as requested by the OP, it does "solve the requirement" but it does not create a ContextMenu.

This is the shortest implementation I found:

In Activity, override as usual:

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

In RecyclerView's item ViewHolder:

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

In Activity onCreate:

registerForContextMenu(myRecyclerView);
like image 35
AlexVPerl Avatar answered Oct 08 '22 04:10

AlexVPerl