Android Listview滑动时不加载数据,停下来时加载数据

本文来源:http://blog.csdn.net/yy1300326388/article/details/45153813

思路:1.Activity中对Listview的滑动进行监听;并且将状态传递到适配器中;

        2. 适配器中接收Activity中滑动状态;若是非停止状态那么加载“假数据”,设置tag为对应的数据,若是静止或空闲状态那么就加载真实数据,设置tag为null;

       3.在Activity中,监听滑动状态为空闲,那么获取Listview的儿子的数量,遍历,判断tag从而决定是否填充真实数据。

详细代码如下:

数据源配置(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;

            }

      }

这个Adapter中重要的代码如下:

//定义当前listview是否在滑动状态

           private boolean scrollState=false;

               public void setScrollState(boolean scrollState) {

                   this.scrollState = scrollState;

       }

     //实体类

UserEnity userEnity=lists.get(position);

if (!scrollState){//如果当前不是滑动的状态,我们填充真数据

           //填充数据

           viewHolder.tv_name.setText(userEnity.getName());

           //设置Tag中数据为空表示数据已填充

            viewHolder.tv_name.setTag(null);

              //加载图片

        ImageLoader.getInstance().displayImage(img_url,viewHolder.iv_icon);

         //设置tag为1表示已加载过数据

       viewHolder.iv_icon.setTag(“1”);

          }else{//如果当前是滑动的状态,我们填充假数据

         viewHolder.tv_name.setText(“加载中”);

          //将数据name保存在Tag当中

       viewHolder.tv_name.setTag(userEnity.getName());

           //将数据image_url保存在Tag当中

             viewHolder.iv_icon.setTag(img_url);

             //设置默认显示图片(最好是本地资源的图片)

        viewHolder.iv_icon.setImageResource(R.mipmap.ic_launcher);

      }

      我们在看看Activity中的代码,主要是监听listview的onscrolllistener方法

          @Override

          public void onScrollStateChanged(AbsListView view, int scrollState) {

            switch (scrollState){

              case AbsListView.OnScrollListener.SCROLL_STATE_IDLE://停止滚动

     {

          //设置为停止滚动

           myAdapter.setScrollState(false);

           //当前屏幕中listview的子项的个数

            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说明需要加载数据

          tv_name.setText(tv_name.getTag().toString());//直接从Tag中取出我们存储的数据name并且赋值

          tv_name.setTag(null);//设置为已加载过数据

         }

         if (!iv_show.getTag().equals(“1”)){//!=”1″说明需要加载数据

        String image_url=iv_show.getTag().toString();//直接从Tag中取出我们存储的数据image——url

        ImageLoader.getInstance().displayImage(image_url, iv_show);//显示图片

           iv_show.setTag(“1”);//设置为已加载过数据

      }

    }

   break;

        }

      case AbsListView.OnScrollListener.SCROLL_STATE_FLING://滚动做出了抛的动作

          {

  //设置为正在滚动

  myAdapter.setScrollState(true);

   break;

      }

case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL://正在滚动

      {

         //设置为正在滚动

         myAdapter.setScrollState(true);

           break;

                }

            }

         }

最后记得给listview加上滑动的监听

listview.setOnScrollListener(this);

    原文作者:正阳Android
    原文地址: https://www.jianshu.com/p/257b73266e95
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞