ListView的局部刷新

来源:互联网 发布:数控铣床编程简单实例 编辑:程序博客网 时间:2024/05/23 18:41

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局部刷新,带设计模式

0 0
原创粉丝点击