Android 关于listview滚动距离代码优化
扫描二维码
随时随地手机看文章
//显示到指定位置private
void
setListViewPos(
int
pos)
{
if
(android.os.Build.VERSION.SDK_INT
>=
8
)
{
mListView.smoothScrollToPosition(pos);
}
else
{
mListView.setSelection(pos);
}
}
//获取Listview滚动距离
public int getScrollY() {// 获取滚动距离
View c = listView_right.getChildAt(0);
if (c == null) {
return 0;
}
int firstVisiblePosition = listView_right.getFirstVisiblePosition();
int top = c.getTop();
int headerHeight = 0;
if (firstVisiblePosition >= 1) {
headerHeight = listView_right.getHeight();
}
return -top + firstVisiblePosition * c.getHeight() + headerHeight;
}
OnscrollviewLister中的OnScroll方法中调用
//优化
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.mb, null);
viewHolder = new ViewHolder();
viewHolder.textView = (TextView) convertView.findViewById(R.id.textView);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.textView.setText(list.get(position));
return convertView;
}
static class ViewHolder{
TextView textView;
}