ListView滑动, 是否显示ToolBar

MainActivity

public class MainActivity extends ActionBarActivity implements AbsListView.OnScrollListener {

    public ArrayList data= new ArrayList<>();
    ListView lv;
    int lastVisibleItem;
    int mTouchSlop;//获取系统认为的滑动的最小距离
    float mFirstY;
    float mCurrentY;
    int direction;
    boolean mShow;
    private Toolbar toolbar;
    Animator mAnimator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化toolBar
        toolbar = (Toolbar) findViewById(R.id.tool);
        if(toolbar != null) {
            setSupportActionBar(toolbar);
        }

        initLayout();
    }

    /**
     * 初始化数据
     */
    private void initLayout() {

        //模拟数据
        for (int i = 0; i < 50; i++) {
            data.add("测试数据"+i);
        }

        //获取系统认为的最低滑动距离
        mTouchSlop = ViewConfiguration.get(this).getScaledTouchSlop();

        //对滑动时间进行判断
        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){
                                toolbarAnimal(1);//隐藏toolBar
                                mShow = !mShow;
                            }
                        }else if(direction == 0){
                            if(!mShow){
                                toolbarAnimal(0);//显示toolBar
                                mShow = !mShow;
                            }
                        }

                        break;
                    case MotionEvent.ACTION_UP:
                        break;
                }
                return false;
            }
        };
        lv = (ListView) findViewById(R.id.lv);
        View header = new View(this);
        header.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 48));
        lv.addHeaderView(header);

        lv.setAdapter(new MyAdapter());
        lv.setOnTouchListener(myTouchListener);
        lv.setOnScrollListener(this);
    }

    /**
     * 隐藏toolbar动画
     * @param flag
     */
    private void toolbarAnimal(int flag) {
        if(mAnimator != null && mAnimator.isRunning()){
             mAnimator.cancel();
        }

        if(flag == 0){ //显示toolBar
            mAnimator = ObjectAnimator.ofFloat(toolbar,"translationY", toolbar.getTranslationY(), 0);
        }else{
            mAnimator = ObjectAnimator.ofFloat(toolbar,"translationY", toolbar.getTranslationY(),-toolbar.getHeight());
        }

        mAnimator.start();
    }

    /**
     * 这里可以无视,是测试滑动状态
     * @param view
     * @param scrollState
     */
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        switch (scrollState){
            case NumberPicker.OnScrollListener.SCROLL_STATE_IDLE:
                Log.e("SCROLL_STATE_IDLE","停止");
                break;
            case NumberPicker.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
                Log.e("SCROLL_STATE_TOUCH_SCROLL","滚动");
                break;
            case NumberPicker.OnScrollListener.SCROLL_STATE_FLING:
                Log.e("SCROLL_STATE_FLING","惯性滑动");
                break;
        }
    }

    /**
     * 这里是判断上滑或者listView滑动的位置
     * @param view
     * @param firstVisibleItem
     * @param visibleItemCount
     * @param totalItemCount
     */
    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        if(firstVisibleItem + visibleItemCount == totalItemCount && totalItemCount > 0){
            Log.e("滑到底部了", "滑到底部了");
        }

        if(firstVisibleItem > lastVisibleItem){
            Log.e("正在上滑","正在上滑");
        
        }else if(firstVisibleItem < lastVisibleItem){
            Log.e("正在下滑","正在下滑");
        }

        if(firstVisibleItem == 0){
            Log.e("滑动到第一项了","滑动到第一项了");
        }

        lastVisibleItem = firstVisibleItem;
    }


    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            convertView = View.inflate(MainActivity.this, R.layout.activity_item,null);
            TextView tv  = (TextView) convertView.findViewById(R.id.tv_content);
            String tvData = data.get(position);
            tv.setText(tvData);
            return convertView;
        }
    }
}

MyListView

public class MyListView extends ListView {
    Context mContxt;
    int mMaxOverDistance = 30;
    public MyListView(Context context) {
        this(context, null);
    }

    public MyListView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContxt = context;
        getmMaxOverDistance();
    }

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

    //实用屏幕的density来计算具体的值,是为了让不同分辨率的弹性距离一致
    public void getmMaxOverDistance(){
        DisplayMetrics metrics = mContxt.getResources().getDisplayMetrics();
        float density = metrics.density;
        mMaxOverDistance = (int)(density*mMaxOverDistance);
    }

    @Override
     public void setOnScrollChangeListener(OnScrollChangeListener l) {
        super.setOnScrollChangeListener(l);
    }
}

activity_main.xml

 



activity_item.xml


你可能感兴趣的:(ListView滑动, 是否显示ToolBar)