最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Android recyclerview gain focus itself before its items gain focus - Stack Overflow

programmeradmin0浏览0评论

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=";
    xmlns: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.

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/apk/res/android"
    xmlns:tools="http://schemas.android/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.

Share Improve this question asked Feb 1 at 11:49 JawegielJawegiel 1014 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

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/apk/res/android"
xmlns:tools="http://schemas.android/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>
发布评论

评论列表(0)

  1. 暂无评论