实现的方式参见下面的示例:
public class ParticalRefreshActivity extends ListActivity {
String[] contents = new String[]{"aa","bb","cc","dd","ee","ff","gg","hh","ii","jj","kk","ll","mm","nn","oo","pp","qq","rr","ss","tt","uu","vv","ww","xx","yy","zz"};
ParticalRefreshAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAdapter = new ParticalRefreshAdapter(this,contents);
setListAdapter(mAdapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
mAdapter.refreshFixPosition(position);
}
class ParticalRefreshAdapter extends BaseAdapter{
public String[] contents;
LayoutInflater mInflater;
public ParticalRefreshAdapter(Context context,String[] contents){
this.contents = contents;
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return contents.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
//局部刷新的代码
public void refreshFixPosition(int position){
//刷新局部的item
ViewHolder holder = map.get(position);
String text = holder.textView.getText().toString().trim();
holder.textView.setText(text+"-");
//更改数据源,防止滑动时数据错误
contents[position] = text+"-";
}
public Map map = new HashMap<>();
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null) {
convertView = mInflater.inflate(R.layout.item_partical_refresh, null);
holder = new ViewHolder();
holder.textView = (TextView) convertView.findViewById(R.id.tv_partical_refresh);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(contents[position]);
map.put(position, holder);
return convertView;
}
class ViewHolder{
TextView textView;
}
}
}