我有一个根元素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
,在一个布局?