Optimize ListView loading data logic

One, ListView introduction

Listview is a very important component in android development. It adaptively displays specific content according to the length of the data in the form of a list. Users can freely define the layout of each column of the listview, but when the listview has a large amount of data to be loaded, It will take up a lot of memory and affect performance. At this time, you need to fill and reuse the view as needed to reduce the creation of objects.
It displays the data content in the form of a list, and can adapt to the screen display according to the height of the list

Second, the form of expression

Insert picture description here

This is the simplest form of ListView. The black box is the ListView control, which consists of one item (red box content), and then you can swipe down to view many items.

Three, working principle

ListView is only used as a container (list) for loading display data (that is, the contents of the red boxes above, also called items). The specific data in the item is provided by the adapter.
When the data needs to be displayed, the ListView will fetch the data from the Adapter and then load the data.
Insert picture description here

Four, commonly used data adapter (Adapter)

1.BaseAdapter

BaseAdapter is the basic adapter. In fact, it is actually an abstract class, which usually inherits BaseAdapter when customizing the adapter. This class has four abstract methods. According to these abstract methods, the ListView control is used for data adaptation.

Four abstract methods of BaseAdapter:

Method name Function description
public int getCount() Get the total number of Item items
pyblic Object getItem(int position) Get the object of an Item according to position
public long getItemId(int position) Get the id of an Item according to position
public View getView(int position,View convertView,ViewGroup parent) Get the Item view corresponding to the corresponding position, position is the position of the current Item, converView is used to reuse the old view, and parent is used to load the XML layout

2.SimpleAdapter

SimpleAdapter inherits from BaseAdapter and implements and encapsulates the four abstract methods of BaseAdapter. Therefore, when using SimpleAdapter for data adaptation, you only need to pass in the corresponding parameters in the construction method. The specific information of the SimpleAdapter construction method is as follows:

public SimpleAdapter(Context context,List<? extends Map<String,?>> data,int resourse,String[] from,int[] to)

The meaning of the 5 parameters in the SimpleAdapter() construction method:

  • context: represents the context object
  • data: data collection, each item in data corresponds to the data of the entry in the ListView control
  • resourse: Resource id of Item layout
  • from: the key value in the Map collection
  • to: the corresponding control in the Item layout

3.ArrayAdapter

ArrayAdapter is also a subclass of BaseAdapter. Its usage is similar to SimpleAdapter. Developers only need to pass in the corresponding parameters in the constructor. ArrayAdapter is usually used to adapt the TextView control, such as the Setting (setting menu) in the Android system. ArrayAdapter has multiple construction methods, the specific information of the ArrayAdapter construction method is as follows:

public ArrayAdapter(Context context,int resourse);
public ArrayAdapter(Context context,int resourse,int textViewResourceId);
public ArrayAdapter(Context context,int resourse,T[] objects);
public ArrayAdapter(Context context,int resourse,int textViewResourceId,T[] objects);
public ArrayAdapter(Context context,int resourse,List<T> objects);
public ArrayAdapter(Context context,int resourse,int textViewResourceId,List<T> objects);

The meaning of the 5 parameters in the ArrayAdapter() construction method:

  • context: represents the context object
  • resourse: Resource id of Item layout
  • textViewResourceId: the id of the corresponding TextView in the Item layout
  • T[] objects: data of the array type that needs to be adapted
  • List objects: List type data that needs to be adapted

Five, optimization method

To optimize the loading speed of the listview, it is necessary to make the convertView match the list type, and reuse the convertView to the greatest extent.

There are generally three ways to load getview:

1. Redefine a View loading layout every time, and then load the data (the slowest loading method)

public View getView(int position, View convertView, ViewGroup parent) {
    
    
 View item = mInflater.inflate(R.layout.list_item_icon_text, null);

 ((TextView) item.findViewById(R.id.text)).setText(DATA[position]);

 ((ImageView) item.findViewById(R.id.icon)).setImageBitmap(

 (position & 1) == 1 ? mIcon1 : mIcon2);

 return item;

}

2. Reuse directly when convertView is not empty

Thereby reducing a lot of unnecessary View creation, and then loading data

public View getView(int position, View convertView, ViewGroup parent) {
    
    
 if (convertView == null) {
    
    
 convertView = mInflater.inflate(R.layout.item, parent, false);

 }

 ((TextView) convertView.findViewById(R.id.text)).setText(DATA[position]);

 ((ImageView) convertView.findViewById(R.id.icon)).setImageBitmap(

 (position & 1) == 1 ? mIcon1 : mIcon2);

 return convertView;

 }

3. Define a ViewHolder

Set the tag of ConvetView to ViewHolder, and re-use it when it is not empty (the fastest way)

static class ViewHolder {
    
    
TextView text;

ImageView icon;

}

 

public View getView(int position, View convertView, ViewGroup parent) {
    
    
 ViewHolder holder;

 

 if (convertView == null) {
    
    
 convertView = mInflater.inflate(R.layout.list_item_icon_text,

 parent, false);

 holder = new ViewHolder();

 holder.text = (TextView) convertView.findViewById(R.id.text);

 holder.icon = (ImageView) convertView.findViewById(R.id.icon);

 convertView.setTag(holder);

} else {
    
    
holder = (ViewHolder) convertView.getTag();

}

holder.text.setText(DATA[position]);

holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2);

return convertView;

}

Guess you like

Origin blog.csdn.net/weixin_46311652/article/details/114955284