[英]onFling and LinearLayouts
我的線性布局: main_linear_layout
包含四個線性布局: ll1
ll2
ll3
ll4
。 每個LinearLayout
包含Buttons
。 我想實現的onFling方法main_linear_layout
。 我希望能夠在“ Buttons
布局”上的任意位置滑動並調用一個動作。
目前,我可以在屏幕上的任意位置滑動(按鈕布局除外)。 我嘗試使用以下代碼解決此問題:
swipeLayout = (LinearLayout) findViewById(R.id.main_linear_layout);
//swipeLayout is the main Layout that contains 4 other linear layouts
swipeLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
gestureScanner.onTouchEvent(event);
return true;
}
});
但是我仍然無法在“ Buttons
的布局”上滑動。 有人知道這是為什么嗎? 還有什么我需要實現的嗎? 我是否應該對主要線性布局中的每個線性布局實現OnTouchListener
? 還是每個布局中的每個Button
?
然后在xml
中,向主要的線性布局中添加了一堆代碼:
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:longClickable="true"
但這也不起作用。 有人可以幫忙嗎?
如果您具有LinearLayout
,然后在其中具有更多布局,然后按您說的那樣在這些布局上LinearLayout
按鈕,那么這將按預期發揮作用。
您只是將偵聽器添加到外部布局中...因此,當然,它僅在您在其上滑動時才會觸發。 通過在其他布局甚至按鈕上滑動,滑動事件甚至不會到達該偵聽器,因為它被消耗了。
您需要將偵聽器添加到要檢查的每個元素。
一種方法是創建按鈕數組,然后一次完成所有操作:
private Button button1;
private Button button2;
....
private Button button10;
...
protected void onCreate(Bundle b) {
super.onCreate(b);
button1 = findViewById(R.id.button1);
...
button10 = findViewById(R.id.button10);
OnClickListener onCL = new OnClickListener . . . //do all of your creation here
Button[] buttons = {button1, button2, . . . , button10};
for(Button b : buttons) {
b.setOnClickListener(onCL);
}
}
好吧,ll1,ll2,ll3,ll4之所以無法接收觸摸事件,是因為父線性布局接收到了運動事件,並且它不會進一步傳播。
為什么在主要的線性布局上需要觸摸事件監聽器? 您是否希望所有子布局一起出現?
如果您想讓其他布局在touch事件中使用,請在touch監聽器上返回false
swipeLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
gestureScanner.onTouchEvent(event);
return false;
}
});
或者,您可以創建4個GestureDetectors並將事件傳遞給右邊的事件,具體取決於按下的視圖
swipeLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(v.getId()){
case R.id.ll1:
gesture1.ontouchEvent(e);
break;
case R.id.ll2:
gesture1.ontouchEvent(e);
break;
}
//etc
return false;
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.