ListView中实现部分刷新的两种方法
ListView在开发中用到的地方非常多,我们经常是全部刷新来更新数据,如果只需要更新某一条数据,该怎么实现呢?我在项目中使用过以下两种方法:
1.通过点击的位置,获取需要刷新那一列对应的控件,然后在控件上显示新的数据。
2.通过点击的位置,在数据源上移除对应位置的数据,并且重新设置新的数据,然后刷新。
先看一下效果图
假如我们点击第二条数据里面的刷新按钮,变成下面的效果
下面我们看一下这个简单示例的代码:
public class MainActivity extends Activity {
private ListView lvMain;
private TestAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lvMain = (ListView) findViewById(R.id.mListView);
adapter = new TestAdapter(this,getDatas());
adapter.setListView(lvMain);
lvMain.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
//数据源
private List<StudentBean> getDatas(){
List<StudentBean> studentList = new ArrayList<StudentBean>();
for(int i = 0;i < 6; i++){
StudentBean st = new StudentBean();
st.setName("孙悟空" + i);
st.setHobby("打妖怪 " + i);
studentList.add(st);
}
return studentList;
}
}
adapter里面,主要是按钮点击后,两个方法updateItemView(position) & updateItemData(position),一个是更新ItemView,一个是重新设置数据源来实现更新某一条数据。
这里有个地方需要注意下:View view = mListView.getChildAt(pos - mListView.getFirstVisiblePosition());,位置不要计算错。
public class TestAdapter extends BaseAdapter implements View.OnClickListener{
private Context ctx;
private List<StudentBean> studentDatas;
public TestAdapter(Context ctx, List<StudentBean> studentDatas) {
this.ctx = ctx;
this.studentDatas = studentDatas;
}
@Override
public int getCount() {
return studentDatas == null? 0 : studentDatas.size();
}
@Override
public Object getItem(int position) {
return studentDatas.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if(convertView == null){
convertView = LayoutInflater.from(ctx).inflate(R.layout.adapter_main_item, parent, false);
viewHolder = new ViewHolder();
viewHolder.tvItemTitle = (TextView) convertView.findViewById(R.id.tvItemTitle);
viewHolder.tvItemContent = (TextView) convertView.findViewById(R.id.tvItemContent);
viewHolder.btnItemRefresh = (Button) convertView.findViewById(R.id.btnItemRefresh);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.tvItemTitle.setText(studentDatas.get(position).getName());
viewHolder.tvItemContent.setText(studentDatas.get(position).getHobby());
viewHolder.btnItemRefresh.setTag(position);
viewHolder.btnItemRefresh.setOnClickListener(this);
return convertView;
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btnItemRefresh:
int position = (int) v.getTag();
//更新ItemView
updateItemView(position);
//重新设置数据源来实现更新一条数据
// updateItemData(position);
break;
}
}
private void updateItemView(int pos){
if(mListView != null){
View view = mListView.getChildAt(pos - mListView.getFirstVisiblePosition());
TextView tvItemTitle = (TextView) view.findViewById(R.id.tvItemTitle);
TextView tvItemContent = (TextView) view.findViewById(R.id.tvItemContent);
StudentBean stNew = getNewDatas();
tvItemTitle.setText(stNew.getName());
tvItemContent.setText(stNew.getHobby());
}
}
private void updateItemData(int pos){
StudentBean stNew = getNewDatas();
studentDatas.remove(pos);
studentDatas.add(pos, stNew);
notifyDataSetChanged();
}
private ListView mListView;
public void setListView(ListView listView){
mListView = listView;
}
static class ViewHolder{
TextView tvItemTitle;
TextView tvItemContent;
Button btnItemRefresh;
}
private StudentBean getNewDatas(){
StudentBean st = new StudentBean();
st.setName("二狗子");
st.setHobby("打土匪");
return st;
}
}
StudentBean
public class StudentBean {
private String name;
private String hobby;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
}