android群英传笔记--第四章--ListView使用技巧(一)

设置项目间分割线

android:divider="android:color/darker_gray"
android:dividerHeight="10dp"
android:divider="@null"

隐藏ListView的滚动条

android:scrollbars="none"

取消ListView的Item点击效果

android:listSelector="#00000000"
android:listSelector="@android:color/transparent"

设置ListView需要显示在第几项

listView.setSelection(N);//需要显示的第N个Item
//实现平滑移动
mListView.smoothScrollBy(distance,duration);
mListView.smoothScrollByOffset(offset);
mListView.smoothScrollToPostion(index);

遍历所有条目

for(int i=0;i<mListView.getChildCount;i++){ View view=mListView.getChildAt(i); }

ListView的监听
mListView.setOnTouchListener(){}

mListView.setOnTouchListener(myTouchListener ){}
View.OnTouchListener myTouchListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    mFirstY = event.getY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    mCurrentY = event.getY();
                    if (mCurrentY - mFirstY > mTouchSlop) {
                        direction = 0;// down
                    } else if (mFirstY - mCurrentY > mTouchSlop) {
                        direction = 1;// up
                    }
                    if (direction == 1) {
                        if (mShow) {
                            toolbarAnim(1);//hide
                            mShow = !mShow;
                        }
                    } else if (direction == 0) {
                        if (!mShow) {
                            toolbarAnim(0);//show
                            mShow = !mShow;
                        }
                    }
                    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://滑动停止时
                        break;
                    case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL://正在滚动
                        break;
                    case AbsListView.OnScrollListener.SCROLL_STATE_FLING://惯性滑动
                        break;
                }
            }
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            //滚动时一直调用
           //firstVisibleItem 第一个能看见的Item from 0
           //当前能看见的item总数
           //totalItemCount整个llistview的Item总数

            }
        });
//判断滚动到最后一行
if(firstVisibleItem+visibleItemCount==totalItemCount && totalItemCount>0){
                    //滚动到最后一行
                }
//判断滑动方向
 if(firstVisibleItem>lastVisibleItemPosition){
                   //上滑
               }else if(firstVisibleItem<lastVisibleItemPosition){
                   //下滑
               }
                lastVisibleItemPosition =firstVisibleItem;
//获得可视区域内最后一个item的id
mListView.getLastVisiblePosition();
mListView.getFirstVisiblePosition();//第一个

具有弹性的ListView 仿iOS效果

public class FlexibleListView extends ListView {

    private static int mMaxOverDistance = 50;
    private Context mContext;

    public FlexibleListView(Context context, AttributeSet attrs,
                            int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initView();
    }

    public FlexibleListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        initView();
    }

    public FlexibleListView(Context context) {
        super(context);
        this.mContext = context;
        initView();
    }

    private void initView() {
        DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
        float density = metrics.density;
        mMaxOverDistance = (int) (density * mMaxOverDistance);
    }

    @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, mMaxOverDistance,
                isTouchEvent);
    }
}

你可能感兴趣的:(android,ListView)