2

I'm new to android programming, and I've been reading a lot about it lately. One of the features of ListView, if I understood it right, is that it recycle views and just replaces it with new data when an item is off the screen.

And just a few minutes ago, I was reading up about endless scrolling, and RecyclerView has been one of the popular choices to implement such a feature. So I looked up RecyclerView, and in this video, it is mentioned that RecyclerView recycles a view automatically to reuse it for new data (as a way to contrast its difference with ListView).

Did I misunderstand ListView about its recycling mechanism? Or if it does recycle, how do you actually implement (or how do you know you are implementing) it?

christianleroy
  • 1,084
  • 5
  • 25
  • 39
  • no, it doesnt, but it gives you recycled view in `Adapter#getView` method as a second `View convertView` parameter – pskink Jul 14 '16 at 06:53

1 Answers1

3

RecyclerView does recycling automatically. In order to make ListView recycle items you will need to do this modification inside of adapter class.

 @Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder;

    if (convertView == null) {
        //brand new
        convertView = LayoutInflater.from(mContext).inflate(R.layout.days_list_item, null);
        holder = new ViewHolder();

        // below is variables that will be different in your case
        holder.numberOfDays = (TextView) convertView.findViewById(R.id.eventDays);
        holder.sinceOrUntil = (TextView) convertView.findViewById(R.id.eventType);
        holder.eventTitle = (TextView) convertView.findViewById(R.id.eventTitle);
        holder.daysText = (TextView) convertView.findViewById(R.id.DaysText);

        convertView.setTag(holder);
    }
    else {
        //reusing item
        holder = (ViewHolder) convertView.getTag();
    }

    // rest of the code
}

For more details refer to this link.

Community
  • 1
  • 1
Marat
  • 6,142
  • 6
  • 39
  • 67