android recyclerview获得重点在其项目获得焦点之前

问题描述 投票:0回答:1
我想在单击时扩展我的recyclerview,但与此同时,首次点击,我不希望任何回收库中的项目获得焦点。 RecyClerview项目应重点介绍第二次点击。 我希望您不要专注于扩展方法,而希望将重点放在recyclerview(或虚拟父母布局'@+id/rll'如果不在不用此事的情况下完成),而又不会在RecyClerview项目上获得。 RecyClerview项目应重点介绍第二次点击。 我简化的布局是:

`<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尝试了各种与降落和关注性的组合,可在不同视图中进行焦点,焦点可构想。

您可以在Intercepttouchevent()上覆盖单击事件,也可以尝试了解视图的事件分布机制
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; 
    }
}

最终在布局中使用它
android android-layout android-recyclerview
1个回答
0
投票

© www.soinside.com 2019 - 2025. All rights reserved.