ListView小常识
作者:互联网
一、一般我们继承Adapter后 我们都是这么重写getItemId的
@Override
public long getItemId(int position){
return position;
}
如果listview 只是展示没有问题,如果有删除item的话最好f返回该数据的ID
@Override
public long getItemId(int position){
return items.get(position).getId())(;
}
二、有时我们会发现我们修改了item布局的高度却没有生效,是因为getView方法中加载布局的方式问题
使用该加载方式即可
View view = layoutInflater.inflate(R.layout.text_item,parent,false);
三、listview有不同的item需要复写两个方法
int ViewTypeCount = 2;
private interface ViewType{
int TEXT = 0;
int IMAGE = 1;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
if(getItemViewType(position) == ViewType.text){
View view = layoutInflater.inflate(R.layout.text_message_item,parent,false);
...
return view;
}else{
View view = layoutInflater.inflate(R.layout.img_message_item,parent,false);
...
return view;
}
}
@Override
public int getItemViewType(int position){
if(getItem(position) instanceof TextMessage){
return ViewType.TEXT;
}else{
return ViewType.IMAGE;
}
}
@Override
public int getViewTypeCount(){
return ViewTypeCount
}
四、listview 复用机制
ListView的RecycleBin的缓存机制如下图
int ViewTypeCount = 2;
private interface ViewType{
int TEXT = 0;
int IMAGE = 1;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
if(getItemViewType(position) == ViewType.text){
//复用
if(convertView == null){
convertView = layoutInflater.inflate(R.layout.text_message_item,parent,false);
}
...
return convertView;
}else{
if(convertView == null){
convertView = layoutInflater.inflate(R.layout.img_message_item,parent,false);
}
...
return convertView;
}
}
......
五、ViewHold机制 报存findviewbyid,来优化时间
@Override
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder viewHolder;
if(convertView == null){
convertView = layoutInflater.inflate(R.layout.text_message_item,parent,false);
TextView time = convertView.findviewById(R.id.time);
TextView name = convertView.findviewById(R.id.name);
viewHolder = new ViewHolder(time ,name);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder)convertView.getTag()
}
viewHolder.getTime.settext(...)
viewHolder.getName.settext(...)
private static class ViewHolder {
private TextView time;
private TextView name;
public View Holder(Textview time, TextView name){
this.time = time;
this.name = name
}
public TextView getTime(){
return time;
}
public TextView getName(){
return name;
}
}
六、Listview的刷新
1、修改数据源
2、notifyDataSetChanged
七、局部刷新
如果修改的数据源不需要显示在当前listview显示的几个item上就不需要刷新
private void partialRefresh(ListView listview, int position){
if(position >= listView.getFisrtVisiblePosition() && position <= listview.getLastVisiblePosition()){
int childIndex = position - listView.getFirstvisblePosition();
View child = listView.getChildAt(childIndex);
if(child.getTag() instanceof ViewHolder){
((ViewHolder)child.getTag()).refreshContent(datas.get(postion).getContent)}
}
}
ViewHolder.refreshContent方法
baseAdapter中的
private static class ViewHolder {
private TextView time;
private TextView name;
public View Holder(Textview time, TextView name){
this.time = time;
this.name = name
}
public TextView getTime(){
return time;
}
public TextView getName(){
return name;
}
public void refreshContent(String content){
this.name.setText(content)
}
}
八、HeaderView FooterView EmptView
//添加HeaderView
listView.addHeaderView(headerView);
listView.removeHeaderView(headerView);
//添加FooterView
listView.addFooterView(footerView);
listView.removeFooterView(footerView);
//设置EmptyView
listView.setEmptyView(emptyView);
标签:return,name,int,convertView,position,小常识,ListView,public 来源: https://blog.csdn.net/yanwenyuan0304/article/details/110120146