I want to make recyclerview inside of the another recyclerview's item.,Please help me,Thankyou.
            Asked
            
        
        
            Active
            
        
            Viewed 2,806 times
        
    0
            
            
        - 
                    2I have never tried this but there has got to be a better way. I feel like that would be incredibly inefficient. – Richard Dapice Nov 02 '19 at 06:40
 - 
                    What is the purpose of this ? – Archu Mohan Nov 02 '19 at 06:44
 - 
                    2Its not an Rocket Science !! : ) You can dynamically add child recyclerview into parent recyclerview but first explain your requirement in details then we can help you to achieve your task..!! – niceumang Nov 02 '19 at 06:46
 - 
                    Possible duplicate of [How to add a recyclerView inside another recyclerView](https://stackoverflow.com/questions/34569217/how-to-add-a-recyclerview-inside-another-recyclerview) – Jakir Hossain Nov 02 '19 at 06:59
 - 
                    I want to display all comments and replies of the post like a facebook,But all comments and replies are in same page not in different activity. – Madhuslin KS Nov 02 '19 at 07:05
 
2 Answers
2
            Try this code
- MainRecyclerView Item
item_post.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <LinearLayout
        android:id="@+id/llItemPost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="@dimen/_5sdp">
        <TextView
            android:id="@+id/txtPostCname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Company Name"
            android:textColor="@color/Blue"
            android:textSize="@dimen/_21ssp"
            android:textStyle="bold" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/_8sdp"
            android:layout_marginLeft="@dimen/_8sdp"
            android:layout_marginTop="@dimen/_8sdp"
            android:text="Post:-"
            android:textColor="@color/Blue"
            android:textSize="@dimen/_17ssp"
            android:textStyle="bold" />
        <android.support.v7.widget.RecyclerView
            android:id="@+id/rvPostSub"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingTop="@dimen/_5sdp"
            android:paddingBottom="@dimen/_10sdp">
        </android.support.v7.widget.RecyclerView>
    </LinearLayout>
</LinearLayout>
- MainRecyclerView Adapter
PostAdapter.class
public class PostAdapter extends RecyclerView.Adapter<PostAdapter.ViewHolder> {
    private static final String TAG = "PostAdapter";
    private PostSubAdapter postSubAdapter;
    private ArrayList<String> arrayList;
    private Context mContext;
    private ArrayList<String> yourSubArrayList;
    public PostAdapter(ArrayList<String> arrayList, ArrayList<String> yourSubArrayList, Context mContext) {
        this.arrayList = arrayList;
        this.yourSubArrayList = yourSubArrayList;
        this.mContext = mContext;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_post, viewGroup, false);
        return new ViewHolder(view);
    }
    @Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, int i) {
        viewHolder.txtPostCname.setText(arrayList.get(i));
        viewHolder.rvPostSub.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        postSubAdapter = new PostSubAdapter(yourSubArrayList, mContext);
        viewHolder.rvPostSub.setAdapter(postSubAdapter);
        postSubAdapter.notifyDataSetChanged();
    }
    @Override
    public int getItemCount() {
        return arrayList.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder {
        RecyclerView rvPostSub;
        TextView txtPostCname;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            txtPostCname = itemView.findViewById(R.id.txtPostCname);
            rvPostSub = itemView.findViewById(R.id.rvPostSub);
        }
    }
    @Override
    public int getItemViewType(int position) {
        return position;
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
}
- SubRecyclerView Item
item_post_sub.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/txtPost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/_12sdp"
        android:layout_marginEnd="@dimen/_12sdp"
        android:text="Post"
        android:textColor="#000000"
        android:textSize="@dimen/_13ssp" />
    <View
        android:visibility="visible"
        android:id="@+id/viewDivider"
        android:layout_width="match_parent"
        android:layout_height="@dimen/_2sdp"
        android:layout_marginLeft="@dimen/_8sdp"
        android:layout_marginTop="@dimen/_5sdp"
        android:layout_marginRight="@dimen/_8sdp"
        android:layout_marginBottom="@dimen/_5sdp"
        android:backgroundTint="@color/Blue"
        android:background="@drawable/social_media_divider" />
</LinearLayout>
- SubRecyclerView Adapter
PostSubAdapter.class
public class PostSubAdapter extends RecyclerView.Adapter<PostSubAdapter.ViewHolder> {
    private ArrayList<String> arrayList;
    Context mContext;
    public PostSubAdapter(ArrayList<String> arrayList, Context mContext) {
        this.arrayList = arrayList;
        this.mContext = mContext;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_post_sub, viewGroup, false);
        return new ViewHolder(view);
    }
    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        String post=arrayList.get(i).replace("\\n","\n");
        viewHolder.txtPost.setText(post);
        if(i==arrayList.size()-1)
        {
            viewHolder.viewDivider.setVisibility(View.GONE);
        }
    }
    @Override
    public int getItemCount() {
        return arrayList.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView txtPost;
        View viewDivider;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            txtPost=itemView.findViewById(R.id.txtPost);
            viewDivider=itemView.findViewById(R.id.viewDivider);
        }
    }
    @Override
    public int getItemViewType(int position) {
        return position;
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
}
I hope this can help You!
Thank You.
        Hardik Talaviya
        
- 1,396
 - 5
 - 18
 
0
            
            
        In the bind view holder of 1st recyclerview use it like:
InnerRecyclerviewAdapter adapter=new InnerRecyclerviewAdapter(context,urlistArray);
holder.recyclerView.setAdapter(adapter);
holder.recyclerView.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(context, 
LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(layoutManager);
        Huzaifa Asif
        
- 658
 - 1
 - 8
 - 24