Marcelo
Marcelo

Reputation: 2095

Composed handler within Android SlidingDrawer

My issue is the following: I want to create a sliding drawer where the handler (the view which the sliding uses to open/close) is a composed view in such a way that a button exists attached like the picture bellow:

enter image description here

The desired behavior is:

  1. When the user clicks in the handler the drawer opens (the button follows the view as it is attached on it);
  2. If I click in the button, the system makes something different (in my case, I open a dialog box with options to add views as a drawer's child);

The main problem in this type of implementation is that the listeners are in conflict due to the fact that the button "+" is part of the handler and can't create an overrided listener onclick. As first approach, I'm thinking to do all this programatically, but I really wonder if there is another easy way to do this layout.

Does anyone haves a hint to give, or knows a way to implement this with xml only? Thanks in advance!

Upvotes: 1

Views: 1489

Answers (1)

slavkoder
slavkoder

Reputation: 166

You can use LinearLayout as container for handle or content elements of SlidingDrawer container. Like following:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <SlidingDrawer android:id="@+id/drawer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="showPopUp"
        android:handle="@+id/handle"
        android:content="@+id/content">
        <LinearLayout android:id="@id/content"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <!-- here goes your content -->
        </LinearLayout>
        <LinearLayout android:id="@id/handle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#ffff5600"
            android:orientation="horizontal">
            <TextView android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="4"
                android:layout_gravity="left"
                android:text="musicas" />
            <Button android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="showPopUp"
                android:text="+" />
        </LinearLayout>
    </SlidingDrawer>
</LinearLayout>

It allows you to make layout like in the screenshot above, though it doesn't allow to capture the button press event. For this I've made some research and I suggest overriding MultiDirectionSlidingDrawer (source http://blog.sephiroth.it/2011/03/29/widget-slidingdrawer-top-to-bottom/). I did it as follows: In onInterceptTouchEvent() method I added following code after final View handle = mHandle;

    boolean handleTouch = false;
    if (mHandle instanceof ViewGroup) {
        ViewGroup group = (ViewGroup) mHandle;

        int count = group.getChildCount();
        for (int i = 0; i < count; i++) {
            View v = group.getChildAt(i);
            v.getHitRect(frame);
            if (frame.contains((int) x, (int) y)) {
                handleTouch = v.onTouchEvent(event);
                if (handleTouch) {
                    return false;
                }
            }
        }
    }

Now it handles events from the button and drawer itself separately.

Upvotes: 1

Related Questions