与多次使用 Androids TouchDelegate 作斗争

Struggling with multiple use of Androids TouchDelegate

我有一个包含根元素 LinearLayout 的布局,它包含另外两个 LinearLayouts,它们组成两行。那些分别包含一个Switch

实际上它看起来有点像这样:example picture

这是我的示例布局文件的样子:

<LinearLayout android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <LinearLayout android:id="@+id/layout_row_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Switch android:id="@+id/switch_row_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout android:id="@+id/layout_row_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Switch android:id="@+id/switch_row_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
</LinearLayout>

现在,我想要完成的是,触摸第一个 LinearLayout 的区域将触发第一个 Switch,触摸第二个 LinearLayout 的区域将触发第二 Switch.

Is there an example of how to use a TouchDelegate in Android to increase the size of a view's click target? and How To Use Multiple TouchDelegate 中所述,我使用 TouchDelegate 将 Switch 的可触摸区域增加到其父级 ViewLinearLayout.

这是我的方法:

public static void expandTouchArea(final View smallView, final View largeView) {
largeView.getViewTreeObserver().addOnGlobalLayoutListener(
        new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect delegateArea = new Rect();
                largeView.getHitRect(delegateArea);
                TouchDelegate delegate = new TouchDelegate(delegateArea, smallView);
                largeView.setTouchDelegate(delegate);
            }
        });
}

在我的 onCreate() 中,我为两行调用它:

mLayoutRow1 = findViewById(R.id.layout_row_1);
mLayoutRow2 = findViewById(R.id.layout_row_2);

mSwitchRow1 = (Switch) findViewById(R.id.switch_row_1);
mSwitchRow2 = (Switch) findViewById(R.id.switch_row_2);

expandTouchArea(mSwitchRow1, mLayoutRow1);
expandTouchArea(mSwitchRow2, mLayoutRow2);

我无法理解的是,对于第 1 行它有效,但对于第 2 行(以及对 expandTouchArea 的任何其他连续调用)它不起作用。

我还尝试将 OnGlobalLayoutListener、post 和 Runnable 添加到 Switch 本身的事件消息队列中,结果完全相同.

所以到目前为止我无法回答的问题是:是否有可能有多个TouchDelegates用于不同的LinearLayouts路由触摸事件以分开Switches 分别在一个布局中?

我发现当我在每一行周围添加另一个 LinearLayout 时,两个 TouchDelegates 都有效。

现在我的示例布局文件如下所示:

<LinearLayout android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout android:id="@+id/layout_row_1"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content" >

            <Switch android:id="@+id/switch_row_1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout android:id="@+id/layout_row_2"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content" >

            <Switch android:id="@+id/switch_row_2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

也许还有其他解决方案。这个看起来有点乏味。