Buttons on a sliding drawer? - Android

Okey, so I applied the button on a sliding box in the Android application that I am creating. The only problem is that when I press the button, the whole sliding drawer is pressed and it slides up.

I know that I can turn off "clicking on a slide up" in XML, but this does not seem to work, since the sliding box is still pressed only without a slide.

If I call slideDrawer.lock (); the function actually works, but then the sliding drawer cannot extend or is even pressed.

Anyone have a simple solution to this problem?

+4
source share
3 answers

If I understand well, have you added buttons to your SlidingDrawer descriptor and want them to work like buttons when the user clicks them, while maintaining the standard SlidingDrawer behavior when clicking / dragging a handle?

I just solved a similar problem.

My pen looked something like this:

Capture of the layout

It consists of two buttons and a TextView center, which will be a real descriptor (reacts like a standard SlidingDrawer descriptor).

To make the buttons work independently of SlidingDrawer, I changed the source code a bit in the onInterceptTouchEvent method of the standard SlidingDrawer.java class (copy the source file from the android 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 while creating the activity of the real hanlde to use:

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

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

+6
source

in response to Joakim Engstrom: Yes, it is possible! for this you need to override onInterceptTouchEvent as shown below.

  @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 must also add an installer to set the handle to the actual rudder view during the creation of the action.

+2
source
0
source

Source: https://habr.com/ru/post/1335809/


All Articles