繁体   English   中英

onFling和LinearLayouts

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM