最近有群友提出一些效果,这里做些简单介绍:顶部topbar 底部几个tab 中间recyclerview,好了,recyclerview上滑topbar和tab隐藏,下滑topbar和tab显示,说到这,相信不少读者们已经想到了方法,那就是监听recyclerview,根据滑动的距离或者趋势去显示隐藏自己的view,这当然是个办法,而且实现起来也很快,但是这里呢,为了让大家能去学习更多新的知识,与时俱进嘛,所以介绍了5.0以上的Material风格,具体怎么做,下面细细道来:
step1:添加compile依赖
compile 'com.android.support:design:25.3.1'
step2:布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.zlx.zlxbasecv.MainActivity">
<include layout="@layout/appbar_main"/>
<include layout="@layout/content_main" />
<include layout="@layout/footer_main"/>
</android.support.design.widget.CoordinatorLayout>
<include layout="@layout/appbar_main"/> 布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"/>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" /> 布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:onClick="CoorClick"
android:text="你是谁?你从哪里来?你到哪里去?"/>
</android.support.v4.widget.NestedScrollView>
<include layout="@layout/footer_main"/>布局:
此布局中,大家可以看到父布局有个自定义的behavior:app:layout_behavior="com.example.zlx.zlxbasecv.custom_view.cus_behavior.FooterBehavior" 在下面会提供
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_gravity="bottom"
app:layout_behavior="com.example.zlx.zlxbasecv.custom_view.cus_behavior.FooterBehavior"
android:background="?attr/colorPrimary">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="Tab1"
android:layout_weight="1"
android:gravity="center"
android:textColor="@android:color/white"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="Tab2"
android:layout_weight="1"
android:gravity="center"
android:textColor="@android:color/white"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="Tab3"
android:layout_weight="1"
android:gravity="center"
android:textColor="@android:color/white"/>
</LinearLayout>
布局都写完了,接下来介绍下自定义behavior:新建个FooterBehavior 继承 CoordinatorLayout.Behavior,重写里面的几个方法:
情况一:属性的绑定
- layoutDependsOn:根据返回的布尔值判断两个view是否形成绑定关系。
- onDependentViewChanged:完成两件事
根据View dependency的状态改变V child的状态
返回true,表示已经改变V child的状态。这个返回值给谁用?
情况二:滚动的绑定
onStartNestedScroll,返回true表示子类可以触发nested scroll。其中参数int nestedScrollAxes表示当前滚动方向。例如,return (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;表示滚动方向是垂直的才触发nest scroll。
onNestedPreScroll,onStartNestedScroll返回true会触发这个函数。这个函数的参数是坐标相关的,可以根据滑动距离写其他View的响应逻辑。
public class FooterBehavior extends CoordinatorLayout.Behavior<View>{
public FooterBehavior(Context context, AttributeSet attributeSet){
super(context,attributeSet);
}
/**
* 依赖条件,true表示绑定关系成立
* @param parent
* @param child
* @param dependency
* @return
*/
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
return dependency instanceof AppBarLayout;
}
/**
* 属性依赖逻辑,返回true表示要执行
* @param parent
* @param child
* @param dependency
* @return
*/
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
float scaleY = Math.abs(dependency.getY()) / dependency.getHeight();
child.setTranslationY(child.getHeight() * scaleY);
return true;
}
}