Android Toolbar跟随ListView滑动隐藏和现实
2015-01-19 15:26
393 查看
转载自:http://blog.csdn.net/boybeak/article/details/41410113
使用过Google Play Store应用或者Google+应用的人都知道,其ActionBar能随着ListView的滑动而相应的隐藏或者显示。效果看起来很不错,为此,我笨拙的模仿了一个类似的效果,不知道有没有更好的办法。
先上主布局activity_main:
[html]
view plaincopy
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.beak.music.ui.MainActivity">
<ListView
android:id="@+id/main_list_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<android.support.v7.widget.Toolbar
android:id="@+id/main_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/std_color_A"
/>
</RelativeLayout>
注意这里的Toolbar,我们将会用这个Toolbar来替换原来的的actionBar。
现在是MainActivity.java里的代码:
[java]
view plaincopy
public class MainActivity extends BaseActivity{
private static final String TAG = MainActivity.class.getSimpleName();
private Toolbar mMainToolbar = null;
private ListView mMainListView = null;
private float mStartY = 0, mLastY = 0, mLastDeltaY;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMainToolbar = (Toolbar)this.findViewById(R.id.main_bar);
this.setSupportActionBar(mMainToolbar);
mMainListView = (ListView)this.findViewById(R.id.main_list_view);
final View header = LayoutInflater.from(this).inflate(R.layout.layout_header, null);
mMainListView.addHeaderView(header);
mMainListView.setAdapter(new AudioAdapter(this));
mMainListView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
final float y = event.getY();
float translationY = mMainToolbar.getTranslationY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// Log.v(TAG, "Down");
mStartY = y;
mLastY = mStartY;
break;
case MotionEvent.ACTION_MOVE:
float mDeltaY = y - mLastY;
float newTansY = translationY + mDeltaY;
if (newTansY <= 0 && newTansY >= -mMainToolbar.getHeight()) {
mMainToolbar.setTranslationY(newTansY);
}
mLastY = y;
mLastDeltaY = mDeltaY;
// Log.v(TAG, "Move");
break;
case MotionEvent.ACTION_UP:
ObjectAnimator animator = null;
Log.d(TAG, "mLastDeltaY=" + mLastDeltaY);
if (mLastDeltaY < 0 && mMainListView.getFirstVisiblePosition() > 1) {
Log.v(TAG, "listView.first=" + mMainListView.getFirstVisiblePosition());
animator = ObjectAnimator.ofFloat(mMainToolbar, "translationY", mMainToolbar.getTranslationY(), -mMainToolbar.getHeight());
} else {
animator = ObjectAnimator.ofFloat(mMainToolbar, "translationY", mMainToolbar.getTranslationY(), 0);
}
animator.setDuration(100);
animator.start();
animator.setInterpolator(AnimationUtils.loadInterpolator(MainActivity.this, android.R.interpolator.linear));
// Log.v(TAG, "Up");
break;
}
return false;
}
});
}
}
主要的问题是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事件后,就要,我们就要用一个动画,来过度一下。先判断滑动方向,方向向上,则向上滑动,向下,则向下滑动。
使用过Google Play Store应用或者Google+应用的人都知道,其ActionBar能随着ListView的滑动而相应的隐藏或者显示。效果看起来很不错,为此,我笨拙的模仿了一个类似的效果,不知道有没有更好的办法。
先上主布局activity_main:
[html]
view plaincopy
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.beak.music.ui.MainActivity">
<ListView
android:id="@+id/main_list_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<android.support.v7.widget.Toolbar
android:id="@+id/main_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/std_color_A"
/>
</RelativeLayout>
注意这里的Toolbar,我们将会用这个Toolbar来替换原来的的actionBar。
现在是MainActivity.java里的代码:
[java]
view plaincopy
public class MainActivity extends BaseActivity{
private static final String TAG = MainActivity.class.getSimpleName();
private Toolbar mMainToolbar = null;
private ListView mMainListView = null;
private float mStartY = 0, mLastY = 0, mLastDeltaY;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMainToolbar = (Toolbar)this.findViewById(R.id.main_bar);
this.setSupportActionBar(mMainToolbar);
mMainListView = (ListView)this.findViewById(R.id.main_list_view);
final View header = LayoutInflater.from(this).inflate(R.layout.layout_header, null);
mMainListView.addHeaderView(header);
mMainListView.setAdapter(new AudioAdapter(this));
mMainListView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
final float y = event.getY();
float translationY = mMainToolbar.getTranslationY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// Log.v(TAG, "Down");
mStartY = y;
mLastY = mStartY;
break;
case MotionEvent.ACTION_MOVE:
float mDeltaY = y - mLastY;
float newTansY = translationY + mDeltaY;
if (newTansY <= 0 && newTansY >= -mMainToolbar.getHeight()) {
mMainToolbar.setTranslationY(newTansY);
}
mLastY = y;
mLastDeltaY = mDeltaY;
// Log.v(TAG, "Move");
break;
case MotionEvent.ACTION_UP:
ObjectAnimator animator = null;
Log.d(TAG, "mLastDeltaY=" + mLastDeltaY);
if (mLastDeltaY < 0 && mMainListView.getFirstVisiblePosition() > 1) {
Log.v(TAG, "listView.first=" + mMainListView.getFirstVisiblePosition());
animator = ObjectAnimator.ofFloat(mMainToolbar, "translationY", mMainToolbar.getTranslationY(), -mMainToolbar.getHeight());
} else {
animator = ObjectAnimator.ofFloat(mMainToolbar, "translationY", mMainToolbar.getTranslationY(), 0);
}
animator.setDuration(100);
animator.start();
animator.setInterpolator(AnimationUtils.loadInterpolator(MainActivity.this, android.R.interpolator.linear));
// Log.v(TAG, "Up");
break;
}
return false;
}
});
}
}
主要的问题是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 Toolbar跟随ListView滑动隐藏和现实
- Android -- Toolbar跟随ListView滑动隐藏和显现
- Android:菜单栏Menubar跟随ListView滑动隐藏和显示
- Android list列表滑动显示隐藏toolbar(ListView)
- Android学习笔记-实现toolBar跟随ListView滑动显示与隐藏
- android——自定义滑动隐藏(结合FrameLayout+ScrollView)+ListView滑动隐藏
- Android list列表滑动显示隐藏toolbar(RecyleView)
- android listview headerview 滑动显示与隐藏标题(记录)
- 【Android】(已更新解决方案)更新SDK版本(25)后,自定义FloatingActionButton的Behavior(跟随列表滑动显示隐藏)只隐藏不出现的问题
- AndroidStudio Toolbar 滑动隐藏以及返回按钮点击事件
- android ListView向上滑动隐藏标题,下拉显示标题栏
- [Android Pro] 完美解决隐藏Listview和RecyclerView去掉滚动条和滑动到边界阴影的方案
- android ListView向上滑动隐藏标题,下拉显示标题栏
- Android View深入学习——实现QQ滑动显示隐藏按钮ListView
- ListView滑动隐藏显示ToolBar的实例
- fragment中实现listView的滑动隐藏toolbar
- Android WebView滑动隐藏Toolbar
- AndroidSwipeLayout的使用(listview,gridview,view中滑动显示隐藏按钮的使用)
- android ListView向上滑动隐藏标题,下拉显示标题栏
- Android ListView GridView添加 padding后padding部分不跟随滑动移动问题解决方案