[Android development experience] What is the modification of ViewHolder? Static? Final? Static final ?, Androidviewholder

Source: Internet
Author: User

[Android development experience] What is the modification of ViewHolder? Static? Final? Static final ?, Androidviewholder

Reprinted please indicate the source: http://blog.csdn.net/zhaokaiqiang1992

Now we all know that ViewHolder is used to optimize the listview. But what does ViewHolder use to modify it? What is the significance of such modification? How many VIewHolder instances exist in a ListView? Why does VIewHolder reduce the number of findview and optimize the efficiency? To solve this problem, I did the following tests. The test code is very simple.

public class MainActivity extends Activity {public static int itemId = 1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);ListView listView = new ListView(this);listView.setAdapter(new MyAdapter());setContentView(listView);}private class MyAdapter extends BaseAdapter {@Overridepublic int getCount() {return 50;}@Overridepublic Object getItem(int position) {return position;}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ViewHolder viewHolder = null;if (convertView == null) {convertView = getLayoutInflater().inflate(R.layout.item,parent, false);viewHolder = new ViewHolder();viewHolder.tv = (TextView) convertView.findViewById(R.id.tv);convertView.setTag(viewHolder);} else {viewHolder = (ViewHolder) convertView.getTag();}viewHolder.tv.setText("item" + position);Log.d("TAG", "position=" + position + "---" + viewHolder.toString());return convertView;}}private  class ViewHolder {//private static class ViewHolder {//private final class ViewHolder {//private static final class ViewHolder {private int id;public ViewHolder() {id = itemId;Log.d("TAG", "ViewHolder" + id);itemId++;}TextView tv;@Overridepublic String toString() {return "--------id=" + id;}}}

I will briefly explain what the code means. ViewHolder has a member variable id, which is used in toString () to distinguish different ViewHolder. In the constructor, the id is assigned a value, itemId is a static variable. Each initialization time is + 1. We can calculate the number of ViewHolder instantiation times based on the number of constructor prints, according to toString () you can determine which ViewHolder is used. The Writing Method in getVIew is fixed. The test result is as follows:


Private class ViewHolder modifier, the number of items visible in the interface is 9, ViewHolder is initialized 10 times, and then ViewHolder is reused.


Private static class ViewHolder modifier, the number of items visible in the interface is 9, ViewHolder is initialized 10 times, and then ViewHolder is reused.


Private final class ViewHolder modifier, the number of items visible in the interface is 9, ViewHolder is initialized 10 times, and then ViewHolder is reused.


Private static final class ViewHolder modifier, exactly the same!



As a matter of fact, it is okay to paste a picture as soon as I come up, because these items are exactly the same. No matter what modifier is used, ViewHolder will initialize the number of items visible on the current interface more than once, it is the same as the number of convertView instantiation times. Therefore, no matter what modifier I use, it is okay and unnecessary.

The static modifier class is a static internal class. It does not mean that only one instance exists, but static variables of the external class can be accessed. The final modifier class does not allow the class to inherit, there is no reason to use final here. Therefore, when you write ViewHolder in the future, you don't need to worry about adding anything. You don't need to add anything!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.