0

I am working with Nested recyclerView, which mean a RecylerView's item also has a RecyclerView in it. I know basic approach using RecyclerView.Adapter but I want to achieve this using ListAdapter. Is it even possible to get this with ListAdapter?

My activity_main.xml looks like this

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView
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">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp"
    android:fitsSystemWindows="true">

    <include
        android:id="@+id/title_layout"
        layout="@layout/item_title_profile"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/parent_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clipToPadding="false"
        android:paddingTop="10dp"
        android:paddingBottom="60dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/title_layout" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>

And this is my item_row_parent.xml.

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
app:cardCornerRadius="10dp"
app:cardElevation="1dp">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/content_title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:includeFontPadding="false"
        android:padding="10dp"
        android:text="House of Stark"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/child_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/content_title" />
       </androidx.constraintlayout.widget.ConstraintLayout>
       </androidx.cardview.widget.CardView>

How to get it done using ListAdapter?

  • Why do you need to use ListAdapter in the first place? – bongo Feb 03 '23 at 09:37
  • @bongo because new feature needs nesting of RecyclerView but in past I used ListAdapter and there are so many codes written, If I change ListAdapter to Recyclerview.Adapter, it will required lot of time and a hectic work too. So is it even possible to achieve this in ListAdapter? – Abhishek Charismatic Feb 03 '23 at 09:40

1 Answers1

0

So I would say that the easiest/fastest and most reliable option would be to create a new RecyclerView.Adapter. But if you can't do that you can try to wrap ListAdapter into RecycerView.Adapter, and proxy/pass all of the requests directly to ListAdapter. This is sample usage to show the idea, to properly handle dataset changes you will need to add some additional data observers etc.

public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
    private ListAdapter mListAdapter;

    public MyAdapter(ListAdapter listAdapter) {
        mListAdapter = listAdapter;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // Inflate the view for a single item in the RecyclerView.
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        // Get the item from the ListAdapter at the specified position.
        Object item = mListAdapter.getItem(position);

        // Bind the item data to the view holder.
        mListAdapter.getView(position, holder.itemView, parent);
    }

    @Override
    public int getItemCount() {
        return mListAdapter.getCount();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
        }

    }
}
bongo
  • 733
  • 4
  • 12