本文實例為大家分享了Listview滑動時不加載數(shù)據(jù) 停止時加載數(shù)據(jù)的具體代碼,供大家參考,具體內(nèi)容如下
創(chuàng)新互聯(lián)主營莊河網(wǎng)站建設的網(wǎng)絡公司,主營網(wǎng)站建設方案,成都APP應用開發(fā),莊河h5成都微信小程序搭建,莊河網(wǎng)站營銷推廣歡迎莊河等地區(qū)企業(yè)咨詢
數(shù)據(jù)源配置(Adapter)
package com.zhengsonglan.listview_loading.adapter; 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; import com.nostra13.universalimageloader.core.ImageLoader; import com.zhengsonglan.listview_loading.R; import com.zhengsonglan.listview_loading.entity.UserEnity; import java.util.List; /** * */ public class MyAdapter extends BaseAdapter { Context context; LayoutInflater inflater; Listlists; private boolean scrollState=false; public void setScrollState(boolean scrollState) { this.scrollState = scrollState; } public MyAdapter(Context context, List lists) { this.context=context; this.inflater=LayoutInflater.from(context); this.lists=lists; } @Override public int getCount() { return lists!=null?lists.size():0; } @Override public Object getItem(int position) { return lists.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder; if (convertView == null) { convertView=inflater.inflate(R.layout.main_item,null,true); viewHolder=new ViewHolder(); 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("加載中"); viewHolder.tv_name.setTag(userEnity.getName()); viewHolder.iv_icon.setTag(img_url); viewHolder.iv_icon.setImageResource(R.mipmap.ic_launcher); } return convertView; } static class ViewHolder{ TextView tv_name; ImageView iv_icon; } }
這個Adapter中重要的代碼如下:
//定義當前l(fā)istview是否在滑動狀態(tài) private boolean scrollState=false; public void setScrollState(boolean scrollState) { this.scrollState = scrollState; }
//實體類 UserEnity userEnity=lists.get(position); if (!scrollState){//如果當前不是滑動的狀態(tài),我們填充真數(shù)據(jù) //填充數(shù)據(jù) viewHolder.tv_name.setText(userEnity.getName()); //設置Tag中數(shù)據(jù)為空表示數(shù)據(jù)已填充 viewHolder.tv_name.setTag(null); //加載圖片 ImageLoader.getInstance().displayImage(img_url,viewHolder.iv_icon); //設置tag為1表示已加載過數(shù)據(jù) viewHolder.iv_icon.setTag("1"); }else{//如果當前是滑動的狀態(tài),我們填充假數(shù)據(jù) viewHolder.tv_name.setText("加載中"); //將數(shù)據(jù)name保存在Tag當中 viewHolder.tv_name.setTag(userEnity.getName()); //將數(shù)據(jù)image_url保存在Tag當中 viewHolder.iv_icon.setTag(img_url); //設置默認顯示圖片(最好是本地資源的圖片) viewHolder.iv_icon.setImageResource(R.mipmap.ic_launcher); }
設置監(jiān)聽
ok,我們在看看Activity中的代碼,主要是監(jiān)聽listview的onscrolllistener方法
@Override public void onScrollStateChanged(AbsListView view, int scrollState) { switch (scrollState){ case AbsListView.OnScrollListener.SCROLL_STATE_IDLE://停止?jié)L動 { //設置為停止?jié)L動 myAdapter.setScrollState(false); //當前屏幕中l(wèi)istview的子項的個數(shù) int count = view.getChildCount(); Log.e("MainActivity",count+""); for (int i = 0; i < count; i++) { //獲取到item的name TextView tv_name = (TextView) view.getChildAt(i).findViewById(R.id.main_item_tv_name); //獲取到item的頭像 ImageView iv_show= (ImageView) view.getChildAt(i).findViewById(R.id.main_item_iv_icon); if (tv_name.getTag() != null) { //非null說明需要加載數(shù)據(jù) tv_name.setText(tv_name.getTag().toString());//直接從Tag中取出我們存儲的數(shù)據(jù)name并且賦值 tv_name.setTag(null);//設置為已加載過數(shù)據(jù) } if (!iv_show.getTag().equals("1")){//!="1"說明需要加載數(shù)據(jù) String image_url=iv_show.getTag().toString();//直接從Tag中取出我們存儲的數(shù)據(jù)image——url ImageLoader.getInstance().displayImage(image_url, iv_show);//顯示圖片 iv_show.setTag("1");//設置為已加載過數(shù)據(jù) } } break; } case AbsListView.OnScrollListener.SCROLL_STATE_FLING://滾動做出了拋的動作 { //設置為正在滾動 myAdapter.setScrollState(true); break; } case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL://正在滾動 { //設置為正在滾動 myAdapter.setScrollState(true); break; } } }
最后記得給listview加上滑動的監(jiān)聽
listview.setOnScrollListener(this);
效果
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。