RecyclerView height wrap_content在NestedScrollView中不起作用

问题描述 投票:2回答:2

因此,我有使用height wrap_content的recyclerView,因为我理解这意味着每当我向其添加项目或删除时,recyclerView都应该展开和折叠,但由于某种原因,在NestedScrollView中,当我向其添加新对象时,它不会表现得像同样大小的任何想法可能是错的?尝试添加

机器人:fillViewport = “真”

但据我所知,没有任何反应可能是因为当孩子们改变时,RecyclerView没有得到更新,因为当我重新启动app时它会折叠或扩展到合适的大小。

这是我的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
2个回答
1
投票

您好kosas为回收者视图提供wrap _content,而设置布局管理器则遵循此

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

0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.