Я использую несколько горизонтальных просмотрщиков в 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>
Все работает нормально, когда мой Фрагмент запускается впервые, но все виды рециркулятора опустеют, если я перейду к другому фрагменту и вернусь к нему. Ниже приведены мои фрагменты 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;
}
Все данные были загружены правильно, но они не отображаются в обзоре утилизатора.
Я ценю любую помощь.
![AFTER NAVIGATION](https://i.stack.imgur.com/DJmAu.jpg)