Aman Verma
Aman Verma

Reputation: 3325

How to implement Vertical Recyclerview inside vertical recyclerview android?

Suppose i have 2 items in a RecyclerView where each item has a innerrecyclerview which have around 100 items in it both are vertical or anything that does not have any affect on the existing problem.

I have implemented the method available here on other stackoverflow questions or other android Blogs but were not helpful.

I have a question regarding onbindviewholder, the item 1 inner RecyclerView's onbindviewholder gets called for all the elements i.e. 100 at once and they are not recycling or anything as the onbindviewholder is not calling with scrolling which should get for a normal behaving RecyclerView.

How to make the innerrecyclerview recycle as well because all other posts have not talked about this.

CourseRVAdapter or ParentAdapter

public class CourseRVAdapter extends RecyclerView.Adapter<CourseRVAdapter.SimpleViewHolder> {

        private Context mContext;
        private List<data> merchantlist;
        private List<data> merchantlist2;
        private RecyclerView horizontalList;
        Typeface MRR;
        private Typeface MR;


        public class SimpleViewHolder extends RecyclerView.ViewHolder {
            public final TextView title;
            private HorizontalRVAdapter horizontalAdapter;

            public SimpleViewHolder(View view) {
                super(view);
                Context context = itemView.getContext();
                title = (TextView) view.findViewById(R.id.text_1);
                horizontalList = (RecyclerView) itemView.findViewById(R.id.my_recycler_view);
                horizontalList.setLayoutManager(new CustomLinearLayoutManager(mContext, 3));
                horizontalAdapter = new HorizontalRVAdapter(merchantlist, mContext, MR);
                horizontalList.setAdapter(horizontalAdapter);
            }
        }



        CourseRVAdapter(List<data> merchantlist2, Context mContext, Typeface MRR, List<data> merchantlist, Typeface MR) {

            this.merchantlist2 = merchantlist2;
            this.merchantlist = merchantlist;
            this.mContext = mContext;
            this.MRR = MRR;
            this.MR = MR;

        }
        public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            final View view = LayoutInflater.from(mContext).inflate(R.layout.merchant_list_header, parent, false);
            return new SimpleViewHolder(view);
        }

        @Override
        public void onBindViewHolder(SimpleViewHolder holder, final int position) {



        }

        @Override
        public int getItemCount() {

            return merchantlist2.size();
        }

    }

HorizontalRVAdapter or Inneradapter

public class HorizontalRVAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

            List<data> merchantlist;
            FragmentActivity main;
            Context mContext;
            Typeface MRR;

            public HorizontalRVAdapter(List<data> merchantlist, Context mContext, Typeface MRR) {

                this.merchantlist = merchantlist;
                this.mContext = mContext;
                this.MRR = MRR;

            }

            private class ItemViewHolder extends RecyclerView.ViewHolder {

                private TextView merchantname;

                public ItemViewHolder(View itemView) {
                    super(itemView);
                    merchantname = (TextView) itemView.findViewById(R.id.merchant_name);
                }
            }

            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                Context context = parent.getContext();
                View itemView = LayoutInflater.from(context).inflate(R.layout.merchant_list_item, parent, false);
                ItemViewHolder holder = new ItemViewHolder(itemView);
                return holder;
            }

            @Override
            public void onBindViewHolder(RecyclerView.ViewHolder rawHolder, int position) {

                ItemViewHolder holder = (ItemViewHolder) rawHolder;
                data list = merchantlist.get(position);
                holder.merchantname.setText(list.merchantname);
                holder.itemView.setTag(position);

                Log.d("TAG", "onbindviewholder -- > " + position);
            }

            @Override
            public int getItemCount() {
                return merchantlist.size();
            }

        }

I have not added how i am populating the data for both the recyclerview.

For instance parentrecyclerview has 1 or 2 items and innerrecyclerview has 100 items.

EDIT :

so after getting the first comment i am updating what i want to acheive to get better responses.

I have a two textview and a Gridrecyclerview and i want the two textview to scroll when i scroll gridrecyclerview. i came up with the idea of doing this by defining header for those two text view and simple item holder for the griditems.

Then i had to s set Gridlayoutmanager with the span but it also included the first two text and i end up with the two text view in that grid too which was unwanted. The blue one is the one item of the oarentrecyclerview and the icon and text are the item for insiderecyclerview i want them to scroll in cohesion

Upvotes: 5

Views: 6336

Answers (1)

Abbas
Abbas

Reputation: 3331

So the problem was with a VERTICAL RecyclerView inside another VERTICAL RecyclerView. OP's issue was onBindView() was not called while scrolling for nested RecyclerView's items.

The reason was simple: the nested RecyclerView was drawing all its Views because it had infinite available space from its parent RecyclerView.


The problem was solved for the OP by using a GridLayoutManager with span lookup (for details see this SO answer), which removed the need to use Nested RecyclerViews.

Upvotes: 1

Related Questions