How to disable behind view click event Framelayout

无人久伴 提交于 2019-11-27 18:39:34
 imageView.setSingleTapListener(new OnImageViewTouchSingleTapListener() {
                    @Override
                    public void onSingleTapConfirmed() {
                        Log.d("TAG", "setSingleTapListener");

                        sCounter++;
                        if (sCounter % 2 == 0) {
                            mRlTopOverlayBar.setVisibility(View.GONE);
                            mRlBottomOverlayBar.setVisibility(View.GONE);
                            pager.requestFocus();
                        } else {
                            mRlTopOverlayBar.setVisibility(View.VISIBLE);
                            mRlBottomOverlayBar.setVisibility(View.VISIBLE);
                            mRlTopOverlayBar.requestFocus();
                            mRlBottomOverlayBar.requestFocus();
                            mRlBottomOverlayBar.setClickable(true);
                            mRlTopOverlayBar.setClickable(true);
                        }
                    }
                });

A better way is to set the top and bottom frame to be clickable, with:

android:clickable="true"

Doing so will make sure that the view/frame itself will trap all clicking events, and will not pass it through the view behind it. Note this method works for all layout/view/controls, but many controls (such as buttons) already have this function on by default.

burulangtu

android:clickable="true" for top and bottom bar.

or give each FrameLayout an onClickListener.

@gordon1hd1 answer is correct but for those who are still confused, I am adding my layout which contains a FrameLayout as parent and a LinearLayout and twoImageViews as childs.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">         
     <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/scroll_parent"
            android:orientation="horizontal" />
    <ImageView
        android:id="@+id/ivArrowLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="4dp"
        android:src="@drawable/actionbar_back"
        android:layout_gravity="left|center_vertical"
        android:background="#3f808080"
        android:clickable="true"
        />
    <ImageView
        android:id="@+id/ivArrowRight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="4dp"
        android:src="@drawable/actionbar_back"
        android:layout_gravity="right|center_vertical"
        android:background="#3f808080"
        android:rotation="180"
        android:clickable="true"
        />
</FrameLayout>

Previously, the Linearlayout was also intercepting touch events when either of ImageViews were pressed. Adding android:clickable="true" to both ImageViews resolved the issue.

If you are also facing this type of issue, add android:clickable="true" to the view you want to trap the clicking event.

Simply, Set android:clickable="true" in xml to your foreground view.

Vijay Sonawane
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/llSettings"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="right"
    android:background="#ff106279"
    android:minHeight="25px"
    android:minWidth="25px"
    android:onClick="click"
    android:orientation="vertical"
    android:visibility="visible">

    <LinearLayout
        android:id="@+id/llSettings1"
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        android:background="#ff000f"
        android:clickable="true"
        android:minHeight="25px"
        android:minWidth="25px"
        android:orientation="vertical"
        android:visibility="visible">

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:onClick="buttonclick"
            android:text="New Button" />
    </LinearLayout>
</RelativeLayout>

and

public void click(View v) {
    Toast.makeText(this, "((RelativeLayout)v).toString()", Toast.LENGTH_SHORT).show();
}

public void buttonclick(View v) {
    Toast.makeText(this, "Button", Toast.LENGTH_SHORT).show();
}
Garry Dias

Add android:clickable="true" to rl_bottom_overlay and rl_top_overlay. If you don´t set click events to these layouts (nor via XML neither programatically), no events will be triggered on background views.

just add this to each of your framelayout view containers so that absorb the click:

  android:clickable="true"
    android:focusable="true"
    android:focusableInTouchMode="true"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!