Android 關(guān)于listview滾動距離代碼優(yōu)化
//顯示到指定位置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方法中調(diào)用
//優(yōu)化
@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;
?}