1.设置分割线高度和颜色
android:divider="@android:color/darker_gray" android:dividerHeight="10dp"
2.隐藏滚动条
android:scrollbars="none"
3.取消点击效果(5.x以上是波纹)
android:listSelector="@android:color/transparent"
4.设置listView需要显示在第几页
//N代表第N个item mListView.setSelection(N); //平滑效果 mListView.smoothScrollToPosition(index); mListView.smoothScrollBy(distance,duration); mListView.smoothScrollByOffset(offset);
5.遍历所有item
for(int i=0;i
6.设置空空数据显示提示
mListView.setEmptyView(view);
7.listView滑动监听
①、onTonchListener
mListView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { switch (motionEvent.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 absListView, int state) { switch (state) { case SCROLL_STATE_IDLE: //滑动停止时 break; case SCROLL_STATE_TOUCH_SCROLL: //正在滚动 break; case SCROLL_STATE_FLING: //手指抛动时 即手指用力滑动 //在离开后ListView由于惯性继续滑动 break; } } @Override public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) { //滑动时一直调用 //firstVisibleItem:当前能看到的第一个item的id,从0开始 //visibleItemCount:当前能看到的Item的总数,显示了一小半的item也包括在内了 //整个listView的item总数 if(firstVisibleItem+visibleItemCount==totalItemCount && totalItem-count>0){ //滑动到最后一行 } if(firstVisibleItem>lastVisibleItemPosition){ //上滑 }else if(firstVisibleItem
8.listView获取最后一个item的id和第一个的id
mListView.getLastVisiblePosition(); mListView.getFirstVisiblePosition();
9.具有弹性的listView
只需重写overScrollBy方法即可
@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); }
重写之后:
@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+slideDistanceX, maxOverScrollY+slideDistanceY, isTouchEvent); }
当然计算的时候为了适应多分辨率:
DisplayMetrics metrics = context.getResources().getDisplayMetrics(); float density = metrics.density; slideDistanceX =(int)(density * slideDistanceX); slideDistanceY =(int)(density * slideDistanceY);