【问题标题】:Struggling with multiple use of Androids TouchDelegate多次使用 Android TouchDelegate 苦苦挣扎
【发布时间】:2017-04-20 15:03:35
【问题描述】:

我有一个带有根元素 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?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,而是将Runnable 发布到Switch 本身的事件消息队列中,结果完全相同。

所以到目前为止我无法回答的问题是:是否可以在一个布局中为不同的LinearLayouts 设置多个TouchDelegates,将触摸事件分别分开Switches

【问题讨论】:

    标签: android android-layout user-interface touch android-view


    【解决方案1】:

    我发现当我在每一行周围添加另一个 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>
    

    也许还有其他解决方案。这似乎有点乏味。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-03
      • 1970-01-01
      • 2015-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多