开发者

Buttons on a Sliding Drawer? - Android

开发者 https://www.devze.com 2023-02-04 21:05 出处:网络
Okey, so I\'ve implemented a button on a Sliding drawer in a android application I\'m building. The only problem is that wh开发者_C百科en I press the button the whole sliding drawer is pressed and it

Okey, so I've implemented a button on a Sliding drawer in a android application I'm building. The only problem is that wh开发者_C百科en 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?


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:

Buttons on a Sliding Drawer? - Android

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.


in response to Joakim Engstrom: Yes that's possible! to do that you have to override onInterceptTouchEvent as follow.

        @Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub
    rect = new Rect(handle.getLeft(), ((View) handle.getParent()).getTop(),
            handle.getRight(), ((View) handle.getParent()).getBottom());
    if (!rect.contains((int) event.getX(), (int) event.getY())) {
        if (event.getAction() == MotionEvent.ACTION_UP)
            this.lock();
        else
            this.unlock();
        return false;
    } else {
        this.unlock();
        return super.onInterceptTouchEvent(event);
    }
}

you have also to add a setter to set handle to actual handle view during activity creation.


may be this code can help you https://github.com/xPutnikx/SlidingDrawerWithButtons

0

精彩评论

暂无评论...
验证码 换一张
取 消