【问题标题】:RecyclerView height wrap_content doesn't work in NestedScrollViewRecyclerView 高度 wrap_content 在 NestedScrollView 中不起作用
【发布时间】:2017-10-05 12:02:49
【问题描述】:

所以我的 recyclerView 高度为 wrap_content,据我了解,这意味着每当我向其中添加或删除项目时 recyclerView 应该展开和折叠,但由于某种原因,在 NestedScrollView 中,当我向其中添加新对象时,它的行为并不像那样,它的大小保持不变,因为任何想法可能有什么问题? 尝试添加

android:fillViewport="true"

但据我了解,没有反应可能是因为 RecyclerView 在其子项更改时没有更新,因为当我重新启动应用程序时,它会折叠或展开到适当的大小。

这是我的 xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/icons"
    android:orientation="vertical">


<LinearLayout
    android:id="@+id/main_holder"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:orientation="vertical">

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


        <android.support.v4.widget.NestedScrollView
            android:id="@+id/nested"
            android:fillViewport="true"
            android:scrollbars="none"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

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

                <RelativeLayout
                    android:id="@+id/empty_group_view"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    android:visibility="gone">

                    <RelativeLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true">

                        <TextView
                            android:id="@+id/empty_text_first"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_centerVertical="true"
                            android:text="@string/no_group_first"
                            android:textColor="@color/secondary_text"
                            android:textSize="17sp" />

                        <ImageView
                            android:id="@+id/add_icon"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_centerVertical="true"
                            android:layout_toEndOf="@+id/empty_text_first"
                            android:src="@drawable/ic_add_group_gry" />

                        <TextView
                            android:id="@+id/empty_text_second"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_centerVertical="true"
                            android:layout_toEndOf="@+id/add_icon"
                            android:text="@string/no_group_second"
                            android:textColor="@color/secondary_text"
                            android:textSize="17sp" />

                    </RelativeLayout>
                </RelativeLayout>

                <TextView
                    android:textSize="14sp"
                    android:layout_marginStart="6dp"
                    android:layout_marginTop="10dp"
                    android:layout_marginBottom="10dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Favorite groups"/>


                <android.support.v7.widget.RecyclerView
                    android:id="@+id/favorite_group_list"
                    android:nestedScrollingEnabled="true"
                    android:layout_width="match_parent"
                    android:layout_height="156dp"
                    android:layout_gravity="center"
                    android:background="@color/icons"
                    android:padding="6dp">
                </android.support.v7.widget.RecyclerView>

                <TextView
                    android:textSize="14sp"
                    android:layout_marginBottom="10dp"
                    android:layout_marginTop="10dp"
                    android:layout_marginStart="6dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Groups"/>

                <android.support.v7.widget.RecyclerView
                    app:layout_behavior="@string/appbar_scrolling_view_behavior"
                    android:id="@+id/group_list"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/background"
                    android:nestedScrollingEnabled="false"
                    android:padding="6dp">

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

            </LinearLayout>

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

</LinearLayout>


<android.support.design.widget.FloatingActionButton
    android:id="@+id/add_new_group"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="16dp"
    android:src="@drawable/ic_playlist_add_48px"
    app:behavior_autoHide="true"
    app:layout_anchor="@id/main_holder"
    app:layout_anchorGravity="bottom|right|end" />

【问题讨论】:

    标签: java android xml android-recyclerview android-nestedscrollview


    【解决方案1】:

    您好 kosas 为回收站视图和设置布局提供 wrap _content 经理关注这个

     linearLayoutManager = new LinearLayoutManager(getActivity()) {
                @Override
                public boolean canScrollVertically() {
                    return false;
                }
            };
    

    【讨论】:

    • 对于第二个 recyclerview,我使用的是 GridLayoutManager,第一个没有问题
    • 如果没有将垂直滚动设置为 false ,滚动将不流畅
    • 好的,但是我的问题不是滚动平滑,而是当recyclerView 增大时nestedScrollView 没有展开
    【解决方案2】:

    问题可能与适配器的布局有关。确保在适配器的布局height 上放置一些固定值或wrap_content。如果是match_parent,它将无法按预期工作,因为每一行都会填满所有RecyclerView

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多