How to make RecyclerView impractical and pass onClick event to parent mode?

Is it possible to make RecyclerView not clickable? I want this because my RecyclerView only shows small icons in the CardView clickable . Therefore, if someone clicks the icons, he should just click (and animate) the parent CardView .

I tried the following:

  • recyclerView.setClickable(false);
  • recyclerView.setFocusable(false);
  • Extend RecyclerView and make onTouchEvent(MotionEvent) return false .
  • Use method 3 above and use itemView.setClickable(false); in the RecyclerView adapter. This works, a click is sent to the parent element. However, now RecyclerView no longer scrolls.
  • Set clickable="false" , focusable="false" , focusableInTouchMode="false" in the bloated XML list item. (See Comment by @Ibrahim)
  • Call recyclerView#setLayoutFrozen(true) and itemView.setClickable(false); . This works, but has the same issue as # 4.

Any ideas on how to disable and pass the RecyclerView click events to the parent view? Note that the RecyclerView still needs to scroll (horizontally).


EDIT:
User @ c.dunlap suggested setting OnClick icons to icons and simply redirecting the click to the parent click. This will work, but there will be no click animation on the parent view. And if someone clicks on the itemView element, but still inside the RecyclerView (for example, ItemDecoration padding) - the click is not detected. Unfortunately, this is not a solution.

+8
java android android-layout android-recyclerview
source share
3 answers

My preferred way of handling such things is to attach a listener from your adapter, which will be called when each icon in your recycler view is clicked. Then your activity can react accordingly. For example:

 public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private MyAdapterListener mListener; public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mListener.iconClicked(); } }); } public void setListener(MyAdapterListener listener) { mListener = listener; } public interface MyAdapterListener { void iconClicked(); } } 

Then in your activity, you can simply create an instance of MyAdapterListener and set it as a listener for your Recycler View adapter. Then, when the iconClicked() function iconClicked() , execute the code that will execute with the parent click.

+2
source share

You must expand the parent view and intercept the click, so recyclerview will not receive it.

 @Override public boolean onInterceptTouchEvent(MotionEvent ev) { return true; } 

Loans for: Solution

0
source share

I had the same problem, after I tried a lot more things, I used another layout for recyclerview.

 <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:layoutwidth="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"/> </RelativeLayout> 

after that you can see and disappear that "LinearLayout". It works 100%.

-one
source share

All Articles