1

I am trying to inflate the custom layout inside the OnBindViewHolder of recyclerview. I could inflate the layout and add the view with for loop successfully to the LinearLayout which is the viewgroup of recyclerview's item, but I cannot set the values to the TextViews of the inflated layout. Here is what I tried so far.Any small hint would be helpful.

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

    holder.title.setText(dataModels.get(position).getTopic());

    listModels = dataModels.get(position).getBreakingAndLatestNewsListModels();

    View.OnClickListener myListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String tag = (String) v.getTag();
            Toast.makeText(context, listModels.get(Integer.parseInt(tag)).getTitle(), Toast.LENGTH_SHORT).show();
        }
    };

    for (int i = 0; i < listModels.size(); i++) {
        View v = inflater.inflate(R.layout.news_content_layout, holder.containerLayout, false);
        v.setTag(i + "");
        holder.containerLayout.addView(v);
        v.setOnClickListener(myListener);

        TextView titleTextView = (TextView) v.findViewById(R.id.news_title_text_view);
        titleTextView.setText("jst testing");

    }


}

The alternate way I tried this too, but again there is another ploblem in expanding and collapsing the toolbar

Ram Mandal
  • 1,899
  • 1
  • 20
  • 35
  • Why do you want to inflate in onBindViewHolder? As per docs it should be done in onCreateViewHolder. Inflating inside onBindViewHolder is very bad design. It will make your recyclerView laggy and clumsy. – Ragesh Ramesh Mar 25 '16 at 04:57
  • since I have more than one items inside the recyclerview's item (i.e. parent recyclerview items has multiple items) so I inflated the layout using loop and added those to the recyclerview's item. – Ram Mandal Mar 25 '16 at 05:01
  • is there any other way to do this ? In My case I need recyclerview inside the recyclerview's item. I tried in this way too. but theres problem in expanding and collapsing the toolbar https://stackoverflow.com/questions/36214230/collapsingtoolbarlayout-no-expanding-when-pulling-recyclerview – Ram Mandal Mar 25 '16 at 05:03

2 Answers2

0

I would read up on Expand a RecyclerView. There is an SO post @ Expandable list with RecyclerView?.

I don't know much about it. The idea is you want to expand/collapse from the top items in RecyclerView. The webpage above uses animation for expanding/collapsing of items.

Community
  • 1
  • 1
The Original Android
  • 6,147
  • 3
  • 26
  • 31
0

If you want a vericlally scroll recycler view inside a recycler view item then it is not possible as i know, Because there will be a scrolling conflict and app going to crash in that case, You can use Horizontally scrolled recycler view inside a vertically scrolled recycler view. I suggest you to go for Linear Layout if you want to achieve the behavior which you are trying to implement.

Naveen Shriyan
  • 1,266
  • 1
  • 9
  • 15