1.前言
在Android开发中我们经常会用到listview的数据和界面刷新动作,我们每次可能会用到的都是Adapter.notifyDataSetChanged()方法。这个方法的原理是利用观察者模式对我们的数据源进行监听,当我们的数据源发生变化的时候,会调用Adapter的getView()方法进行整个界面的刷新。这样的话我们发现,getview()会调用多次,刷新了好多个不需要刷新的item,这样的话相对而言,降低了效率。但是,我们有的情况下是只需要对某个item的数据进行刷新就可以了。这样的话,当数据很多的时候,会提高效率。
先看效果图
2.局部刷新方法一(谷歌推荐方法)
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);
}
});
/**
* 修改指定条目的数据
* @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");
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");
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局部刷新,带设计模式