Search code examples
androidandroid-activityandroid-recyclerview

How can i create a RecyclerView inside of the another recyclerview's item?


I want to make recyclerview inside of the another recyclerview's item.,Please help me,Thankyou.


Solution

  • 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.