2017-04-20 25 views
0

我有一个根元素LinearLayout的布局,其中包含另外两个LinearLayouts,它们组成了两行。那些分别包含Switch多次使用Android进行斗争TouchDelegate

在现实中,它看起来有点像这样: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的可触摸区域到其父View,所述LinearLayout

这里是我的这个方法:

​​

在我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,发布RunnableSwitch本身的事件消息队列,导致完全相同的行为。

所以问题我不能到目前为止的回答是:是否有可能有多个TouchDelegates针对不同LinearLayouts这条路线的触摸事件分别分开Switches,在一个布局?

回答

0

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

也许有另一种解决方案。这个似乎有点乏味。