Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android RecyclerView.Adapter onCreateViewHolder() working

I am using RecyclerView.Adapter but I am little confused regarding working of its method onCreateViewHolder.

  @Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
    if(viewType==TYPE_ITEM) {

        View mView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.inflate_common_item, viewGroup, false);
        ViewHolder vh = new ViewHolder(mView);
        return vh;

    } else {
        View mView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.inflate_uncommon_item, viewGroup, false);
        ViewHolderFooter vh = new ViewHolderFooter(mView);
        return vh;

    }
}

So incase I have 10 items in my list so for each item this method will be called and every time a new ViewHolder will be created of course it'll one time for each view but now my question is when we were using ListView and BaseAdapter with them we store ViewHolder in tag and use that. We don't create ViewHolder for each item.

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

            if(convertView == null) {
                    convertView = inflater.inflate(R.layout.layout_list_item, null);
                    mViewHolder = new MyViewHolder();
                    convertView.setTag(mViewHolder);
            } else {
                    mViewHolder = (MyViewHolder) convertView.getTag();
            }

            mViewHolder.tvTitle = detail(convertView, R.id.tvTitle, myList.get(position).getTitle());
            mViewHolder.tvDesc  = detail(convertView, R.id.tvDesc,  myList.get(position).getDescription());
            mViewHolder.ivIcon  = detail(convertView, R.id.ivIcon,  myList.get(position).getImgResId());

            return convertView;
    }

So are we not creating extra viewholders object. Please help me understand the pros and cons.

Thanks

like image 954
Ankur Chaudhary Avatar asked Jun 03 '15 09:06

Ankur Chaudhary


People also ask

What is onCreateViewHolder () trying to achieve?

The job of onCreateViewHolder() is to create instances of a ViewHolder , including working with the ViewHolder to set up the widgets. Since our widgets are defined in a layout resource, we will need a LayoutInflater to accomplish this.

What is the use of onCreateViewHolder in Android?

This method calls onCreateViewHolder to create a new ViewHolder and initializes some private fields to be used by RecyclerView. Returns the position of the given ViewHolder in the given Adapter . Returns the total number of items in the data set held by the adapter. Return the stable ID for the item at position .

What is onCreateViewHolder?

onCreateViewHolder only creates a new view holder when there are no existing view holders which the RecyclerView can reuse. So, for instance, if your RecyclerView can display 5 items at a time, it will create 5-6 ViewHolders , and then automatically reuse them, each time calling onBindViewHolder .

How many times onCreateViewHolder is called?

By default it have 5.

How does oncreateviewholder work with recyclerview?

Show activity on this post. onCreateViewHolder only creates a new view holder when there are no existing view holders which the RecyclerView can reuse. So, for instance, if your RecyclerView can display 5 items at a time, it will create 5-6 ViewHolders, and then automatically reuse them, each time calling onBindViewHolder.

What is recyclerview in Android?

Android Mobile Development Apps/Applications Recycler view is a more advanced version of listview and works based on View holder design pattern. Using recyclerview we can show grids as well as a list of items. This example demonstrates how to integrate RecyclerView by creating a beautiful student records app that displays student name with age.

What is the use of recyclerview adapter?

RecyclerView.Adapter base class for presenting List data in a RecyclerView, including computing diffs between Lists on a background thread. Adapters provide a binding from an app-specific data set to views that are displayed within a RecyclerView.

How to add recycler View Library in Android Studio?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Open build.gradle and add Recycler view library dependency.


1 Answers

onCreateViewHolder only creates a new view holder when there are no existing view holders which the RecyclerView can reuse. So, for instance, if your RecyclerView can display 5 items at a time, it will create 5-6 ViewHolders, and then automatically reuse them, each time calling onBindViewHolder.

Its similar to what your code in the ListView does (checking if convertView is null, and if not, grabbing the existing ViewHolder from the tag), except, with RecyclerView, this is all done automatically.

I suppose this is one of the pros with using a RecyclerView - you don't need to worry so much about reusing ViewHolders as you do with ListView. The con is, RecyclerView is very customisable, but has very little built in functionality - unlike ListView which is not very customisable, but has a lot of built in functionality.

like image 160
Gil Moshayof Avatar answered Oct 05 '22 00:10

Gil Moshayof