Preface
This blog to share is in the Android development encountered in the sliding data confusion problem how to solve, first describe some why the data confusion, familiar with the reason of the ListView of the friend must know, we in the development in order to optimize the data display, avoid stalling, will use the cache mechanism of the ListView to use our view.
Take a look at the code we often write:
PackageCom.xiaowu.adapter;ImportCOM.XIAOWU.ACTIVITY.R;ImportAndroid.content.Context;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.BaseAdapter;ImportAndroid.widget.ImageView;ImportAndroid.widget.TextView; Public class customadapter extends baseadapter { PrivateContext Mcontext;@Override Public int GetCount() {return 0; }@Override PublicObjectGetItem(intPosition) {returnPosition }@Override Public Long Getitemid(intPosition) {returnPosition }@Override PublicViewGetView(intPosition, View Convertview, ViewGroup parent) {Viewholder Holder =NULL;if(Convertview = =NULL) {holder =NewViewholder (); 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 (); }returnConvertview; }StaticClass Viewholder {PrivateImageView Headicon;PrivateTextView username; }}
The above is the simplest custom adapter code, is simply to reuse the convertview, reuse means to reclaim the already used view, need to display the time to use again . This method is highly efficient and can be displayed smoothly, regardless of the number of data you have.
Here is a picture that can be a good embodiment of the ListView reuse principle:
Each time we draw the view, we call the GetView method, which has three parameters:
Position-the position of the view, starting from 0.
Convertview-Our outermost view, which is our dynamic load-in layout, each entry.
Parent-the parents container of our view.
Here's just a simple introduction to the reuse principle of the ListView, which throws a question:
ListView shows data confusion when sliding, how to solve
The reuse mechanism of the ListView can avoid the lag of the data display, improve the efficiency of the display, but also cause the problem of the ListView data display confusion. Here is a list of some of the problems that can occur:
- Duplicate picture Display
- Picture shows confusion
- Image Display flashes
- Text content shows confusion
This is the problem we often encounter in the actual development, the following to mention some solutions:
Whether the data display duplication or confusion, because of the cache mechanism of the ListView, we are fast sliding, may cause the data duplication because of the cache.
* * Images can be used to solve this problem using asynchronous loading, you can use the Imageloader framework to asynchronously request network pictures.
If it is a local image, you can use the set tag to avoid data confusion * *.
int simple_avatar_id = Integer. ValueOf(Commentdomain. Getcommenticon());Holder. Headimg. Settag(simple_avatar_id);Preset a picture holder. Headimg. Setimageresource(R. drawable. Headphoto_unlogin_small);Use tag to prevent picture misalignment if (holder. Headimg. Gettag()! = NULL && Holder. Headimg. Gettag(). Equals(simple_avatar_id)) {switch (simple_avatar_id) {case0: Holder. Headimg. Setimageresource(R. drawable. Headphoto_unlogin_small); Break;Case1: Holder. Headimg. Setimageresource(R. drawable. IC_head_red); Break;Case2: Holder. Headimg. Setimageresource(R. drawable. IC_head_yellow); Break;Case3: Holder. Headimg. Setimageresource(R. drawable. IC_head_blue); Break;Case4: Holder. Headimg. Setimageresource(R. drawable. IC_head_green); Break;Default Break;} }
is to dynamically retrieve the saved data by setting the identity so that the data is not garbled.
This blog content is not much, is to mention the developers often encounter points.
If need to reprint, annotated: It_xiao Witch
Blog address: Here to write the link content
Android record 21-solutions for data confusion in the ListView