Monday, 2 December 2019

Click Outside the RecyclerView to Hide ChildView in RecyclerView Row

I have a simple RecyclerView and when you LongClick a Row in RecyclerView I make a hidden LinearLayout visible on that row. To do so In my public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) I am using the following

LinearLayout REACTION_LAYOUT = RecyclerItem.FindViewById<LinearLayout>(Resource.Id.reaction_layout);
RecyclerItem.LongClick += (sender, e) => { OnLongClick(REACTION_LAYOUT); };

REACTION_LAYOUT is the layout I am showing using the following

private void OnLongClick(LinearLayout _ReactionLayout)
        {
            try
            {  
                _ReactionLayout.Visibility = ViewStates.Visible;

            }
            catch (Exception X)
            {
                Log.Info("1022585", "CLICK (ERROR) : " + X.Message);
            }
        }

This works as Intended, Now what I want is to HIDE the REACTION_LAYOUT when a user touches anywhere outside the Row which means on other rows in RecyclerView or anywhere else

How do I do that?

The approach I tried to use was to detect a touch on parent within my Adapter's OnCreateViewHolder as following

parent.Click += (sender, e) => { OnParentClick(REACTION_LAYOUT); }; 

This works but It disables the scrolling on recycler view.

What is the best way to achieve this?

EDIT: The activity RecyclerView is the following

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/main_message_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?attr/colorPrimary"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    android:orientation="vertical"> 
  <include layout="@layout/toolbar_messages"/>   
  <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="4.5dp"
      android:layout_marginTop="3dp"
      android:background="@drawable/shadow"/>  
  <RelativeLayout     
    android:layout_width="match_parent"
    android:layout_height="fill_parent">   
  <androidx.recyclerview.widget.RecyclerView
      android:id="@+id/rvMessages"
     android:layout_width="match_parent"
      android:layout_height="wrap_content"         
      android:background="?attr/colorPrimary"      
      android:layout_alignParentTop="true"
    android:paddingBottom="70dp"/>  .......

EDIT 2:

Also tried to set the OnTouchLisener, but still no luck

public class _OnRecylerTouch_Listener : Java.Lang.Object, View.IOnTouchListener
        {
            private LinearLayout _L;
            public _OnRecylerTouch_Listener(LinearLayout L)
            {
                _L = L;
            }
            public bool OnTouch(View v, MotionEvent e)
            {
                _L.Visibility = ViewStates.Gone;
                return true;
            }
        }


from Click Outside the RecyclerView to Hide ChildView in RecyclerView Row

No comments:

Post a Comment