Android:菜单栏Menubar跟随ListView滑动隐藏和显示

使用过Google Play Store应用或者Google+应用的人都知道,其ActionBar能随着ListView的滑动而相应的隐藏或者显示。效果看起来很不错,为此,我笨拙的模仿了一个类似的效果,不知道有没有更好的办法。

先上主布局activity_main:

[html]  view plain copy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context="com.beak.music.ui.MainActivity">  
  6.     <ListView  
  7.         android:id="@+id/main_list_view"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="fill_parent"  
  10.         />  
  11.     <android.support.v7.widget.Toolbar  
  12.         android:id="@+id/main_bar"  
  13.         android:layout_width="fill_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:background="@color/std_color_A"  
  16.         />  
  17. </RelativeLayout>  
注意这里的Toolbar,我们将会用这个Toolbar来替换原来的的actionBar。

现在是MainActivity.java里的代码:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class MainActivity extends BaseActivity{  
  2.   
  3.     private static final String TAG = MainActivity.class.getSimpleName();  
  4.   
  5.     private Toolbar mMainToolbar = null;  
  6.     private ListView mMainListView = null;  
  7.   
  8.     private float mStartY = 0, mLastY = 0, mLastDeltaY;  
  9.   
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.activity_main);  
  14.   
  15.         mMainToolbar = (Toolbar)this.findViewById(R.id.main_bar);  
  16.         this.setSupportActionBar(mMainToolbar);  
  17.   
  18.         mMainListView = (ListView)this.findViewById(R.id.main_list_view);  
  19.         final View header = LayoutInflater.from(this).inflate(R.layout.layout_header, null);  
  20.         mMainListView.addHeaderView(header);  
  21.         mMainListView.setAdapter(new AudioAdapter(this));  
  22.   
  23.   
  24.         mMainListView.setOnTouchListener(new View.OnTouchListener() {  
  25.             @Override  
  26.             public boolean onTouch(View v, MotionEvent event) {  
  27.                 final float y = event.getY();  
  28.                 float translationY = mMainToolbar.getTranslationY();  
  29.                 switch (event.getAction()) {  
  30.                     case MotionEvent.ACTION_DOWN:  
  31. //                        Log.v(TAG, "Down");  
  32.                         mStartY = y;  
  33.                         mLastY = mStartY;  
  34.                         break;  
  35.                     case MotionEvent.ACTION_MOVE:  
  36.                         float mDeltaY = y - mLastY;  
  37.   
  38.                         float newTansY = translationY + mDeltaY;  
  39.                         if (newTansY <= 0 && newTansY >= -mMainToolbar.getHeight()) {  
  40.                             mMainToolbar.setTranslationY(newTansY);  
  41.                         }  
  42.                         mLastY = y;  
  43.                         mLastDeltaY = mDeltaY;  
  44. //                        Log.v(TAG, "Move");  
  45.                         break;  
  46.                     case MotionEvent.ACTION_UP:  
  47.                         ObjectAnimator animator = null;  
  48.                         Log.d(TAG, "mLastDeltaY=" + mLastDeltaY);  
  49.                         if (mLastDeltaY < 0 && mMainListView.getFirstVisiblePosition() > 1) {  
  50.                             Log.v(TAG, "listView.first=" + mMainListView.getFirstVisiblePosition());  
  51.                             animator = ObjectAnimator.ofFloat(mMainToolbar, "translationY", mMainToolbar.getTranslationY(), -mMainToolbar.getHeight());  
  52.                         } else {  
  53.                             animator = ObjectAnimator.ofFloat(mMainToolbar, "translationY", mMainToolbar.getTranslationY(), 0);  
  54.                         }  
  55.                         animator.setDuration(100);  
  56.                         animator.start();  
  57.                         animator.setInterpolator(AnimationUtils.loadInterpolator(MainActivity.this, android.R.interpolator.linear));  
  58. //                        Log.v(TAG, "Up");  
  59.                         break;  
  60.                 }  
  61.                 return false;  
  62.             }  
  63.         });  
  64.     }  
  65. }  
主要的问题是ListView滑动手势检测和Toolbar里的动画。

一开始,先用我们自己的Toolbar替换原来的ActionBar,注意,在你的AppTheme中,windowActionbar这一项要设置为false才能用我们自己的去替换原来的,不然运行会报错,然后给Listview一个与Toolbar等高的headerView。然后再设置Touch事件的监听,

在onTouch方法的ACTION_MOVE分支中,我们计算出本次触发move事件与上次触发move或者down事件时候,我们的触发点的位置变化量-mDeltaY,然后计算出一个相应的translationY,经过与Toolbar高度比较,判断出新的translationY是否合法,合法,则用setTranslationY方法,给Toolbar赋值。

触发UP事件:

当触发了UP事件后,就要,我们就要用一个动画,来过度一下。先判断滑动方向,方向向上,则向上滑动,向下,则向下滑动。

android studio project code 代码在这里,不过是Android studio代码,没有写ADT代码


转载:http://blog.csdn.net/boybeak/article/details/41410113

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值