省略ListView性能优化的介绍。。。以下介绍一下局部刷新,主要介绍PullToRefreshListView如何进行局部刷新。
使用ListView进行局部刷新很简单,代码如下:
<span style="font-family:Microsoft YaHei;font-size:14px;">int offset = position - listView.getFirstVisiblePosition();
View convertView = listView.getChildAt(offset);
ViewHolder holder = (ViewHolder) convertView.getTag();
adapterLv.updateView(holder, "局部刷新");
list.set(position, "局部刷新");</span>
<span style="font-family: 'Microsoft YaHei';"><span style="font-size:14px;">listView.getFirstVisiblePosition();</span></span>
<span style="font-family: 'Microsoft YaHei';"><span style="font-size:14px;">listView.getChildAt(offset);</span></span>
使用PullToRefreshListView的时候,直接通过new PullToRefreshListView没有找到 getFirstVisiblePosition()的API;
<span style="font-family: 'Microsoft YaHei';">getChildAt()方法虽然有,但查看源码就知道,PullToRefreshListView只是在PullToRefreshAdapterViewBase这个真正的AbsListView子类上的一层封装,所以需要在</span><span style="font-family: 'Microsoft YaHei';">PullToRefreshAdapterViewBase中添加这两个方法,(局部刷新调用同ListView)如下:</span>
<span style="font-family: 'Microsoft YaHei';">Done! 上班时间写的,太乱没详细说明。</span>
源码:
public class TestActivity extends Activity {
private PullToRefreshListView lv;
private ListView listView;
private List<String> list = new ArrayList<>();
private AdapterLv adapterLv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_activity);
lv = (PullToRefreshListView) findViewById(R.id.lv);
listView = (ListView) findViewById(R.id.listview);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int offset = position - lv.getFirstVisiblePosition();
View convertView = (View) lv.getChidViewAt(offset);
AdapterLv.ViewHolder holder = (ViewHolder) convertView.getTag();
adapterLv.updateView(holder, "局部刷新");
}
});
// listView.setOnItemClickListener(new OnItemClickListener() {
//
// @Override
// public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// int offset = position - listView.getFirstVisiblePosition();
// View convertView = listView.getChildAt(offset);
// AdapterLv.ViewHolder holder = (ViewHolder) convertView.getTag();
// adapterLv.updateView(holder, "局部刷新");
// list.set(position, "局部刷新");
// }
// });
for (int i = 0; i < 30; i++) {
list.add("Text : " + i);
}
adapterLv = new AdapterLv(this, list);
lv.setAdapter(adapterLv);
// listView.setAdapter(adapterLv);
}
}