5

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

Aman Verma
  • 3,155
  • 7
  • 30
  • 60
  • 1
    Why do you need to nested `RecyclerView`s? It is bad practice to do so. Perhaps what you need is something different. If you were to add what you need people here might be able to point you in the right direction. – Abbas Aug 25 '17 at 08:56
  • @Abbas it isnt a bad practice googleplay android app is doing so in their homescreen with horizontalrecyclerview inside verticalrecyclerview. i edited my question – Aman Verma Aug 25 '17 at 09:10
  • Well first off there is no way to know if they have multiple horizontal RecyclerView inside vertical RecyclerView. Could just as easily be a good old ScrollView. Secondly it is a bad idea because nested RecyclerViews can cause memory leak. The change would've been better with a picture. – Abbas Aug 25 '17 at 09:20
  • check the picture and we could check Playstore code to be sure if they have used rcylerview or scrollview and i think they used because google always encouraged to use recyclerview scrollview is just obsolete if talking in terms of memory and performance. – Aman Verma Aug 25 '17 at 09:40
  • I think you don't have to use nested RecyclerView instead GridLayoutManager would be best for you. You can set a span count lookup. Refer to this [SO answer](https://stackoverflow.com/a/31113461/1889768). – Abbas Aug 25 '17 at 09:44
  • it worked amazing... you shud write it as answer and i will accept the answer – Aman Verma Aug 25 '17 at 10:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/152832/discussion-between-aman-verma-and-abbas). – Aman Verma Aug 25 '17 at 10:35

1 Answers1

1

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.

Abbas
  • 3,529
  • 5
  • 36
  • 64
  • did you try by creating inner recylerview with some fixed height? does it still create all cells together or it recycles cells in that case? – Usman Rana Nov 28 '18 at 10:41
  • @UsmanRana if you set the nested `RecyclerView`'s height or width (depending on the parent's orientation) you *can* indeed create nested `RecyclerView`s, however as described in the comments it should not be done since that can cause leaks. And if you do you can only scroll within the defined bounds of the nested `RecyclerView`. The parent `RecyclerView` would be completely useless since it will not scroll at all. – Abbas Nov 28 '18 at 10:50
  • All the cases can be better resolved by using a different `LayoutManager`. If one's not available to match your needs, then you should implement one to suit you. – Abbas Nov 28 '18 at 10:52
  • yes, but if with fixed height it recycles the items even with vertical recyclerview inside vertical recyclerview then it can be useful a little bit. – Usman Rana Nov 28 '18 at 17:45