ListView small common sense

1. Generally, after we inherit Adapter, we rewrite getItemId like this

@Override
public long getItemId(int position){
	return position;
}

If the listview only shows no problem, if there is an item to be deleted, it is best to return the ID of the data

@Override
public long getItemId(int position){
	return items.get(position).getId())(;
}

2. Sometimes we find that we have modified the height of the item layout but it does not take effect because of the problem with the way the layout is loaded in the getView method.
Just use this loading method

View view = layoutInflater.inflate(R.layout.text_item,parent,false);

3. Listview has different items and needs to override two methods

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
}

4. Listview multiplexing mechanism
The cache mechanism of RecycleBin of ListView is as shown in the figure below
insert image description here

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;
	}

}

......


5. The ViewHold mechanism reports and saves findviewbyid to optimize time

@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;
	}
}

Six, Listview refresh
1, modify the data source
2, notifyDataSetChanged

7. Partial refresh
If the modified data source does not need to be displayed on several items displayed by the current listview, there is no need to refresh

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 method

in 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);

After adding HeaderView FooterView, you can't use the previous method to get one of the items

Object item = data.get(position);//这样取是错的 还可能造成崩溃 数组越界
Object item = adapter.get(position);//这样取是错的

Object item = parent.getItemAtPosition(position);//需要这样取

9. Cancel asynchronous loading when sliding the listview, such as image loading
onScrollListener

//ListView 停止滑动
public static int SCROLL_STATE_IDLE= 0;
//手指还停留在屏幕上,ListView随着手指的滑动而滑动
public static int SCROLL_STATE_TOUCH_SCROLL =1;
//手指已经离开屏幕,ListView还在随着惯性滑动
public static int SCROLL_STATE_FLING = 2;


public interface OnScrollListener{
	public void onScrollStateChanged(AbsListView view, int ScrollState);
	public void onScroll(AbsListView view, int firstVisibleItem,int totalItemCount)
}

10. Load onScroll in onScrollListener in batches

listView.setOnScrollListener(new AbsListView.OnScrollListener()){
	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount){
		if(firstVisibleItem + visibleItemCount == totalItemCount){//监测滑动到底部了
			Toast.makeText(MainActivity.this,"自动加载新数据",Toast.LENGTH_SHORT).show();
				int count = data.size();
				for (int i = count; i< count +100; i++){//给数据中再添加数据
					data.add(new Message("我是文本消息"+i))
				}
				adapter.notifyDataSetChanged();//更新界面
		}
	}
}

11. The setSelection method of listView

public void setSelection(int position)

insert image description here
Click on the picture to jump to the last viewed position

public void setSelectionFromTop(int position ,int y)//设置item距离顶部还有多少距离,适合当上次滑动到某个位置,下次进入还从该位置查看

12. Common attributes of Listview
Whether the right slider disappears after sliding
android:fadeScrollbars="true"

After sliding, the left slider can be manually dragged
android:fastScrollEnabled="true"

The slider on the right always shows
android:fastScrollAlwayVisible="true"

When overScrollMode is set to always, when the listview slides to the top, it can still be pulled down, but never will not, ifContentScrolls literally means that if the content needs to be scrolled, it can be pulled down
android:overScrollMode="always"

Set the top drop-down background color
android:overScrollHeader="@android:color/light"

Set the bottom background color that can be pulled up at the bottom
android:overScrollFooter="@android:color/light"

Set top and bottom edge gradient effect
android:requiresFadingEdge="vertical"
android:fadingEdgeLength="100dp"
android:cacheColorHint="@android:color/light"
android:scrollingCache="false"

The effect of item click and no click
android:listSelector="@drawable/list_view_selector"

The dividing line between items can also be set as you need
android:divider="@null"
android:dividerHeight="3px"
android:headerDividerEnabled="true"//The first dividing line is not displayed
android:footerDividerEnabled="true"//The last dividing line is not displayed

When the listView starts from the bottom and the number of items is small, the top is empty
android:stackFromBottom="true"

When adding a message, the message at the bottom will always be displayed, which is the same as the new message from WeChat
android:transcriptMode="alwaysScroll"

Guess you like

Origin blog.csdn.net/yanwenyuan0304/article/details/110120146