我有一个片段加载到我的MainActivity中。在片段布局的顶部,我有一个LinearLayout,我想在用户向上/向下滚动时显示/隐藏。
这可以使用协调器布局来实现,还是我需要自己进行黑客攻击?
layout.xml:
Process.GetCurrentProcess().Close()
答案 0 :(得分:3)
也许有更好的解决方案,但这可以通过创建自定义CoordinatorLayout.Behavior
并将其添加到CustomLinearLayout
来实现:
//This is taken from a project of mine, it scrolls a Layout up if a snackbar shows up.
public class MoveUpwardBehavior extends CoordinatorLayout.Behavior<View> {
public MoveUpwardBehavior(){
//super();
}
public MoveUpwardBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight());
child.setTranslationY(translationY);
return true;
}
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
return dependency instanceof Snackbar.SnackbarLayout;
}
@Override
public void onDependentViewRemoved(CoordinatorLayout parent, View child, View dependency) {
super.onDependentViewRemoved(parent, child, dependency);
child.setTranslationY(0);
}
}
您需要一个自定义LinearLayout
,但这部分很容易让人感到轻松:
@CoordinatorLayout.DefaultBehavior(MoveUpwardBehavior.class)
public class CustomLinearLayout extends LinearLayout {
public CustomLinearLayout(Context context) {
super(context);
}
public CustomLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CustomLinearLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
}
当然,您需要在xml
:
<com.your.project.CustomLinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#f00"
app:layout_anchor="@+id/discoverRView"
app:layout_anchorGravity="top">
</com.your.project.CustomLinearLayout>
所以,我认为你明白了。您需要更新行为以依赖于RecyclerView
的滚动。如果您需要更多帮助,我可以尝试构建一个有效的示例。