android ListView向上滑动隐藏标题,下拉显示标题栏
转自http://blog.csdn.net/tuibiansoar/article/details/9668857
首先看看效果图,最下边有Demo的下载链接:
一、当前的Activity 重写dispatchTouchEvent 方法并且实现AnimationListener接口来达到动画的效果,代码如下:
- private boolean mIsTitleHide = false;
- private boolean mIsAnim = false;
- private float lastX = 0;
- private float lastY = 0;
- @Override
- public boolean dispatchTouchEvent(MotionEvent event)
- {
- super.dispatchTouchEvent(event);
- if (mIsAnim) {
- return false;
- }
- final int action = event.getAction();
- float x = event.getX();
- float y = event.getY();
- switch (action) {
- case MotionEvent.ACTION_DOWN:
- lastY = y;
- lastX = x;
- return false;
- case MotionEvent.ACTION_MOVE:
- float dY = Math.abs(y - lastY);
- float dX = Math.abs(x - lastX);
- boolean down = y > lastY ? true : false;
- lastY = y;
- lastX = x;
- if (dX < 8 && dY > 8 && !mIsTitleHide && !down) {
- Animation anim = AnimationUtils.loadAnimation(
- BlogListActivity.this, R.anim.push_top_in);
- // anim.setFillAfter(true);
- anim.setAnimationListener(BlogListActivity.this);
- title_content.startAnimation(anim);
- } else if (dX < 8 && dY > 8 && mIsTitleHide && down) {
- Animation anim = AnimationUtils.loadAnimation(
- BlogListActivity.this, R.anim.push_top_out);
- // anim.setFillAfter(true);
- anim.setAnimationListener(BlogListActivity.this);
- title_content.startAnimation(anim);
- } else {
- return false;
- }
- mIsTitleHide = !mIsTitleHide;
- mIsAnim = true;
- break;
- default:
- return false;
- }
- return false;
- }
二、重写AnimationListener方法
- @Override
- public void onAnimationEnd(Animation animation) {
- // TODO Auto-generated method stub
- if (mIsTitleHide) {
- title.setVisibility(View.GONE);
- } else {
- }
- mIsAnim = false;
- }
- @Override
- public void onAnimationRepeat(Animation animation) {
- // TODO Auto-generated method stub
- }
- @Override
- public void onAnimationStart(Animation animation) {
- // TODO Auto-generated method stub
- title.setVisibility(View.VISIBLE);
- if (mIsTitleHide) {
- FrameLayout.LayoutParams lp = (LayoutParams) mlinear_listview
- .getLayoutParams();
- lp.setMargins(0, 0, 0, 0);
- mlinear_listview.setLayoutParams(lp);
- } else {
- FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) title
- .getLayoutParams();
- lp.setMargins(0, 0, 0, 0);
- title.setLayoutParams(lp);
- FrameLayout.LayoutParams lp1 = (LayoutParams) mlinear_listview
- .getLayoutParams();
- lp1.setMargins(0,
- getResources().getDimensionPixelSize(R.dimen.title_height),
- 0, 0);
- mlinear_listview.setLayoutParams(lp1);
- }
- }
动画 push_top_in.xml
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android" >
- <translate
- android:duration="300"
- android:fromYDelta="0"
- android:toYDelta="-100%"
- android:fillBefore="true" />
- </set>
动画 push_top_out.xml
- <pre name="code" class="java"><?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android" >
- <translate
- android:duration="300"
- android:fillBefore="true"
- android:fromYDelta="-100%"
- android:toYDelta="0" />
- </set>
以上代码就可以实现具体的功能了。