ListView的局部刷新

1.前言

Android开发中我们经常会用到listview的数据和界面刷新动作,我们每次可能会用到的都是Adapter.notifyDataSetChanged()方法。这个方法的原理是利用观察者模式对我们的数据源进行监听,当我们的数据源发生变化的时候,会调用Adapter的getView()方法进行整个界面的刷新。这样的话我们发现,getview()会调用多次,刷新了好多个不需要刷新的item,这样的话相对而言,降低了效率。但是,我们有的情况下是只需要对某个item的数据进行刷新就可以了。这样的话,当数据很多的时候,会提高效率。

先看效果图 
这里写图片描述

2.局部刷新方法一(谷歌推荐方法)

//设置adapter容器
myAdapter = new MyAdapter();
lv.setAdapter(myAdapter);


//设置条目点击
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            updateSingleRow(lv,i);
//          updatePartRow(lv);
      }
});

    /**
     * 修改指定条目的数据
     * @param listView
     * @param position  制定的位置
     */
    private void updateSingleRow(ListView listView,int position){
        int start = listView.getFirstVisiblePosition();
        int end = listView.getLastVisiblePosition();
        Log.i(TAG, "updateSingleRow: start : "+start+" , end : "+end+" , position : "+position);
        if(position>=start&&position<=end){
            list.set(position,"hhh");
            View childView = listView.getChildAt(position - start);
            myAdapter.getView(position,childView,listView);
        }
    }
    /**
     * 局部刷新条目
     * @param listView
     */
    private void updatePartRow(ListView listView) {

        if (listView != null) {
            int start = listView.getFirstVisiblePosition();
            int end = listView.getLastVisiblePosition();
            Log.i(TAG, "updateSingleRow: start : "+start+" , end : "+end);
            for(int i=start;i<end+1;i++){

                list.set(i,"hh");
                //多条目的item局部刷新
                View childView = listView.getChildAt(i - start);
                myAdapter.getView(i,childView,listView);
            }
        }
    }

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

3.局部刷新方法二

    /**
     * 修改指定条目的数据
     * @param listView
     * @param position  制定的位置
     */
    private void updateSingleRow(ListView listView,int position){
        int start = listView.getFirstVisiblePosition();
        int end = listView.getLastVisiblePosition();
        Log.i(TAG, "updateSingleRow: start : "+start+" , end : "+end+" , position : "+position);
        if(position>=start&&position<=end){
            list.set(position,"hhh");
            View childView = listView.getChildAt(position - start);
            TextView tv = (TextView) childView.findViewById(R.id.tv);
            tv.setText(list.get(position));


        }
    }

    /**
     * 局部刷新条目
     * @param listView
     */
    private void updatePartRow(ListView listView) {

        if (listView != null) {
            int start = listView.getFirstVisiblePosition();
            int end = listView.getLastVisiblePosition();
            Log.i(TAG, "updateSingleRow: start : "+start+" , end : "+end);
            for(int i=start;i<end+1;i++){

                list.set(i,"hh");
                //多条目的item局部刷新
                View childView = listView.getChildAt(i - start);

                TextView tv= (TextView) view.findViewById(R.id.tv);
                tv.setText(list.get(i));
            }
        }
    }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

下载地址

listview局部刷新,不带设计模式

listview局部刷新,带设计模式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值