实际效果只有二级页面的TabLayout能够粘在顶部,而一级页面是不会有联动的。
解决方案:自定义NestedCoordinatorLayout,替换二级页面的CoordinatorLayout。
NestedCoordinatorLayout代码如下,
import android.content.Context;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.Size;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.view.NestedScrollingChild;
import android.support.v4.view.NestedScrollingChildHelper;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* A {@link CoordinatorLayout} that implements {@link NestedScrollingChild}.
* This means it can act as a nested scrolling child, and thus forward scroll events to parents.
*
* This is useful for nested Coordinators, e.g. for inner fragments in a parent activity. If this is used,
* scrolls to the inner fragment will trigger scroll animations (e.g. {@code AppBarLayout}s, fabs, ...)
* to the parent activity as well.
*
* This works by *not* reinventing the wheel and reusing the same nested scrolling logic implemented
* by behaviors. There is a dummy view inside the sheet that is capable of getting nested scrolling
* callbacks, and forward them to the *outer* behavior that they normally would never reach.
*/
public class NestedScrollCoordinatorLayout extends CoordinatorLayout implements NestedScrollingChild {
private static final String TAG = NestedScrollCoordinatorLayout.class.getSimpleName();
/**
* Constant for {@link #setPassMode(int)}. When this is selected, scroll events are
* passed to the parent stream and, at the same time, to this Co