2017-04-20 46 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> 

也許有另一種解決方案。這個似乎有點乏味。