之前有总结过关于ListView的一些优化技巧,比如它的ConvertView的复用Recycler机制,使用ViewHolder来提高列表条目的findById的效率,以及宽高的设置确定值的好处,如果有图片则在停止滑动的时候再进行加载显示等等。
这次是就ListView具有的一些功能,细节的东西来展开。
1. 设置分割线
android:divider="@color/colorAccent"
android:dividerHeight="1dp"
2. 隐藏滚动条(none | horizonal | vertical)
android:scrollbars="none"
3. 取消Item的点击效果(自定义选择效果)
android:listSelector="@color/transparent"
4. 滑动到第几项Item
mListView.setSelection(15);
mListView.smoothScrollToPosition(position);
5. 动态修改ListView
mAdapter.notifyDataSetChanged();
6. 处理空ListView
mListView.setEmptyView(view);
7. ListView的滑动监听
- OnTouchListener
mListView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()){ case MotionEvent.ACTION_DOWN: //手指按下 break; case MotionEvent.ACTION_MOVE: //滑动 break; case MotionEvent.ACTION_UP: //手指抬起 break; } return false; } });
- OnScrollListener
mListView.setOnScrollListener(new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { switch (scrollState){ case AbsListView.OnScrollListener.SCROLL_STATE_IDLE: //停止滑动 Log.e("ListView:", "SCROLL_STATE_IDLE"); break; case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL: //滑动中 Log.e("ListView:", "SCROLL_STATE_TOUCH_SCROLL"); break; case AbsListView.OnScrollListener.SCROLL_STATE_FLING: //手指拋动时 Log.e("ListView:", "SCROLL_STATE_FLING"); break; } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { //滑动时一直调用 //firstVisibleItem 当前页面第一个item //visibleItemCount 可见的item数 //totalItemCount 总数 //Log.e("ListView: onScroll item", "firstVisibleItem=" + firstVisibleItem + ", visibleItemCount" + visibleItemCount); } });
8. 设置ListView具有弹性
修改maxOverScrollY的想要的值,就可让ListView上下可以弹动了。
@Override protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) { return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, maxOverScrollX, maxOverScrollY, isTouchEvent); }
9. 自动显示和隐藏布局的ListView(比如滑动显示隐藏Toolbar或FloatButton等)
根据触摸监听,知道是上滑还是下滑,之后根据需求,把相关的控件随着ListeView的滑动而进行显示与隐藏;
private static final int TOUCH_SLOP = ViewConfiguration.getTouchSlop() * 2; private int mDownY, mCurrY, direction = 0; private View.OnTouchListener mOnTouchListener = new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()){ case MotionEvent.ACTION_DOWN: mDownY = (int) event.getY(); break; case MotionEvent.ACTION_MOVE: mCurrY = (int) event.getY(); if (Math.abs(mCurrY - mDownY) > TOUCH_SLOP){ if (mCurrY > mDownY){ direction = -1; //下滑 显示 }else if (mCurrY < mDownY){ direction = 1; //上滑 隐藏 } mDownY = mCurrY; } if (direction == -1){ btn.setVisibility(View.VISIBLE); }else if (direction == 1){ btn.setVisibility(View.GONE); } break; case MotionEvent.ACTION_UP: break; } return false; } };
10. getItemViewType
根据位置或者固有的逻辑处理需要显示不同位置的布局类型