NestedScrollView、Horizo​​ntal RecyclerView 和 ViewModel

NestedScrollView, Horizontal RecyclerView and ViewModel

我在 NestedScrollView 中使用多个水平回收视图,并使用 MVVM 模式将数据提交给适配器。下面是我的 xml:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    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"
    tools:context=".MainFragment">

    <androidx.core.widget.NestedScrollView
        android:id="@+id/scrollView2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:orientation="vertical"
        android:scrollbars="vertical">

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

            <Button
                android:id="@+id/home_btn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="hide" />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/home_announcements"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/home_btn"
                android:clipToPadding="false"
                android:overScrollMode="never" />

            <androidx.cardview.widget.CardView
                android:id="@+id/home_departments_cardview"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/home_announcements"
                android:layout_marginTop="50dp"
                app:cardBackgroundColor="@color/cardview_shadow_end_color"
                app:cardCornerRadius="10dp">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="5dp">

                    <TextView
                        android:id="@+id/home_cv_department_tittle"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"

                        android:layout_marginLeft="10dp"
                        android:text="D E P A R T A M E N T S"
                        android:textColor="@color/colorAccent"
                        android:textSize="18sp"
                        android:textStyle="bold" />

                    <View
                        android:id="@+id/home_cv_department_view"
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:layout_below="@+id/home_cv_department_tittle"
                        android:layout_marginLeft="5dp"
                        android:layout_marginTop="2dp"
                        android:layout_marginRight="5dp"
                        android:background="@color/colorAccent" />

                    <androidx.recyclerview.widget.RecyclerView
                        android:id="@+id/home_departments"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/home_cv_department_view" />

                </RelativeLayout>


            </androidx.cardview.widget.CardView>

            <androidx.cardview.widget.CardView
                android:id="@+id/home_wanted_cardview"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/home_departments_cardview"
                android:layout_marginTop="50dp"
                app:cardBackgroundColor="@color/cardview_shadow_end_color"
                app:cardCornerRadius="10dp">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="5dp">

                    <TextView
                        android:id="@+id/home_cv_wanted_tittle"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:text="M O S T  W A N T E D"
                        android:textColor="@color/colorAccent"
                        android:textSize="18sp"
                        android:textStyle="bold" />

                    <View
                        android:id="@+id/home_cv_wanted_view"
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:layout_below="@+id/home_cv_wanted_tittle"
                        android:layout_marginLeft="5dp"
                        android:layout_marginTop="2dp"
                        android:layout_marginRight="5dp"
                        android:background="@color/colorAccent" />

                    <androidx.recyclerview.widget.RecyclerView
                        android:id="@+id/home_mostwanted"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/home_cv_wanted_view" />

                </RelativeLayout>


            </androidx.cardview.widget.CardView>

            <androidx.cardview.widget.CardView
                android:id="@+id/home_chiepest_cardview"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/home_wanted_cardview"
                android:layout_marginTop="50dp"
                app:cardBackgroundColor="@color/cardview_shadow_end_color"
                app:cardCornerRadius="10dp">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="5dp">

                    <TextView
                        android:id="@+id/home_cv_chiepest_tittle"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:text="C H I E P E S T"
                        android:textColor="@color/colorAccent"
                        android:textSize="18sp"
                        android:textStyle="bold" />

                    <View
                        android:id="@+id/home_cv_chiepest_view"
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:layout_below="@+id/home_cv_chiepest_tittle"
                        android:layout_marginLeft="5dp"
                        android:layout_marginTop="2dp"
                        android:layout_marginRight="5dp"
                        android:background="@color/colorAccent" />

                    <androidx.recyclerview.widget.RecyclerView
                        android:id="@+id/home_chiepest"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:paddingBottom="70dp"
                        android:clipToPadding="false"
                        android:layout_below="@+id/home_cv_chiepest_view"/>

                </RelativeLayout>


            </androidx.cardview.widget.CardView>


        </RelativeLayout>


    </androidx.core.widget.NestedScrollView>

</RelativeLayout>

当我的 Fragment 首次启动时一切正常,但如果我导航到另一个 fragment 并返回它,所有的回收视图都会变空。下面是我的片段的 onCreateView:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    view = inflater.inflate(R.layout.fragment_main, container, false);

    button = (Button) view.findViewById(R.id.home_btn);

    firebaseViewModel = ViewModelProviders.of(requireActivity()).get(FirebaseViewModel.class);

    announcementRecyclerView = (RecyclerView) view.findViewById(R.id.home_announcements);
    announcementRecyclerView.setHasFixedSize(true);
    RecyclerView.LayoutManager lm = new LinearLayoutManager(getContext(), RecyclerView.HORIZONTAL,false);
    announcementRecyclerView.setLayoutManager(lm);

    departmentsRecyclerView = (RecyclerView) view.findViewById(R.id.home_departments);
    departmentsRecyclerView.setHasFixedSize(true);
    RecyclerView.LayoutManager linearLayoutManager = new LinearLayoutManager(getContext(), RecyclerView.HORIZONTAL,false);
    departmentsRecyclerView.setLayoutManager(linearLayoutManager);

    mostWantedRecyclerView = (RecyclerView) view.findViewById(R.id.home_mostwanted);
    mostWantedRecyclerView.setHasFixedSize(true);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext(), RecyclerView.HORIZONTAL,false);
    mostWantedRecyclerView.setLayoutManager(layoutManager);

    chiepestRecyclerView = (RecyclerView) view.findViewById(R.id.home_chiepest);
    chiepestRecyclerView.setHasFixedSize(true);
    RecyclerView.LayoutManager llm = new LinearLayoutManager(getContext(), RecyclerView.HORIZONTAL,false);
    chiepestRecyclerView.setLayoutManager(llm);

    announcementsAdapter = new AnnouncementsAdapter(announcementList);
    announcementRecyclerView.setAdapter(announcementsAdapter);

    chiepAdapter = new HomeChiepAdapter();

    departmentsRecyclerView.setAdapter(chiepAdapter);
    mostWantedRecyclerView.setAdapter(chiepAdapter);
    chiepestRecyclerView.setAdapter(chiepAdapter);

    firebaseViewModel.getShowHallChiepest("BR", "AL").observe(getViewLifecycleOwner(), new Observer<List<HomeModel>>() {
        @Override
        public void onChanged(List<HomeModel> homeModels) {
            Log.i(TAG, "onChanged: chiep recyclerview size is: "+homeModels.size());
            chiepAdapter.submitList(homeModels);

        }
    });


    // Inflate the layout for this fragment
    return view;
}

所有数据都已正确下载,但没有显示在recyclerview上。

感谢任何帮助。

我在@UrielUVD 的 Whosebug 的评论部分找到了一个丢失的解决方案。

出于某种原因,在我的例子中,RecyclerView 导航后高度 = 0。

这似乎是 RecyclerViewNestedScrollView 内时发生的问题。

通过向所有 RecyclerView 添加 android:minHeight 解决了问题。

<androidx.recyclerview.widget.RecyclerView
                android:id="@+id/home_announcements"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:minHeight="350dp"
                android:layout_below="@+id/home_btn"
                android:overScrollMode="never"
                tools:listitem="@layout/item_home_announcements"/>