重定义AppBarLayout.Behavior
public class AppBarLayoutBehavior extends AppBarLayout.Behavior {
float mOrignBottomOfAppBarLayout;
private AppBarLayout mAppBarLayout;
AppBarLayoutScrollListem appBarLayoutScrollListem ;
public AppBarLayoutBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
appBarLayoutScrollListem = new AppBarLayoutScrollListem();
}
@Override
public boolean onLayoutChild(CoordinatorLayout parent, AppBarLayout child, int layoutDirection) {
boolean handled = super.onLayoutChild(parent, child, layoutDirection);
if(mAppBarLayout == null) {
mAppBarLayout = child;
mOrignBottomOfAppBarLayout = child.getBottom();
}
return handled;
}
@Override
public boolean onStartNestedScroll(CoordinatorLayout parent, AppBarLayout child, View directTargetChild, View target, int nestedScrollAxes, int type) {
return (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;//表示滚动方向是垂直的才触发nest scroll。
}
@Override
public boolean onNestedPreFling(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, float velocityX, float velocityY) {
RecyclerView recyclerView = target.findViewWithTag("recyclerView");
if (recyclerView != null) {
recyclerView.removeOnScrollListener(appBarLayoutScrollListem);
recyclerView.addOnScrollListener(appBarLayoutScrollListem);
appBarLayoutScrollListem.child = child;
appBarLayoutScrollListem.target = target;
}
return super.onNestedPreFling(coordinatorLayout, child, target, velocityX, velocityY);
}
public class AppBarLayoutScrollListem extends RecyclerView.OnScrollListener {
AppBarLayout child;
View target;
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
if (child != null && target != null) {
RecyclerView recyclerView1 = target.findViewWithTag("recyclerView");
if (recyclerView1 != null) {
if (!recyclerView1.canScrollVertically(-1)) {
child.setExpanded(true);
}
recyclerView1.removeOnScrollListener(this);
}
}
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
}
}
}
xml文件如下:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayoutID"
android:layout_width="match_parent"
android:layout_height="@dimen/x90"
app:layout_behavior="xxx.AppBarLayoutBehavior">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="50dip"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<...>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerViewId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20px"
android:layout_marginRight="20px"
android:tag="recyclerView"/>
</android.support.design.widget.CoordinatorLayout>