Bismillaahirrohmaanirrohiim…
Saat menggunakan dua atau lebih recyclerview dalam satu halaman, kemungkinan besar data item dari recyclerview tidak ditampilkan semua jika isinya banyak.
Namun hal ini terjadi pada HP android versi 8 (Oreo) ke atas. Untuk versi Nougat masih aman.
Contoh kodenya seperti berikut
<ScrollView
android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:layout_marginBottom="50dp"
android:fillViewport="true"
android:scrollbars="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f4f4f4"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_satu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f4f4f4"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_dua"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</LinearLayout>
</LinearLayout>
</ScrollView>
Agar bisa berjalan sesuai harapan, dimana semua item di dalam recyclerview bisa tampil, maka
– scrollview harus dirubah menjadi NestedScrollView
– menambahkan setNestedScrollingEnabled(false) di code java recyclerview
Hasilnya menjadi seperti di bawah ini:
<android.support.v4.widget.NestedScrollView
android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:layout_marginBottom="50dp"
android:fillViewport="true"
android:scrollbars="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f4f4f4"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_satu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f4f4f4"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_dua"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</LinearLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
di code javanya:
rv_satu = (RecyclerView) view.findViewById(R.id.rv_satu); GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 3); rv_satu.setLayoutManager(gridLayoutManager); rv_satu.setItemAnimator(new DefaultItemAnimator()); rv_satu.setNestedScrollingEnabled(false); rv_dua = (RecyclerView) view.findViewById(R.id.rv_dua); GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 3); rv_dua.setLayoutManager(gridLayoutManager); rv_dua.setItemAnimator(new DefaultItemAnimator()); rv_dua.setNestedScrollingEnabled(false);
Taraa… sudah keluar semua itemnya.
Semoga bermanfaat.