javaandroidandroid-linearlayoutonfling

onFling and LinearLayouts


My Linear Layout: main_linear_layout contains four Linear Layouts: ll1 ll2 ll3 ll4. Each LinearLayout contains Buttons. I am trying to implement the onFling method on main_linear_layout. I want to be able to swipe anywhere on the Layout of Buttons and call an action.

Currently I am able to swipe anywhere on the screen except for the Layout of Buttons. I tried using the following code to fix the problem:

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;
    }
});

But I am still not able to swipe on the Layout of Buttons. Does anybody know why this is? Is there something else I need to implement? Am I supposed to implement an OnTouchListener to every Linear Layout inside the main Linear Layout? Or to every Button in each Layout?

And in the xml I added a bunch of code to the main Linear Layout:

android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:longClickable="true" 

But this did not work either. Can someone please help?


Solution

  • If you have a LinearLayout, and then more layouts inside of that, and then buttons on those as you say you do, then this is functioning as intended.

    You're only adding the listener to the outside layout...so of course, it'll only trigger when you slide on it. By sliding on the other layouts, or even the buttons, the slide event doesn't even get to that listener, because it is consumed.

    You'd need to add the listener to each element that you want to be checking for.

    One way to do this is to create an array of your buttons and do it all at once:

    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);
        }
    }