【问题标题】:Nested Coordinator+AppBar layout's not working嵌套的 Coordinator+AppBar 布局不起作用
【发布时间】:2015-08-11 11:46:01
【问题描述】:

我刚开始使用 CoordinatorLayout 和 AppBar。我有一个布局 activity_home.xml

<RelativeLayout 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.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

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

            <android.support.design.widget.CoordinatorLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <android.support.design.widget.AppBarLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

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

                        <AutoCompleteTextView
                            android:id="@+id/ac_search"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:background="@android:color/transparent"
                            android:focusable="true"
                            android:hint="@string/search"
                            android:imeOptions="actionSearch"
                            android:singleLine="true"
                            android:visibility="gone"/>

                    </android.support.v7.widget.Toolbar>

                    <LinearLayout
                        android:id="@+id/layout_search_results"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:visibility="gone"
                        android:orientation="vertical">
                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_margin="0dp"
                            android:background="@color/white"
                            android:orientation="horizontal"
                            android:paddingStart="@dimen/_6sdp"
                            android:paddingEnd="@dimen/_6sdp">

                            <CheckBox
                                android:id="@+id/cb_search_deals"
                                android:layout_width="wrap_content"
                                android:layout_height="50dp"
                                android:layout_gravity="bottom"
                                android:layout_marginStart="@dimen/_6sdp"
                                android:layout_weight="0.45"
                                android:button="@drawable/checkbox_button"
                                android:checked="true"
                                android:paddingStart="@dimen/_8sdp"
                                android:text="@string/Search_for_Deals"
                                android:textSize="13sp"/>

                            <View
                                android:layout_width="0.25dp"
                                android:layout_height="match_parent"
                                android:layout_weight="0.015"
                                android:background="@color/grey"/>

                            <CheckBox
                                android:id="@+id/cb_search_business"
                                android:layout_width="wrap_content"
                                android:layout_height="50dp"
                                android:layout_marginStart="@dimen/_12sdp"
                                android:layout_weight="0.50"
                                android:button="@drawable/checkbox_button"
                                android:paddingStart="@dimen/_8sdp"
                                android:text="@string/Search_for_Business"
                                android:textSize="13sp"/>
                        </LinearLayout>

                        <RelativeLayout
                            android:id="@+id/rl_search_results"
                            android:layout_width="match_parent"
                            android:layout_height="50dp"
                            android:layout_gravity="start|center_vertical"
                            android:background="@color/red"
                            android:gravity="start|center_vertical">

                            <TextView
                                android:id="@+id/tv_search_results"
                                android:layout_width="fill_parent"
                                android:layout_height="wrap_content"
                                android:layout_alignParentStart="true"
                                android:layout_toStartOf="@+id/iv_filter_results"
                                android:paddingStart="15dp"
                                android:text="Showing 10 Results"
                                android:textAllCaps="false"
                                android:textColor="@color/white"
                                android:textSize="14sp"
                                android:textStyle="normal"/>

                            <ImageView
                                android:id="@+id/iv_filter_results"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_alignParentEnd="true"
                                android:layout_marginEnd="10dp"
                                android:src="@drawable/ic_filter"/>
                        </RelativeLayout>

                        <ListView
                            android:id="@+id/lv_search_results"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_margin="0dp"
                            android:divider="@color/white"
                            android:dividerHeight="0dp"
                            android:scrollbars="none"
                            android:visibility="visible"/>
                    </LinearLayout>

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

                        <android.support.design.widget.TabLayout
                            android:id="@+id/tab_layout"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:paddingStart="@dimen/_12sdp"
                            android:paddingEnd="@dimen/_12sdp"
                            android:scrollbars="none"
                            android:background="@color/red"
                            app:tabTextColor="@color/tab_text_color"
                            app:tabIndicatorColor="@color/white"
                            app:tabMode="scrollable"
                            app:tabSelectedTextColor="@color/white"
                            app:tabTextAppearance="@android:style/TextAppearance.DeviceDefault.Small" />

                        <ImageView
                            android:layout_width="match_parent"
                            android:layout_height="48dp"
                            android:src="@drawable/tabs_overlay" />

                    </FrameLayout>

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

                <FrameLayout
                    android:id="@+id/fragment_container"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:visibility="gone"/>

                <android.support.v4.view.ViewPager
                    android:id="@+id/home_viewpager"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layoutDirection="locale"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

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

        </LinearLayout>

        <include layout="@layout/layout_navigation"/>

        <include layout="@layout/layout_filter"/>

    </android.support.v4.widget.DrawerLayout>

</RelativeLayout>

ViewPagerid=home_viewpager 是处理tablayout 片段。

它只在 Lollipop 上运行良好,因为在代码中我设置了 listview.setNestedScrollEnabed(true) 但它在 4.2.2 上不起作用,而且 viewpager 的第一个片段(带有标题的列表视图)从底部被切断,即完整的屏幕内容未显示.

在我的第二个 viewpager 片段中,布局名称为 fragment_top_deals

<RelativeLayout 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="@color/white"
    android:orientation="vertical">


    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white">


        <android.support.design.widget.AppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent">

    <ImageView
        android:id="@+id/banner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="fitXY" />

            <include
                layout="@layout/layout_filter_header"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/banner" />

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

    <com.app.views.MultiSwipeRefreshLayout
        android:id="@+id/swipeRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="@dimen/_8sdp"
        android:layout_below="@+id/header"
        >

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

            <ListView
                android:id="@+id/list_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scrollbars="none"
                android:divider="@null"
                android:dividerHeight="@dimen/_8sdp"
              app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 

            <TextView
                android:id="@+id/empty_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="center_horizontal"
                android:text="@string/error_no_data"
                android:textSize="24sp"
                android:visibility="gone"/>

        </FrameLayout>

    </com.app.views.MultiSwipeRefreshLayout>

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



    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:visibility="gone"
        android:indeterminateDrawable="@drawable/custom_progress"
        style="?android:attr/progressBarStyle"/>

</RelativeLayout>

问题是 activity_home 协调器布局 + 应用栏效果不起作用,而当前布局的 CoordinatorLayout 和 Appbar 工作正常。此实现也不适用于 4.2.2。

【问题讨论】:

    标签: android


    【解决方案1】:

    如您所见,ListView 仅支持从 Lollipop 开始的嵌套滚动(AppBarLayout 滚动支持的要求)。在此之前,您必须使用实现了NestedScrollingChild 的视图,例如RecyclerViewNestedScrollView

    【讨论】:

    • 谢谢伊恩。是的,我了解ListView 支持从Lollipop 进行嵌套滚动。但是如果你看到我的activity_home 它有一个AppBarLayout 包围Toolbar and Tabsfragment_top_deals 也有AppBarLayout 包围ImageView 和一个布局。当我在 TopDealsFragment 选项卡上时,AppBarLayout 的片段级别效果正在工作,ImageView 在向上滚动时离开屏幕,但Toolbar and Tabs 在向上滚动时不会移出屏幕,这是activity_home 的一部分。请注意,activity_home 的 viewpager 托管了 tab 的所有片段。
    • 我也试过把我的ListView放在NestedScrollView里面,但是有问题吗? NestedScrollView 是打算这样使用还是我误解了!
    • 我的意思是,无论您是否将其包装在 NestedScrollView 中,您都不会从棒棒糖前的 ListView 中获取滚动事件来使用 AppBarLayout 等。最好切换到 RecyclerView,它在任何地方都能更好地工作
    • 我说的是棒棒糖!
    猜你喜欢
    • 1970-01-01
    • 2014-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-17
    • 1970-01-01
    • 1970-01-01
    • 2016-05-26
    相关资源
    最近更新 更多