CoordinatorLayout和RecyclerView的问题

5
我正在使用CoordinatorLayout和NestedScrollView创建一个看起来像这样的活动界面: enter image description here 我的布局结构如下:
<android.support.design.widget.CoordinatorLayout          
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.MovieDetailActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsingToolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/imgToolbarImage"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                android:src="@drawable/backdrop"
                app:layout_collapseMode="parallax" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

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

        <android.support.v4.widget.NestedScrollView
            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"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

                <android.support.v7.widget.CardView
                    ... >
                    <ImageView
                       ... />
                </android.support.v7.widget.CardView>

                <TextView
                        ... />

                <TextView
                        ... />

                <ImageView
                        ... />

                <TextView
                    ... />

                <com.aminiam.moviekade.custom_view.ExpandableTextView
                    ... />

                <android.support.v4.view.ViewPager
                   ... />

                <com.aminiam.moviekade.custom_view.DotIndicator
                    ... />

                <View
                    ... />

                <LinearLayout
                     ... >

                    <TextView
                        ... />

                    <android.support.v7.widget.RecyclerView
                        ... />
                </LinearLayout>

                <android.support.v4.view.ViewPager
                    ... />

                <TextView
                    ... />
            </RelativeLayout>

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

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/activity_horizontal_margin"
        app:layout_anchor="@id/appBar"
        app:layout_anchorGravity="bottom|end"
        app:srcCompat="@drawable/ic_favorite_off" />
</android.support.design.widget.CoordinatorLayout>

当我将RecyclerView添加到布局中时,NestedScrollView会出现在AppBarLayout下面,如下图所示:

enter image description here

我测试了一些修复这个问题的方法,比如将NestedScrollView放入FrameLayout中或给NestedScrollView添加android:layout_gravity="fill_vertical"android:fillViewport="true"属性,但这些方法都无法解决这个问题。

recyclerview 的高度是多少?wrap_content 是什么意思? - user2756345
不,我为此添加了一个固定高度。android:layout_height="150dp" - hosseinAmini
1个回答

6
尝试将 android:descendantFocusability="blocksDescendants" 添加到你的 RelativeLayout 中。
<RelativeLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:descendantFocusability="blocksDescendants" > 

</RelativeLayout>

注意:descendantFocusability 定义了 ViewGroup 在查找要接受焦点的视图时与其子视图之间的关系。 在这里,我们正在阻止它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接