3

I am having a RecyclerView with a Custom Adapter which extends RecyclerView.Adapter

I am Creating LinearLayout with Textview at Runtime and inflating it in each row of RecyclerView

For Example, 1st Row of RecyclerView will have 2 or 3 Textview created at runtime, 2nd Row will have 2 or 3 Textviews created at runtime, 3rd Row will have some Textviews...and so on...

Its working almost Prefect if I check my Log... But when I scroll it down, it just places some textview in wrong places, means I get previous Textviews again when I scroll down in wrong places

@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
    //Movie movie = mItems.get(i);
    hm2 = new HashMap<String, ArrayList<PD_Data>>();
    sub_rows2 = new ArrayList<PD_Data>();
    hm2=categories.get(i);
    String key=hm2.keySet().toArray()[0].toString();
    sub_rows2=hm2.get(key);

    Log.i(LOG_TKT,key);

    viewHolder.textview_category.setText(key);

    LayoutInflater inflater;
    View new_sub_row;

    for(int x=0;x<sub_rows2.size();x++){
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        new_sub_row = inflater.inflate(R.layout.recyclerview_pd, null);
        TextView heading2 = (TextView)new_sub_row.findViewById(R.id.heading2);
        heading2.setText(sub_rows2.get(x).sub_heading);
        Log.i(LOG_TKT,sub_rows2.get(x).sub_heading);
        TextView detail2 = (TextView)new_sub_row.findViewById(R.id.detail2);
        detail2.setText(sub_rows2.get(x).value);
        Log.i(LOG_TKT, sub_rows2.get(x).value);
        viewHolder.linearlayout_recyclerview_pd.addView(new_sub_row);
    }
    //viewHolder.imgThumbnail.setImageResource(movie.getThumbnail());
    hm2 = new HashMap<String, ArrayList<PD_Data>>();
    sub_rows2 = new ArrayList<PD_Data>();
}

What am I doing wrong ?

Avtar Singh
  • 261
  • 8
  • 14

2 Answers2

2

It is obvious from your question that you aren't familier with how to use RecyclerViews. You need to read up on the subject. Here is a good start.

Basically, onBindViewHolder() is only responsible for binding your data to your viewHolders which hold your item layouts provided by you through onCreateViewHolder(). The reason for that is that RecyclerView recycles your views so it doesn't have to create new views every time you scroll.

In your case, it appears that you would need to use some technique that will tell the RecyclerView to use different viewHolders for different items. See how you can do it here.

Community
  • 1
  • 1
Ari
  • 3,086
  • 1
  • 19
  • 27
  • Hey @Ari I have gone through the links your provided and answers at that link. I got it now how RecyclerView works and the use of getItemViewType i.e., I can provide different layouts for different rows depending on my logic (i.e., on which row no)...But I am not getting how to do it at Runtime, means I have a layout.xml file for looks of cardsView...but some rows may have 2 cardsView , and some rows may have more than 2 CardsView....so how to do that....please help... – Avtar Singh Oct 13 '15 at 01:48
  • Good job on the reading! You are probably missing just one more thing. You have to create your different ViewHolder types with different layouts. So, in your case, you need one regular layout, and one layout with two CardViews. – Ari Oct 13 '15 at 02:00
  • Hey @Ari You mean If I have to show 3 cardsView in a particular Row of RecyclerView, then I need another layout with 3 cardsView...and so on...If Yes, then this would be very problematic, there can be any no of cardsView in a particular row of Recyclerview depending on an Array I have...and how can I create so many layouts.... – Avtar Singh Oct 13 '15 at 02:05
1

I have given answer for better practice of binding data with ViewHolder in another question. You can check it here RecyclerView causes issue when recycling I hope this will help you. This will solve you problem well. EDIT

Have you solved your problem yet? If not , consider my advice. You know the problem right? suppose you create a ViewHOlder for item 0 and add some text in it. While scrolling suppose this ViewHolder recycled for item number 10, then according to your code it will add new text rows along with some text rows you added for item number 0. You can solve it like

LayoutInflater inflater;
View new_sub_row;

//check here if the linear layout already has previusely added child, if  yes remove them   
if(viewHolder.linearlayout_recyclerview_pd.getChildCount()>0){
    viewHolder.linearlayout_recyclerview_pd.removeAllViews();
}
for(int x=0;x<sub_rows2.size();x++){
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    new_sub_row = inflater.inflate(R.layout.recyclerview_pd, null);
    TextView heading2 = (TextView)new_sub_row.findViewById(R.id.heading2);
    heading2.setText(sub_rows2.get(x).sub_heading);
    Log.i(LOG_TKT,sub_rows2.get(x).sub_heading);
    TextView detail2 = (TextView)new_sub_row.findViewById(R.id.detail2);
    detail2.setText(sub_rows2.get(x).value);
    Log.i(LOG_TKT, sub_rows2.get(x).value);
    viewHolder.linearlayout_recyclerview_pd.addView(new_sub_row);
}
Community
  • 1
  • 1
Mahendra Chhimwal
  • 1,810
  • 5
  • 21
  • 33