The ListView does not load data when it slides, and loads the data when it stops, making the app more optimal

Source: Internet
Author: User

Data source Configuration (Adapter)
 PackageCom.zhengsonglan.listview_loading.adapter;ImportAndroid.content.Context;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.BaseAdapter;ImportAndroid.widget.ImageView;ImportAndroid.widget.TextView;ImportCom.nostra13.universalimageloader.core.ImageLoader;ImportCom.zhengsonglan.listview_loading. RImportcom.zhengsonglan.listview_loading.entity.UserEnity;ImportJava.util.List;/** * */ Public  class myadapter extends baseadapter {Context context;    Layoutinflater Inflater; List<userenity> lists;Private  BooleanScrollstate=false; Public void setscrollstate(BooleanScrollstate) { This. scrollstate = scrollstate; } Public Myadapter(Context context, list<userenity> lists) { This. Context=context; This. Inflater=layoutinflater.from (context); This. lists=lists; }@Override     Public int GetCount() {returnlists!=NULL? Lists.size ():0; }@Override     PublicObjectGetItem(intPosition) {returnLists.get (position); }@Override     Public Long Getitemid(intPosition) {returnPosition }@Override     PublicViewGetView(intPosition, View Convertview, ViewGroup parent) {Viewholder viewholder;if(Convertview = =NULL) {Convertview=inflater.inflate (R.layout.main_item,NULL,true); Viewholder=NewViewholder ();            viewholder.iv_icon= (ImageView) Convertview.findviewbyid (R.id.main_item_iv_icon);            Viewholder.tv_name= (TextView) Convertview.findviewbyid (r.id.main_item_tv_name);        Convertview.settag (Viewholder); }Else{viewholder= (Viewholder) Convertview.gettag ();        } userenity Userenity=lists.get (position); String Img_url=userenity.geticon ();if(!scrollstate)            {ViewHolder.tv_name.setText (Userenity.getname ()); ViewHolder.tv_name.setTag (NULL);            Imageloader.getinstance (). DisplayImage (Img_url,viewholder.iv_icon); ViewHolder.iv_icon.setTag ("1"); }Else{ViewHolder.tv_name.setText ("in Load");            ViewHolder.tv_name.setTag (Userenity.getname ());            ViewHolder.iv_icon.setTag (Img_url);        ViewHolder.iv_icon.setImageResource (R.mipmap.ic_launcher); }returnConvertview; }StaticClass viewholder{TextView tv_name;    ImageView Iv_icon; }}

The important code in this adapter is as follows:

//定义当前listview是否在滑动状态private  boolean scrollState=false;publicvoidsetScrollState(boolean scrollState) {    this.scrollState = scrollState;}
//entity classUserenity Userenity=lists.get (position);if(!scrollstate) {//If the current is not a sliding state, we populate the true data            //Fill dataViewHolder.tv_name.setText (Userenity.getname ());//Set the data in tag to NULL to indicate that the data is populatedViewHolder.tv_name.setTag (NULL);//Load PictureImageloader.getinstance (). DisplayImage (Img_url,viewholder.iv_icon);//Set tag to 1 to indicate that data has been loadedViewHolder.iv_icon.setTag ("1");}Else{//If the state is currently sliding, we fill the dummy dataViewHolder.tv_name.setText ("in Load");//Save the data name in the tagViewHolder.tv_name.setTag (Userenity.getname ());//Save the data image_url in the tagViewHolder.iv_icon.setTag (Img_url);//Set the default display picture (preferably a picture of a local resource)ViewHolder.iv_icon.setImageResource (R.mipmap.ic_launcher);}
Setting up Monitoring

OK, we're looking at the code in the activity, mostly listening to the Onscrolllistener method of the ListView.

@Override     Public void onscrollstatechanged(Abslistview view,intScrollstate) {Switch(scrollstate) { CaseAbsListView.OnScrollListener.SCROLL_STATE_IDLE://Stop scrolling{//Set to stop scrollingMyadapter.setscrollstate (false);The number of children of the ListView in the current screen                intCount = View.getchildcount (); LOG.E ("Mainactivity", count+""); for(inti =0; I < count; i++) {//Get to item's nameTextView tv_name = (TextView) view.getchildat (i). Findviewbyid (R.id.main_item_tv_name);//Get to item's avatarImageView iv_show= (ImageView) View.getchildat (i). Findviewbyid (R.id.main_item_iv_icon);if(Tv_name.gettag ()! =NULL) {//non-null instructions to load dataTv_name.settext (Tv_name.gettag (). toString ());//Remove the data we stored directly from the tag name and assign a valueTv_name.settag (NULL);//set to already loaded data}if(!iv_show.gettag (). Equals ("1")){//!= "1" indicates that data needs to be loadedString Image_url=iv_show.gettag (). toString ();//Remove our stored data directly from the tag Image--urlImageloader.getinstance (). DisplayImage (Image_url, iv_show);//Show PicturesIv_show.settag ("1");//set to already loaded data}                } Break; } CaseAbsListView.OnScrollListener.SCROLL_STATE_FLING://Scroll to make a throw action{//Set to scrollingMyadapter.setscrollstate (true); Break; } CaseAbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL://scrolling{//Set to scrollingMyadapter.setscrollstate (true); Break; }        }    }

Finally remember to add sliding monitoring to the ListView

listview.setOnScrollListener(this);
Effect

The ListView does not load data while it is sliding and loads the data when it stops, making the app more optimal

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.