Nom*_*sta 12 android android-recyclerview
我找不到这种行为的解决方案:
这是RecyclerView的标题是项目下的一点点.当然我猜它是RecyclerView.
我怎样才能做到这一点?
编辑
我所做的只是为回收者视图添加装饰.
这是我的简单装饰:
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
它正在工作,但问题是这个标题消失得太快,我的意思是下一个项目位于顶部,并且在它下面有标题,并且立即消失,因为通常它应该在下一个项目位于顶部时隐藏.
编辑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>
Run Code Online (Sandbox Code Playgroud)
它几乎正常工作.差不多,因为我注意到了不必要的效果,当我滚动到顶部有时我必须重复滚动手势才能到达顶部.我记录了它:
我假设我的目标使用CollapsingToolbarLayout可能是错误的.
ekc*_*ang 12
当我为Scotts的草坪护理应用程序编写主屏幕时,我实现了这种效果.这是一个看起来如何的图像.
这是通过使用CoordinatorLayout,AppBarLayout,CollapsingToolbarLayout和RecyclerView来完成的.关键是滚动视图行为,您需要在RecyclerView上设置它们app:behavior_overlapTop和app:layout_behavior="@string/appbar_scrolling_view_behavior"属性(只有在它是AppBarLayout的同级视图时才有效).
在我的场景中,我将标题与RecyclerView完全分开.根据您的内容是如何管理的,这种解决方案可能不适合你(虽然它是简单了很多对我来说,保持头部的RV的外 - !一个都不能少viewtype /视图持有人管理)
要点最终看起来像这样:
<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>
Run Code Online (Sandbox Code Playgroud)
它是两个视图的组合,而不仅仅是回收者视图。
Android-ObservableScrollView将帮助您达到您想要的效果。
诀窍是,背景中有一个视图,前面有一个回收器。回收器视图有一个标题,它可以使顶部与您想要的间隙。每当您滚动回收器时,您将收到您使用的侦听器的通知,并且您将手动滚动底部布局,
| 归档时间: |
|
| 查看次数: |
2169 次 |
| 最近记录: |