For this, you can use the library SlidindMenu to do this. It works like a charm. Or you can inspire you from the code of this library.
Overlaying Fragments react unexpected to click events
Question
I want to create a drawer functionality, similar to the Facebook app, with two overlaying fragments.
The main layout looks as following:
<merge
xmlns:android="http://schemas.android.com/apk/res/android" >
<fragment
android:id="@+id/menu"
android:name="ch.simon.drawertest.MenuFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="80dp" />
<fragment
android:id="@+id/home"
android:name="ch.simon.drawertest.HomeScreenFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</merge>
The home Fragment contains a button in the top left corner that opens the drawer. Opening happens with a TranslateAnimation inside the HomeScreenFragment
.
public void open(){
translateTo(canScrollXBy());
}
public void close(){
translateTo(0);
}
private void translateTo(int x){
Log.e(TAG, "Translate from " +mTranslated +" to " +x);
TranslateAnimation anim = new TranslateAnimation(mTranslated, x, 0, 0);
anim.setDuration(500);
anim.setFillAfter(true);
mRootView.startAnimation(anim);
mTranslated = x;
}
The opening and closing works fine, but I observe 2 unexpected behaviours:
Even when the drawer is closed and the
HomeScreenFragment
completely covers theMenuFragment
, theMenuFragment
still receives click events.When the drawer is open and the Button from the
HomeScreenFragment
is located in the top right corner of the screen, it still receives the click events as if it was still on the left side. Meaning even if the Button is on the right side, to tap it, I need to tap the left side of the screen.
Solution