我有一個根元素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
,發佈Runnable
到Switch
本身的事件消息隊列,導致完全相同的行爲。
所以問題我不能到目前爲止的回答是:是否有可能有多個TouchDelegates
針對不同LinearLayouts
這條路線的觸摸事件分別分開Switches
,在一個佈局?