Android record 21-solution to ListView data disorder, androidlistview
Preface
This blog will share with you how to solve the problem of Slide data disorder during Android development. First, I will describe why data disorder occurs. If you are familiar with the reasons for ListView reuse, you must know that, to optimize data display and avoid lagging during development, the ListView cache mechanism is used to reuse our View.
Let's look at a piece of code we often write:
package com.xiaowu.adapter;import com.xiaowu.activity.R;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.TextView;public class CustomAdapter extends BaseAdapter { private Context mContext; @Override public int getCount() { return 0; } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { holder = new ViewHolder(); convertView = LayoutInflater.from(mContext).inflate(R.layout.custom_list_item, null); holder.headIcon = (ImageView) convertView.findViewById(R.id.iv_small_head); holder.username = (TextView) convertView.findViewById(R.id.tv_name); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } return convertView; } static class ViewHolder { private ImageView headIcon; private TextView username; }}
The above is the simplest custom adapter code, that is, the convertView is simply reused.Reclaim used views and reuse them when they need to be displayed.. This method is highly efficient, and can be smoothly displayed no matter how many data entries you have.
The following figure shows the Reuse Principle of ListView:
Every time we draw a view, we call the getView method. It has three parameters:
Position-the position of the view, starting from 0.
ConvertView-Our outermost view, that is, our Dynamically Loaded layout, each entry.
Parent-parent container of our view.
Here is a simple introduction to the Reuse Principle of ListView. A problem is thrown out below:
How to solve the problem of display data disorder during ListView sliding
Although the reuse mechanism of ListView avoids lag for data display and improves the display efficiency, it also causes confusion of ListView data display. The following are some problems:
- Duplicate Image Display
- Picture Display disorder
- Flashing
- Text content display disorder
This is a problem that we often encounter during actual development. Here are some solutions:
Whether the data display is repeated or disordered, it is caused by the ListView cache mechanism. When we slide quickly, data duplication may occur due to caching.
** Images can be asynchronously loaded to solve this problem. You can use the ImageLoader framework to asynchronously request network images.
If the image is a local image, you can use a tag to avoid data confusion **.
Int simple_avatar_id = Integer. valueOf (commentDomain. getCommentIcon (); holder. headImg. setTag (simple_avatar_id); // preset an image holder. headImg. setImageResource (R. drawable. headphoto_unlogin_small); // uses tags to prevent image misplacement if (holder. headImg. getTag ()! = Null & holder. headImg. getTag (). equals (simple_avatar_id) {switch (simple_avatar_id) {case 0: holder. headImg. setImageResource (R. drawable. headphoto_unlogin_small); break; case 1: holder. headImg. setImageResource (R. drawable. ic_head_red); break; case 2: holder. headImg. setImageResource (R. drawable. ic_head_yellow); break; case 3: holder. headImg. setImageResource (R. drawable. ic_head_blue); break; case 4: holder. headImg. setImageResource (R. drawable. ic_head_green); break; default: break ;}}
It is to set the identifier to dynamically obtain the stored data, so that the data will not be disordered.
There is not much content in this blog, that is, a point that developers often encounter.
If you need to reprint it, enter IT_xiao xiaowu
Blog address: Write link content here