实现效果:
这种效果只能自定义Behavior才能实现:
实现上面这个效果我们只需要重载Behavior以下两个方法就可以实现:
layoutDependsOn():
确定使用Behavior的View要依赖的View的类型
onDependentViewChanged():
当依赖的View状态改变时的回调
说的通俗点就是:当小明移动的时候我要出现,这里依赖的view就是我,监听的view就是小明,监听他是不是移动了
public class SampleTitleBehavior extends CoordinatorLayout.Behavior<View> {
// 列表顶部和title底部重合时,列表的滑动距离。
private float deltaY;
public SampleTitleBehavior() {
}
public SampleTitleBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* 确定使用bahavior的View要依赖的View的类型
* 我这里监听的是Recycleview,当recycleview的状态发生改变的时候我要改变child的状态
* @param parent 代表CoordingLayout
* @param child 代表使用该Behavior的View
* @param dependency 代表要监听的View
* @return
*/
@Override
public boolean layoutDependsOn(@NonNull CoordinatorLayout parent, @NonNull View child, @NonNull View dependency) {
return dependency instanceof RecyclerView;
}
/**
* 被依赖的view状态发生改变的回调
* @param parent 代表CoordingLayout
* @param child 代表使用该Behavior的view
* @param dependency 代表要监听的view
* @return
*/
@Override
public boolean onDependentViewChanged(@NonNull CoordinatorLayout parent, @NonNull View child, @NonNull View dependency) {
Log.i("zjc","dependency.getY():"+dependency.getY()+"");//400 这个高度 = Recycleview上面Imageview的高度
Log.i("zjc","child.getHeight():"+child.getHeight()+"");//100 这个高度 = TextView的高度
Log.i("zjc","deltaY:"+deltaY+""); //0
if(deltaY == 0){
deltaY = dependency.getY()-child.getHeight();
Log.i("zjc","deltaY == 0 deltaY:"+deltaY+""); //300
}
Log.i("zjc","deltaY:"+deltaY+"");
float dy = dependency.getY() - child.getHeight();
dy = dy<0?0:dy;
Log.i("zjc","dy:"+dy+"");//300
float y = -(dy/deltaY)*child.getHeight();
Log.i("zjc","y:"+dy+"");
child.setTranslationY(y);
return true;
}
}
然后我们要在strings.xml里面定义包文件的路径:
<string name="behavior_sample_title">com.othershe.behaviortest.test1.SampleTitleBehavior</string>
然后就是布局文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.othershe.behaviortest.test1.TestActivity1">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:elevation="0dp">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="#00ffffff"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@mipmap/bg"
android:fitsSystemWindows="true"
android:scaleType="fitXY"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.7" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/my_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#ff0000"
android:gravity="center"
android:text="Hello World"
android:textColor="#ffffff"
android:textSize="18sp"
app:layout_behavior="@string/behavior_sample_title" />
</android.support.design.widget.CoordinatorLayout>
效果实现
参考:https://www.jianshu.com/p/b987fad8fcb4