Search code examples
androidandroid-layoutandroid-recyclerview

Android recyclerview gain focus itself before its items gain focus


I want to expand my recyclerview on click on it but at the same time with this first click I do not want any recyclerview item to gain focus. Recyclerview item should gain focus on second click.

I would like you not to focus on expanding method but on gaining focus on recyclerview (or dummy parent layout '@+id/rll' if it can't be done without this) without gaining on recyclerview item. Recyclerview item should gain focus on second click.

Here is my simplified layout:

`<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="?actionBarSize"
    android:orientation="vertical"
    android:weightSum="2">

    <RelativeLayout
        android:id="@+id/rll"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:descendantFocusability="blocksDescendants"
        android:clickable="true">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rv_benchmarks"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="5dp"

            android:divider="#ffffff"
            android:dividerHeight="1dp"
            android:footerDividersEnabled="true"
            android:headerDividersEnabled="true"
            android:scrollbars="vertical" />

    </RelativeLayout>
</LinearLayout>`

I tried various combinations with descendandFocusability, focusable, focusableInTouchMode in different views and none ot these worked.


Solution

  • You can override onInterceptTouchEvent () to intercept click events, or you can try to understand the View's event distribution mechanism

    public class CustomRelativeLayout extends RelativeLayout {
        private int clickCount = 0;
        private static final int REQUIRED_CLICKS = 2; // The number of clicks you need
    
        public CustomRelativeLayout(Context context) {
            super(context);
        }
    
        public CustomRelativeLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public CustomRelativeLayout(
           Context context, 
           AttributeSet attrs, 
           int defStyleAttr
        ) {
            super(context, attrs, defStyleAttr);
        }
    
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            if (ev.getAction() == MotionEvent.ACTION_DOWN) {
                clickCount++;
                if (clickCount >= REQUIRED_CLICKS) {
                    clickCount = 0; // Reset click count
                    return false; 
                }
            }
            return true; 
        }
    }
    

    Finally, use it in your layout

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="?actionBarSize"
    android:orientation="vertical"
    android:weightSum="2">
    
    <com.yourpackage.CustomRelativeLayout
        android:id="@+id/rll"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:descendantFocusability="blocksDescendants"
        android:clickable="true">
    
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rv_benchmarks"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="5dp"
            android:divider="#ffffff"
            android:dividerHeight="1dp"
            android:footerDividersEnabled="true"
            android:headerDividersEnabled="true"
            android:scrollbars="vertical" />
    
        </com.yourpackage.CustomRelativeLayout>
    </LinearLayout>