自动显示与隐藏布局的ListView

效果图

这里写图片描述

代码

public class MainActivity extends AppCompatActivity {

    private ListView listView;
    String[] datas = {"A","A","A","A","A","A","A","A","A","A",
            "A","A","A","A","A","A","A","A","A","A","A","A","A",};
    private int scaledTouchSlop;
    private float firstY = 0;
    private Toolbar toolbar;
    private ObjectAnimator animtor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        listView = (ListView) findViewById(R.id.listview);
        //设置空的布局
        initHeadView();//添加headvier要在setAdapter之前
        listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,datas));
        //判断认为是滑动的最小距离
        scaledTouchSlop = ViewConfiguration.get(this).getScaledTouchSlop();
        listView.setOnTouchListener(new View.OnTouchListener() {
            private float currentY;
            private int direction;
            private boolean mShow = true;
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        firstY = event.getY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        currentY = event.getY();
                        if(currentY - firstY >scaledTouchSlop){
                            direction = 0;//向下
                        }else if(firstY - currentY > scaledTouchSlop){
                            direction = 1;//向上
                        }
                        if(direction == 1){
                            if(mShow){
                                toobarAnim(1);
                                mShow = !mShow;
                            }
                        }else if(direction == 0){
                            if(!mShow){
                                toobarAnim(0);
                                mShow = !mShow;
                            }
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                        break;
                }
                return false;//注意此处不能返回true,因为如果返回true,onTouchEvent就无法执行,导致的后果是ListView无法滑动
            }
        });
    }

    /**
     * 设置头布局,注意:这个头布局的高度要和ToolBar的高度一致
     */
    public void initHeadView(){
        View view = new View(this);
        AbsListView.LayoutParams params = new AbsListView.LayoutParams
                (AbsListView.LayoutParams.MATCH_PARENT,
                        (int)getResources().getDimension(R.dimen.abc_action_bar_default_height_material));
        view.setLayoutParams(params);
        listView.addHeaderView(view);
    }

    public void toobarAnim(int direction){
        //开始新的动画之前要先取消以前的动画
        if(animtor!=null&&animtor.isRunning()){
            animtor.cancel();
        }
        if(direction == 0){//手指向下滑动,显示ToolBar
            //toolbar.getTranslationY()获取的是Toolbar距离自己顶部的距离
            animtor = ObjectAnimator.ofFloat(toolbar, "translationY", toolbar.getTranslationY(), 0);
        }else if( direction == 1){
            animtor = ObjectAnimator.ofFloat(toolbar, "translationY", toolbar.getTranslationY(), -toolbar.getHeight());
        }
        animtor.start();
    }
}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Small_Lee/article/details/51536690
想对作者说点什么? 我来说一句

ListView根据条件隐藏显示条目

2016年11月15日 23.54MB 下载

没有更多推荐了,返回首页

不良信息举报

自动显示与隐藏布局的ListView

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭