Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Buttons on a Sliding Drawer? - Android

Tags:

android

Okey, so I've implemented a button on a Sliding drawer in a android application I'm building. The only problem is that when I press the button the whole sliding drawer is pressed and it slides up.

I know I can disable 'press to slide up' in the XML, but that does not seem to work as the sliding drawer still is pressed just without the slide up.

If I call the slidingDrawer.lock(); function the button actually works but then the sliding drawer can't slide up or even be pressed up.

Any one have a simple solution to this problem?

like image 356
Joakim Engstrom Avatar asked Feb 25 '23 08:02

Joakim Engstrom


1 Answers

If I understand well you have added buttons on your SlidingDrawer handle and you want them to work like buttons when the user press them with keeping a standard SlidingDrawer behaviour when the handle is pressed/dragged?

I just solved a similar problem.

My Handle was looking something like that:

Capture of the layout

It's composed of two buttons and a center TextView which will be the real handle (reacting as a standard SlidingDrawer handle).

To make the buttons work independently of the SlidingDrawer I changed a bit of source code in the onInterceptTouchEvent method of the standard SlidingDrawer.java class (copy paste the source file from the android code source):

public boolean onInterceptTouchEvent(MotionEvent event) {
    //...
    final Rect frame = mFrame;
    final View handle = mHandle;

    // original behaviour
    //mHandle.getDrawingRect(frame);

    // New code
    View trackHandle = mTrackHandle;
    // set the rect frame to the mTrackHandle view borders instead of the hole handle view

    // getParent() => The right and left are valid, but we need to get the parent top and bottom to have absolute values (in screen)
    frame.set(trackHandle.getLeft(), ((ViewGroup) trackHandle.getParent()).getTop(), trackHandle.getRight(), ((ViewGroup) trackHandle.getParent()).getBottom());



    if (!mTracking && !frame.contains((int) x, (int) y)) {
        return false;
    }
    //...
}

I also added a setter for the mTrackHandle attribute to set, during the activity creation, the real hanlde to use:

protected void onCreate(Bundle savedInstanceState) {
    //...
    mSlidingDrawer.setTrackHandle((View) findViewById(R.id.menu_handle_TextView_Title));
    //...
}

After that you can set standard listener on your two buttons. They will work like a charm.

like image 77
ol_v_er Avatar answered Mar 07 '23 09:03

ol_v_er