2

I want to implement a long press of a CardView inside a RecyclerView

The layout:

<android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:scrollbars="vertical"
        android:longClickable="true"
        android:hapticFeedbackEnabled="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

I tried this:

mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(Utils.context));
mRecyclerView.setOnLongClickListener(new AdapterView.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                Toast.makeText(Utils.context,"dsfd",Toast.LENGTH_LONG).show();
                return true;
            }
        });

And I also tried to implement this event in the RecyclerView.ViewHolder class but nothing works. I have the long press effect but the event itself is not triggered. There is no onItemLongClickListener. I also tried this: RecyclerView onClick .

What am I missing?

Community
  • 1
  • 1
Amos
  • 1,321
  • 2
  • 23
  • 44
  • Put your listeners on the widgets in the `RecyclerView`, not on the `RecyclerView` itself. "I also tried this: RecyclerView onClick ." -- that seems to have the right answers. – CommonsWare Apr 05 '15 at 11:12
  • Regarding the first part: You mean I should define a long click event for all widgets inside the card view? what if the user long presses outside of the widgets? – Amos Apr 05 '15 at 11:25
  • "Should it be on the recycler view or not?" -- sorry. Eng.Fouad's answer is not setting a long-click listener on the `RecyclerView`. His is adding an on-item-touch listener, one that is set up to detect clicks and long-clicks. If you want the whole item to be long-clickable, that's probably the best approach. If you only need part of the items to be long-clickable, put the listener on the widgets in the items. – CommonsWare Apr 05 '15 at 11:29
  • I need the whole item to be clickable, I tried his approach and it didn't work but now I realized it was before I added android:longClickable="true" to the RecyclerView. I will try that now, though it seems like a hack more than an intuitive solution. – Amos Apr 05 '15 at 11:35
  • Some of the other answers should be relevant, such as the suggestion to use TwoWayView, if you are having problems with Eng.Fouad's answer. – CommonsWare Apr 05 '15 at 11:38
  • It worked! Thanks. Using this method while the view has its own event seems like a workaround of a bug. – Amos Apr 05 '15 at 11:44
  • Funny but now I wonder how to make a certain widget inside the clickable cardview consume the click and the cardview should ignore it, any idea? http://stackoverflow.com/questions/29486763/android-clicking-on-a-widget-inside-a-clickable-cardview – Amos Apr 07 '15 at 10:03

1 Answers1

3

You can create Interface where you should implement your onLongClicked(int position) method. And use it. Follow this steps to create your onLongClickListener:

Create your Interface:

public interface IRecyclerViewClickListener {

   void onLongClicked(int position); 
}

Create listener object in your activity/fragment and pass this listener object to your adapter in the create adapter method. Example:

// ... some code ...
adapter = new MoviesListAdapter(getActivity(), list, listener);
rvDialogs.setAdapter(adapter);
// ... code ...

In your adapter in your owned ViewHolder constructor set long listener on itemView:

public class MyViewHolder extends RecyclerView.ViewHolder {

        TextView Title;
        // ...

        public MessageViewHolder(View itemView) {
            super(itemView);

            Title = (TextView) itemView.findViewById(R.id.tvDialogTitle);
            // ...

            itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    listener.onLongClicked(getAdapterPosition());
                    return false;
                }
            });
        }
    }

After in your activity/fragment setup your IListener:

listener = new IRecyclerViewClickListener() {
            @Override
            public void onLongClicked(int position) {
                Toast.makeText(getActivity(), String.valueOf(position), Toast.LENGTH_SHORT).show();
            }
        };

Hope it help you! Good luck and sorry for my bad english ;)

Artiom
  • 47
  • 1
  • 10