Android中项目下的RecyclerView标题
我无法找到解决此类行为的方法: 这是因为RecyclerView的标题在items下面有一点。当然,我想这是回收视图 我怎样才能做到这一点 编辑 我所做的只是为回收者视图添加装饰 这是我的简单装饰:Android中项目下的RecyclerView标题,android,android-recyclerview,Android,Android Recyclerview,我无法找到解决此类行为的方法: 这是因为RecyclerView的标题在items下面有一点。当然,我想这是回收视图 我怎样才能做到这一点 编辑 我所做的只是为回收者视图添加装饰 这是我的简单装饰: public class HeaderItemDeceration extends RecyclerView.ItemDecoration { public HeaderItemDeceration() { } @Override public void get
public class HeaderItemDeceration extends RecyclerView.ItemDecoration {
public HeaderItemDeceration() {
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
if (parent.getChildAdapterPosition(view) == 0) {
outRect.bottom = -100;
}
}
}
它正在工作,但问题是这个标题消失得太快了,我的意思是下一个项目在顶部,它下面有标题,并且立即消失,因为通常下一个项目在顶部时它应该被隐藏
编辑2
我没有解释所有的事情,所以我在这里解释
就我而言,我不想要ActionBar。我想要的是在RecyclerView下的图像,就像上面的例子一样,但不要折叠工具栏。假设我的活动具有父级为Theme.AppCompat.Light.NoActionBar
的样式
考虑到我下面的解释和答案,我正试图通过这样的布局达到目标:
<?xml version="1.0" encoding="utf-8"?>
<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:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="fitXY"
android:src="@drawable/header"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view_items"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:behavior_overlapTop="24dp"/>
</android.support.design.widget.CoordinatorLayout>
我假设我的目标是使用CollasingToolbarLayout可能是错误的 它是两个视图的组合,而不仅仅是回收器视图
将帮助你达到你想要的
诀窍是,在背景中有一个视图,在前面有一个回收站。“回收器”视图有一个标题,可以从顶部形成所需的间隙。
无论何时滚动回收器,您都会收到您使用的侦听器的通知,您将手动滚动底部布局,我在为Scotts的草坪护理应用程序编写主主主屏幕时实现了此效果
这是通过使用CoordinatorLayout、AppBarLayout、CollasingToolbarLayout和RecyclerView实现的。关键是滚动视图行为您需要在RecyclerView上设置app:behavior\u overlapper
和app:layout\u behavior=“@string/appbar\u scrolling\u view\u behavior”
属性(仅当它是AppBarLayout的同级视图时才起作用)
在我的场景中,我将标题与RecyclerView完全分开。根据您的内容管理方式,此解决方案可能不适用于您(尽管将标题保留在RV之外对我来说要简单得多——要管理的视图类型/视图保持架少了一个!)
要点是这样的:
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
>
<android.support.design.widget.AppBarLayout
android:layout_height="300dp" // fixed height of your header container
android:layout_width="match_parent"
android:fitsSystemWindows="true"
>
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap" // snap/exitUntilCollapsed are optional. See more info on scroll flags here: https://developer.android.com/reference/android/support/design/widget/AppBarLayout.LayoutParams.html#setScrollFlags(int)
>
<ImageView // This is where you'd put your header backdrop
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:layout_collapseMode="parallax" // not essential, but most people want the parallax scrolling effect with their image header in this setup. this is how you would do it.
/>
</CollapsingToolbarLayout>
</AppBarLayout>
<RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:behavior_overlapTop="64dp" // This is what you're looking for!
/>
</CoordinatorLayout>
尝试AppBarLayout的所有可能的布局组合\u scrollFlags,这将有助于改进滚动
app:layout_scrollFlags=“scroll | snap”//试试这个它会对你有用的
app:layout_scrollFlags=“scroll | exituntilclopsed”//ur当前滚动
app:layout_scrollFlags=“scroll | enterallways collapsed”
app:layout_scrollFlags=“scroll | enterally”
阅读你能更好地解释你面临的问题吗?该图像中的哪个元素代表您提到的标题?您是否用一个?我可能是解决方案,但您的代码是否可以像上面的图像一样将recyclerview中的第一项覆盖在标题上?您尝试过吗?是的,因为标题将在背景中绘制。如果您选择让它重叠,那么是的。在我完全尝试之前,什么是“@string/appbar\u scrolling\u view\u behavior”?它是设计支持库附带的内置字符串之一。无论何时使用AppBarLayout
,您都很可能希望将任何内容视图的滚动行为绑定到它。如果您好奇,下面是设计库当前设置为:android.support.design.widget.AppBarLayout$ScrollingViewBehavior
的appbar\u scrolling\u view\u behavior
,但您不应该自己定义这个字符串。谢谢,现在我明白了。我编辑了问题,如果您知道我的解释的解决方案,请尝试更改您的答案。已编辑。只需将RV overscrollMode设置为never!只要您使用的是最新版本的支持库23.4.0
,就不会有问题。如果你想让我看看,可以在GitHub上上传一些东西并链接到这里!您能在ObservableScrollView中显示位置或示例吗?我找不到你的主意。