本文來源:http://blog.csdn.net/yy1300326388/article/details/45153813
思路:1.Activity中對Listview的滑動(dòng)進(jìn)行監(jiān)聽脸侥;并且將狀態(tài)傳遞到適配器中建邓;
??????? 2. 適配器中接收Activity中滑動(dòng)狀態(tài);若是非停止?fàn)顟B(tài)那么加載“假數(shù)據(jù)”睁枕,設(shè)置tag為對應(yīng)的數(shù)據(jù)官边,若是靜止或空閑狀態(tài)那么就加載真實(shí)數(shù)據(jù),設(shè)置tag為null外遇;
?????? 3.在Activity中注簿,監(jiān)聽滑動(dòng)狀態(tài)為空閑,那么獲取Listview的兒子的數(shù)量跳仿,遍歷诡渴,判斷tag從而決定是否填充真實(shí)數(shù)據(jù)。
詳細(xì)代碼如下:
數(shù)據(jù)源配置(Adapter)
public class MyAdapter extends BaseAdapter {
???????? ? Context context;
?????? ? ? LayoutInflater inflater;
?????????? List lists;
????????? private boolean scrollState=false;
????????? public void setScrollState(boolean scrollState) {
??????????????? this.scrollState = scrollState;
???????????????? }
?????? ?? public MyAdapter(Context context, Listlists) {
??????????? ? ? ? ? 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;
??????????? }
????? }
這個(gè)Adapter中重要的代碼如下:
//定義當(dāng)前l(fā)istview是否在滑動(dòng)狀態(tài)
?????????? private boolean scrollState=false;
??????? ? ? ?? public void setScrollState(boolean scrollState)?{
????? ? ? ? ? ? ?? this.scrollState?=?scrollState;
?????? }
???? //實(shí)體類
UserEnity userEnity=lists.get(position);
if (!scrollState){//如果當(dāng)前不是滑動(dòng)的狀態(tài)菲语,我們填充真數(shù)據(jù)
?????????? //填充數(shù)據(jù)
?????????? viewHolder.tv_name.setText(userEnity.getName());
?????????? //設(shè)置Tag中數(shù)據(jù)為空表示數(shù)據(jù)已填充
??????????? viewHolder.tv_name.setTag(null);
????????????? //加載圖片
??????? ImageLoader.getInstance().displayImage(img_url,viewHolder.iv_icon);
???????? //設(shè)置tag為1表示已加載過數(shù)據(jù)
?????? viewHolder.iv_icon.setTag("1");
????????? }else{//如果當(dāng)前是滑動(dòng)的狀態(tài)妄辩,我們填充假數(shù)據(jù)
???????? viewHolder.tv_name.setText("加載中");
????????? //將數(shù)據(jù)name保存在Tag當(dāng)中
?????? viewHolder.tv_name.setTag(userEnity.getName());
?????????? //將數(shù)據(jù)image_url保存在Tag當(dāng)中
???????????? viewHolder.iv_icon.setTag(img_url);
???????????? //設(shè)置默認(rèn)顯示圖片(最好是本地資源的圖片)
??????? viewHolder.iv_icon.setImageResource(R.mipmap.ic_launcher);
????? }
????? 我們在看看Activity中的代碼惑灵,主要是監(jiān)聽listview的onscrolllistener方法
????????? @Override
????????? public void onScrollStateChanged(AbsListView view, int scrollState) {
??????????? switch (scrollState){
????????????? case AbsListView.OnScrollListener.SCROLL_STATE_IDLE://停止?jié)L動(dòng)
???? {
????????? //設(shè)置為停止?jié)L動(dòng)
?????????? myAdapter.setScrollState(false);
?????????? //當(dāng)前屏幕中l(wèi)istview的子項(xiàng)的個(gè)數(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中取出我們存儲(chǔ)的數(shù)據(jù)name并且賦值
????????? tv_name.setTag(null);//設(shè)置為已加載過數(shù)據(jù)
???????? }
???????? if (!iv_show.getTag().equals("1")){//!="1"說明需要加載數(shù)據(jù)
??????? String image_url=iv_show.getTag().toString();//直接從Tag中取出我們存儲(chǔ)的數(shù)據(jù)image——url
??????? ImageLoader.getInstance().displayImage(image_url, iv_show);//顯示圖片
?????????? iv_show.setTag("1");//設(shè)置為已加載過數(shù)據(jù)
????? }
??? }
?? break;
? ? ? ? }
????? case AbsListView.OnScrollListener.SCROLL_STATE_FLING://滾動(dòng)做出了拋的動(dòng)作
? ? ? ? ? {
? //設(shè)置為正在滾動(dòng)
? myAdapter.setScrollState(true);
?? break;
????? }
case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL://正在滾動(dòng)
????? {
???????? //設(shè)置為正在滾動(dòng)
???????? myAdapter.setScrollState(true);
?????????? break;
??????????????? }
??????????? }
???????? }
最后記得給listview加上滑動(dòng)的監(jiān)聽