滑动隐藏标题栏布局ListView
2016-02-16 14:23
253 查看
由于手机屏幕有限,有时候列表展示时,为了尽可能多的为用户展示列表数据,当用户向上滑动手指,查看更多数据时,可以把标题栏隐藏,从而为用户展示更多的数据。
向上滑动时,隐藏标题栏
实现起来也比较简单,步骤如下:
为ListView添加监听手势
当用户向上滑动时隐藏标题栏
当用户向下滑动时显示标题栏
总体思路比较简单,为ListView添加一个onTouchListener,判断用户当前触发的手势,进行相应的视图布局的显示和隐藏。关于视图的显示的隐藏有很多种方式,我们这里使用的是属性动画。
这里为了有较好的用户体验使用了一个属性动画,当然你也可以简单的使用Visible属性来控制标题栏的显示和隐藏,需要注意的是,注意计算标题栏显示隐藏时,ListView位置进行相应的改变,demo中同样也使用的属性动画进行相应位置的改变。
源码地址
向上滑动时,隐藏标题栏
实现起来也比较简单,步骤如下:
为ListView添加监听手势
当用户向上滑动时隐藏标题栏
当用户向下滑动时显示标题栏
总体思路比较简单,为ListView添加一个onTouchListener,判断用户当前触发的手势,进行相应的视图布局的显示和隐藏。关于视图的显示的隐藏有很多种方式,我们这里使用的是属性动画。
mListView.setOnTouchListener(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) { // 下滑 显示titleBar showHideTitleBar(true); } else if (mFirstY - mCurrentY > mTouchSlop) { // 上滑 隐藏titleBar showHideTitleBar(false); } break; case MotionEvent.ACTION_UP: break; } return false; } });
private void showHideTitleBar(boolean tag) { if (mAnimatorTitle != null && mAnimatorTitle.isRunning()) { mAnimatorTitle.cancel(); } if (mAnimatorContent != null && mAnimatorContent.isRunning()) { mAnimatorContent.cancel(); } if (tag) { mAnimatorTitle = ObjectAnimator.ofFloat(mTitle, "translationY", mTitle.getTranslationY(), 0); mAnimatorContent = ObjectAnimator.ofFloat(mListView, "translationY", mListView.getTranslationY(), getResources().getDimension(R.dimen.title_height)); } else { mAnimatorTitle = ObjectAnimator.ofFloat(mTitle, "translationY", mTitle.getTranslationY(), -mTitle.getHeight()); mAnimatorContent = ObjectAnimator.ofFloat(mListView, "translationY", mListView.getTranslationY(),0); } mAnimatorTitle.start(); mAnimatorContent.start(); }
这里为了有较好的用户体验使用了一个属性动画,当然你也可以简单的使用Visible属性来控制标题栏的显示和隐藏,需要注意的是,注意计算标题栏显示隐藏时,ListView位置进行相应的改变,demo中同样也使用的属性动画进行相应位置的改变。
源码地址
相关文章推荐
- JAVA wait(), notify(),sleep详解
- java的值传递和引用传递
- 自定义导航栏返回按钮
- CPU寻址方式
- 在Java中如何避免死锁?
- 核心层,汇聚层,接入层,交换机性能差别
- python __del__ http://www.cnblogs.com/coder2012/p/4050084.html
- expression="execution(* cn.**.service.*.*(..))"
- 【.Net】鼠标点击控制鼠标活动范围 ClipCursor
- 【Java】hashcode()和equals()
- 关于clientHeight、offsetHeight、scrollHeight
- 哈理工OJ 1431 摞盘子【水题】
- 从源码角度分析Arrays.asList返回的集合不能进行增删操作。
- NET 中C#的switch语句的语法
- AndroidStudio解决unspecifiedonprojectappresolvestoanAPKarchivewhichisnotsupported
- 阿里openSearch
- leetcode Increasing Triplet Subsequence
- IOS开发证书变成“此证书的签发者无效”解决方法
- Linux上根据进程名kill掉指定的进程
- Js作用域与作用域链