简体   繁体   中英

How does getView determines the position of the recycled convertView in the List

I have been reading articles and questions about recycling a listItem rather than Inflating a new one which is costly. In particular I read this question and I understood the answers, but my question is: When we scroll down a list some listItems from the top disappear and they should be recycled and be shown again in the bottom of the List.

How does android determine the position of them? When we use the viewHolder = (ViewHolder)convertView.getTag; the viewHolder obviously holds the row who wants to be recycled and when we set View Elements by using viewHolder.txtName.setText(dataModel.getName()); I understand that we update the Elements in that particular viewHolder so how does it go to the bottom of the List?

I found this article very useful but there is not enough explanation and comments to make recycling clear for me. Android ListView with Custom Adapter

How does android determine the position of them?

The ListView adapter (ArrayAdapter for example) has ArrayAdapter.getView() and the equivalent adpater for RecyclerViews is RecyclerView.Adapter.onBindViewHolder() . They both have a position parameter in their method signatures:

// ListView
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ...
}

// RecyclerView
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    ...
}

This is the list row position that has been scrolled into view, and needs to be bound to fill in the data.

Furthermore, RecyclerView.ViewHolder objects have the method getAdapterPosition() , so that you can find ViewHolder's list row position outside of the onCreateViewHolder() and onBindViewHolder() methods, for example in a click event handler.

See also:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM