gpt4 book ai didi

android - CoordinatorLayout 与 RecyclerView

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:24:57 25 4
gpt4 key购买 nike

我有一个 LinearLayout,当我在 RecyclerView 上向上滚动时我想隐藏它,当我向下滚动时它会重新出现;行为应该就像工具栏隐藏和重新出现的方式一样。

这是我目前所拥有的:

<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
android:id="@+id/viewToHideOnScroll
android:layout_width="match_parent"
android:layout_height="wrap_content">

<!-- other stuff inside the LinearLayout -->

</LinearLayout>

<RecyclerView
android:id="@+id/recyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

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

据我目前所知,我可以在 viewToHideOnScroll 上指定一个 app:layout_behavior 值,以便它根据滚动事件平滑地滚动进出 View 回收 View 。为此,我必须编写一个自定义类 ViewToHideOnScrollBehavior 并覆盖 layoutDependsOn 和一些其他方法(onNestedScroll?)。

如果那是正确的,这就是我所拥有的:

public class ViewToHideOnScrollBehavior extends CoordinatorLayout.Behavior<LinearLayout> {

public ViewToHideOnScrollBehavior(Context context, AttributeSet attrs) {}

@Override
public boolean layoutDependsOn(CoordinatorLayout parent, LinearLayout child, View dependency) {
return dependency instanceof RecyclerView;
}

// some other method to override, I don't know
}

谁能给我提示,还是我做错了?

我一直在关注 https://lab.getbase.com/introduction-to-coordinator-layout-on-android/

最佳答案

当用户滚动你的线性布局时,你必须将 LinearLayout 放在 AppBar 布局中你必须创建如下所示的 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/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
android:id="@+id/lytSearchBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="@dimen/fivedp"
app:layout_scrollFlags="scroll|enterAlways" // layout_scrollFlags for scroll layout
android:visibility="visible">

</LinearLayout>
</android.support.design.widget.AppBarLayout>

<android.support.v7.widget.RecyclerView
android:id="@+id/rvOrderList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/lytSearchBar"
android:paddingTop="@dimen/tendp"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />

在 RecyclerView 中,不要忘记添加属性 app:layout_behaviour,如上面的 xml 所示。

关于android - CoordinatorLayout 与 RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31956148/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com