Android 滚动RecyclerView时是否显示/隐藏LinearLayout?

Android 滚动RecyclerView时是否显示/隐藏LinearLayout?,android,android-layout,android-fragments,android-coordinatorlayout,Android,Android Layout,Android Fragments,Android Coordinatorlayout,我的主要活动中加载了一个片段。在片段布局的顶部,我有一个线性布局,我想在用户上下滚动时显示/隐藏它 这可以通过协调器布局来实现,还是我需要自己动手 layout.xml: <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk

我的主要活动中加载了一个片段。在片段布局的顶部,我有一个线性布局,我想在用户上下滚动时显示/隐藏它

这可以通过协调器布局来实现,还是我需要自己动手

layout.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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ececec">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/discoverRView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingLeft="5dp"
            android:paddingRight="5dp">

        </android.support.v7.widget.RecyclerView>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="#f00"
            app:layout_anchor="@+id/discoverRView"
            app:layout_anchorGravity="top"
            app:layout_scrollFlags="scroll"
            >

         </LinearLayout>



    </android.support.design.widget.CoordinatorLayout>

也许有更好的解决方案,但这可以通过创建一个自定义的
协调布局.行为
并将其添加到
自定义线性布局中来实现:

//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);
    }
}
当然,您需要在
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
的滚动来更新行为。如果您需要更多帮助,我可以尝试构建一个工作示例。

也许有更好的解决方案,但这可以通过创建自定义
协调布局。行为
并将其添加到
自定义线性布局中来实现:

//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);
    }
}
当然,您需要在
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
的滚动来更新行为。如果您需要更多帮助,我可以尝试构建一个工作示例。

您是否已经尝试将
滚动标记添加到
线性布局中?(与
AppBarLayout
中的行为类似)好的,这只是一个简单的想法。^^'但是我添加了一个答案。您是否已经尝试将
滚动标记添加到
线性布局中?(与
AppBarLayout
中的行为类似)好的,这只是一个简单的想法。^^'但我补充了一个答案。谢谢!你真是太好了,我要尝试一下。再次感谢!我已经测试过了,效果很好。但与此同时,我找到了一个更简单的解决方案:在AppBarLayout中使用工具栏(愚蠢的我)。然后我根据需要设置工具栏的样式,将我喜欢的视图放在里面。我尝试了很多方法来编写这段代码,但它对我不起作用。。。你能详细解释一下我如何使用你的电脑吗code@AhamadullahSaikat我只是在回答中详细阐述了解决方案check@yennsarah您能告诉我如何使用您的constom LinearLayout和MoveUpwardBehavior布局吗?谢谢!你真是太好了,我要尝试一下。再次感谢!我已经测试过了,效果很好。但与此同时,我找到了一个更简单的解决方案:在AppBarLayout中使用工具栏(愚蠢的我)。然后我根据需要设置工具栏的样式,将我喜欢的视图放在里面。我尝试了很多方法来编写这段代码,但它对我不起作用。。。你能详细解释一下我如何使用你的电脑吗code@AhamadullahSaikat我只是在回答中详细阐述了解决方案check@yennsarah您能告诉我如何使用您的constom LinearLayout和MoveUpwardBehavior布局吗?