ListView局部更新
原来看到虎牙直播上面最上面一个栏目上面有个刷新按钮,点击后只更新上面一个栏目数据更新,下面都没有更新,个人感觉还是蛮不错的,想到在listview里面怎么实现一部分数据的更新呢,后来研究了下,还真做到了,感觉还是蛮好玩的。记录一下。
public classMainActivityextendsAppCompatActivity{
ListViewlistview;
ArrayListitemList;
@Override
protected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbartoolbar=(Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButtonfab=(FloatingActionButton)findViewById(R.id.fab);
fab.setOnClickListener(newView.OnClickListener() {
@Override
public voidonClick(View view) {
Viewview1=listview.getChildAt(0);
MyAdapter.ViewHolderholder=(MyAdapter.ViewHolder)view1.getTag();
holder.textView.setText(" i am new ao");
}
});
itemList= newArrayList<>();
for(inti=0;i<10;i++) {
itemList.add(" old "+i);
}
listview=(ListView)findViewById(R.id.listview);
//ArrayAdapter adapter=new ArrayAdapter(MainActivity.this,R.layout.item,R.id.tv_item,itemList);
MyAdapteradapter= newMyAdapter(itemList,getApplicationContext());
listview.setAdapter(adapter);
}
}
public classMyAdapterextendsBaseAdapter{
ArrayListitemList;
LayoutInflaterinflater;
publicMyAdapter(ArrayListitemList,Context context){
this.itemList=itemList;
inflater=LayoutInflater.from(context);
}
@Override
public intgetCount() {
returnitemList.size();
}
@Override
publicObjectgetItem(intposition) {
returnitemList.get(position);
}
@Override
public longgetItemId(intposition) {
return0;
}
@Override
publicViewgetView(intposition,View convertView,ViewGroup parent) {
ViewHolderviewHolder;
if(convertView==null){
convertView=inflater.inflate(R.layout.item,null);
viewHolder=newViewHolder();
viewHolder.textView=(TextView)convertView.findViewById(R.id.tv_item);
convertView.setTag(viewHolder);
}else{
viewHolder=(ViewHolder)convertView.getTag();
}
viewHolder.textView.setText((String)getItem(position));
returnconvertView;
}
static classViewHolder{
TextViewtextView;
}
}
布局文件太简单了,就不贴了,学习记录,备忘。