Removing OnClickListener in the view removes all touch events on the views behind

I have a top bar layout with a title, a vertical menu and a transparent background.

When btn_menu pressed btn_menu vertical menu opens with animation. When the menu is open, I set OnClickListener to a transparent background, which closes the menu when a transparent background is clicked. When closing the menu, I OnClickListener from the background view using:

 mTopBarBg.setOnClickListener(null); 

The problem is that it seems to remove all touch events behind its looks (set in the content_container main layout). For instance. a ViewPager that no longer detects swipes, or a ListView that no longer scrolls and can no longer be pressed while they are working correctly.

What's wrong?

in the upper panel fragment

 private void toggleMenu(int duration){ if(mMenuIsOpen){ TranslateAnimation anim1 = new TranslateAnimation(0,0,0,-(mHeight-mMenuVerticalOffset)); anim1.setFillAfter(true); anim1.setDuration(duration); mVerticalMenu.setAnimation(anim1); AlphaAnimation anim2 = new AlphaAnimation(0.7f, 0.0f); anim2.setFillAfter(true); anim2.setDuration(duration); mTopBarBg.setAnimation(anim2); mTopBarBg.setOnClickListener(null); mMenuIsOpen = false; } else{ TranslateAnimation anim1 = new TranslateAnimation(0,0,-(mHeight-mMenuVerticalOffset),0); anim1.setFillAfter(true); anim1.setDuration(duration); mVerticalMenu.setAnimation(anim1); AlphaAnimation anim2 = new AlphaAnimation(0.0f, 0.7f); anim2.setFillAfter(true); anim2.setDuration(duration); mTopBarBg.setAnimation(anim2); mTopBarBg.setOnClickListener(mBgClickListener); mMenuIsOpen = true; } } 

main layout

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/mainbg" android:scaleType="centerCrop"/> <FrameLayout android:id="@+id/content_container" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingTop="44dp" /> <FrameLayout android:id="@+id/top_bar_container" android:layout_width="match_parent" android:layout_height="match_parent" android:clipChildren="false" /> </RelativeLayout> 

top panel layout

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00000000" > <View android:id="@+id/top_bar_bg" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000000" android:visibility="gone" /> <LinearLayout android:id="@+id/vertical_menu" android:layout_width="50dp" android:layout_height="match_parent" android:layout_marginTop="44dp" android:background="#ffffff" android:orientation="vertical" android:visibility="gone" > <!-- vertical menu layout --> </LinearLayout> <RelativeLayout android:id="@+id/header" android:layout_width="match_parent" android:layout_height="44dp" android:background="#ffffff" > <Button android:id="@+id/btn_menu" android:layout_width="50dp" android:layout_height="44dp" android:background="@drawable/menubtn" /> <LinearLayout android:layout_width="match_parent" android:layout_height="44dp" android:layout_toRightOf="@id/btn_menu" android:gravity="center" > <ImageView android:layout_width="130dp" android:layout_height="44dp" android:src="@drawable/logo" /> </LinearLayout> </RelativeLayout> </RelativeLayout> 

top panel with open menu

enter image description here

+6
source share
2 answers

Try also using setClickable(false) on your View overlay. Using the setOnClickListener() method makes View accessible to clicks, and it probably eats up your future events (even after using null , as this does not change the previous clickable property set).

As a side, not your layout is very complicated in relation to what you are trying to do. More precisely, the layout is deeper than it should be. It makes no sense to have a FrameLayout container if it has the same properties as the RelativeLayout your panel (you can use the include tag to enable the panel without the need for this additional FrameLayout ). You can also lose top_bar_bg View and install the listener directly on the RelativeLayout root directory. Finally, the internal RelativeLayout (header) can be removed and replaced by correctly placing the child views in the root directory of the RelativeLayout with a white blank View below them (with the appropriate dimensions to simulate a white background).

+12
source

Instead of removing onClickListener you can do one thing. Always set onClickListener and use boolean variable to track menu on. and off This way your code will look like

  boolean is_menu_open = false; public void onClick(View v){ if(is_menu_open){ hidemenu; is_menu_open = false; } else do nothing } 

and whenever you show the menu set is_menu_open to true

+1
source

All Articles