【问题标题】:Prevent click events in a FrameLayout防止 FrameLayout 中的点击事件
【发布时间】:2017-03-28 21:28:23
【问题描述】:

我有一个FrameLayout,在FrameLayout里面我有两个视图,一个是ViewPager,另一个是LinearLayoutLinearLayout就像一个窗口,所以它有一些按钮.我使用可见性属性显示和隐藏LinearLayout。问题是当我显示LinearLayout 时,它覆盖了ViewPager,但用户仍然可以在ViewPager 内滚动ListView。我想阻止所有点击事件到后面的视图。

XML 看起来像这样:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="@color/colorBeige"
          android:orientation="vertical">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar_diner_food"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@android:color/white"
    android:contentInsetLeft="0dp"
    android:contentInsetStart="0dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:titleTextColor="@android:color/white"
    app:contentInsetLeft="0dp"
    app:contentInsetStart="0dp"/>

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout_diner_food"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:background="@android:color/white"/>

        <android.support.v4.view.ViewPager
            android:id="@+id/view_pager_diner_food"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linea_layout_filter"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorBlackAlpha">

        <com.wonderkiln.blurkit.BlurLayout
            android:id="@+id/blurLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:clipChildren="true"
                android:orientation="vertical">

                <SeekBar
                    android:id="@+id/seek_bar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="61dp"/>
            </LinearLayout>
        </com.wonderkiln.blurkit.BlurLayout>
    </LinearLayout>
</FrameLayout>

【问题讨论】:

  • 我在你的问题中编辑的代码对你有用,我也为你的问题编辑了我的答案并查看了
  • 让我知道这是否有效
  • @NarendraSorathiya 没用。

标签: android layout click android-framelayout


【解决方案1】:

在顶部位置的视图中添加一个 onTouchEvent 方法,然后返回 true。 True 将告诉事件冒泡该事件已被消耗,因此防止事件冒泡到其他视图。

protected boolean onTouchEvent (MotionEvent me) {
    return true;
}

防止在 ListView 下滚动 要禁用任何点击,您可以使用

view.setClickable(false);

【讨论】:

    【解决方案2】:

    当您单击LinearLayout 的任何部分时,它将阻止滚动到ListView

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="@color/colorBeige"
          android:orientation="vertical">
    
    <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar_diner_food"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@android:color/white"
    android:contentInsetLeft="0dp"
    android:contentInsetStart="0dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:titleTextColor="@android:color/white"
    app:contentInsetLeft="0dp"
    app:contentInsetStart="0dp"/>
    
    <FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout_diner_food"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:background="@android:color/white"/>
    
        <android.support.v4.view.ViewPager
            android:id="@+id/view_pager_diner_food"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>
    
    </LinearLayout>
    
    <LinearLayout
        android:id="@+id/linea_layout_filter"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorBlackAlpha">
    
        <com.wonderkiln.blurkit.BlurLayout
            android:id="@+id/blurLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:clipChildren="true"
                android:orientation="vertical">
    
                <SeekBar
                    android:id="@+id/seek_bar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="61dp"/>
            </LinearLayout>
        </com.wonderkiln.blurkit.BlurLayout>
    </LinearLayout>
    </FrameLayout>
    

    【讨论】:

    • 为什么是 FrameLayout?
    • FrameLayout 主要用于 Frame wise 结构设计。您可以在 Frame wise 中添加视图。当第一帧可见时,您将无法访问第二帧
    • FrameLayout 的第一个孩子将在底部,最后一个孩子将在顶部
    • 它没有用。我将结构更改为 FrameLayout,但我仍然可以滚动
    • 因为TabLayout,可以滚动。您需要为 TabFragment UI 的 FrameLayout 创建另一个 Ui 或仅用于测试我的代码,隐藏 tabLayout 代码,我的代码会很好地工作
    猜你喜欢
    • 2012-01-17
    • 1970-01-01
    • 2015-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-03
    • 1970-01-01
    相关资源
    最近更新 更多