多次使用Android的TouchDelegate苦苦挣扎 [英] Struggling with multiple use of Androids TouchDelegate

查看:64
本文介绍了多次使用Android的TouchDelegate苦苦挣扎的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个布局,其中的根元素LinearLayout包围了另外两个LinearLayouts,它们组成了两行.它们分别包含Switch.

I have a layout with a root element LinearLayout which encloses two other LinearLayouts, which make up for two rows. Those contain a Switch respectively.

实际上看起来像这样:示例图片

In reality it looks somewhat like this: example picture

这是我的示例布局文件的外观:

This is how my example layout file looks like:

<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.

Now, what I want to accomplish is, that touching the area of the first LinearLayout will trigger the first Switch and touching the area of the second LinearLayout will trigger the second Switch.

中所述,是否存在一个示例,该示例说明了如何在Android中使用TouchDelegate来增加视图的点击目标的大小?如何使用多个TouchDelegate 我使用了一个TouchDelegate来将Switch的可触摸区域增加到其父View LinearLayout.

As explained in 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 I used a TouchDelegate to increase the touchable area of the Switch to its parent View, the LinearLayout.

这是我的方法:

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()中,我为两行都调用它:

In my onCreate() I call it for both rows:

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的任何其他后续调用),它不起作用.

What I cannot understand is, that for row 1 it is working, but for row 2 (and any other successive calls to expandTouchArea) it does NOT work.

我也尝试不添加OnGlobalLayoutListener,而是将Runnable发布到Switch本身的事件消息队列中,从而导致完全相同的行为.

I also tried to instead of adding the OnGlobalLayoutListener, post a Runnable to the event message queue of the Switch itself, resulting in exactly the same behavior.

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

So the question I could not answer so far is: Is it possible to have multiple TouchDelegates for different LinearLayouts that route touch events to separate Switches respectively, in one layout?

推荐答案

我发现,当我在每行周围添加另一个LinearLayout时,两个TouchDelegates都起作用.

I found that when I add another LinearLayout around each row, both TouchDelegates work.

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

Now my example layout file looks like this:

<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>

也许还有另一种解决方案.这似乎有些乏味.

Maybe there is another solution. This one seems somehow tedious.

这篇关于多次使用Android的TouchDelegate苦苦挣扎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆