Touch事件不适用于ParentActivity

时间:2017-10-24 10:41:40

标签: android toolbar

我已将appbarLayoutcoordinatorLayout添加到父活动及其子级即片段中。首先,触摸事件隐藏并显示父活动efficiently的工具栏,但当我使用appbarlayout作为子片段时,父母的工具栏在滚动时不会隐藏。

有人可以建议如何activate触摸simultaneously的事件吗?

ParentActivity代码:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:focusable="true"
android:focusableInTouchMode="true"
android:fitsSystemWindows="true"
>
<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#00ffffff"
    >
    <android.support.v7.widget.Toolbar
        android:id="@+id/search_bar_settings_container"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_marginStart="-15dp"
        app:layout_scrollFlags="scroll|enterAlways"
        >
        <EditText
            android:id="@+id/search_bar"
            android:layout_marginTop="5dp"
            android:layout_width="305sp"
            android:layout_height="35dp"
            android:hint=" Search Here..."
            android:imeOptions="actionSearch"
            android:singleLine="true"
            android:paddingLeft="9dp"
            android:paddingStart="9dp"
            android:background="@drawable/society_style"/>
        <ImageView
            android:id="@+id/search_icon"
            android:layout_width="35dp"
            android:layout_height="38dp"
            android:layout_marginTop="2dp"
            android:src="@drawable/search"
                            />
                </android.support.v7.widget.Toolbar>
    <android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#001919"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:tabIndicatorColor="#ffffff" />
</android.support.design.widget.AppBarLayout>




<!-- View pager to swipe views -->
<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

Fragment Class的代码:

<android.support.design.widget.CoordinatorLayout   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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="layout.Society_Show"
   >
<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#00ffffff"
    android:layout_marginStart="-13dp"
    >
    <android.support.v7.widget.Toolbar
        android:id="@+id/society_show_container"
        android:layout_width="match_parent"
        android:layout_height="55dp"
        app:layout_scrollFlags="scroll|enterAlways"
        >
<LinearLayout
    android:id="@+id/selected_society_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    >
 <android.support.v7.widget.RecyclerView
     android:id="@+id/selected_society"
     android:layout_marginTop="2sp"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"

     />

</LinearLayout>
    </android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<shivam.developer.featuredrecyclerview.FeaturedRecyclerView
    android:id="@+id/featured_recycler_view_society"
    android:layout_below="@+id/selected_society_layout"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:layout_marginTop="2sp"
    app:defaultItemHeight="270dp"
    app:featuredItemHeight="400dp"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<ImageView
    android:id="@+id/plus"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_gravity="end|bottom"
    android:src="@drawable/new_add_one"
    android:layout_marginRight="5dp"
    android:layout_marginEnd="5dp"
    android:layout_marginBottom="65dp"
    />

2 个答案:

答案 0 :(得分:0)

您不应在AppBarLayout中定义Fragment,而应仅在Activity中定义:

<android.support.design.widget.CoordinatorLayout
    android:fitsSystemWindows="true"
    android:id="@+id/coordinator_layout"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    tools:context=".YourActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:background="?colorPrimary"
            android:id="@+id/toolbar"
            android:layout_height="?actionBarSize"
            android:layout_width="match_parent"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:theme="@style/ThemeOverlay.AppCompat.Dark" />

    </android.support.design.widget.AppBarLayout>

    <LinearLayout
        android:background="@color/colorBackgroundTint"
        android:id="@+id/pager_container"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:showIn="@layout/activity_schedule">

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_height="match_parent"
            android:layout_width="match_parent" />

    </LinearLayout>

</android.support.design.widget.CoordinatorLayout>

因为,您只需要一个AppBar,因为片段是Activity的一部分。

您的ViewPager将包含Fragment,其布局如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Your content e.g. RecyclerView -->

</RelativeLayout>

Android将捕获您的滚动并隐藏Toolbar

答案 1 :(得分:0)

很高兴您使用appbarLayout以避免将一个回收站视图添加到另一个回收站视图的样板代码。它解决了我一起增加两个回收者意见的紧张情绪。

顺便说一下,我认为您不应该使用appbarLayout进行父活动尝试更改您的设计并仅将appbarLayout添加到您的片段活动中。