关于RecyclerViewHeader实现

来源:互联网 发布:php代码加密 编辑:程序博客网 时间:2024/06/01 09:02

使用RecyclerViewHeader实现复杂头布局出现滑动回弹问题

  • github作者对这个用法的初步介绍
    效果图

这里写图片描述

1.借助某道翻译了下:
RecyclerViewHeader:
Super fast and easy way to create header for Android RecyclerView.
Lets you create header View for any RecyclerView that uses LinearLayoutManager or GridLayoutManager with just a simple method call.
译:
超级快速、简单的方法来为Android RecyclerView创建标题。
允许您创建标题视图使用的任何RecyclerView LinearLayoutManager或GridLayoutManager只有一个简单的方法调用。

2.作者对此方法阐述的一些限制
Although the library is super easy to use, it has some limitations.
RecyclerViewHeader is based on a ViewGroup being hovered over the RecyclerView (and properly shifted on scroll).
You need to bare in mind that the library is created to be used with relatively simple headers.
E.g. if your header uses a complex layout (with multiple scrolls and focusable elements) or a complicated Touch management system I advice you not to use RecyclerViewHeader, but a proper RecyclerView Adapter that incorporates inflating multiple types of views. There are other libraries on github (for example HeaderRecyclerView by Karumi) that might seem a bit more complicated to use, but are implementing mentioned approach and will fit your needs better.

TLDR:

Simple header
Header smaller than screen height (for vertical RecyclerView) or width (for horizontal RecyclerView)
No complicated touch system (including scrollable elements)
译:
尽管这个库非常容易使用,但它也有一些限制。
RecyclerViewHeader基于ViewGroup是徘徊在RecyclerView(滚动,妥善转移)。
您需要记住,创建库是为了使用相对简单的头文件。
例如如果您的头使用一个复杂的布局(具有多个卷轴和focusable元素)或一个复杂的管理系统,我建议你联系不使用RecyclerViewHeader,但适当的RecyclerView适配器包含膨胀多种类型的视图。github上还有其他的库(例如HeaderRecyclerView Karumi)看起来可能有点复杂,但提到正在实施的方法,将满足您的需要更好。
TLDR:
1.简单的标题
2.头小于屏幕高度(对于垂直RecyclerView)或宽度(水平RecyclerView)
3.没有复杂的触摸系统(包括可滚动的元素)

  • 如何使用
    1.布局中代码:
<android.support.v7.widget.RecyclerView       android:id="@+id/recycler"       android:layout_width="match_parent"       android:layout_height="wrap_content"       android:layout_gravity="center_horizontal|top" />   <com.bartoszlipinski.recyclerviewheader2.RecyclerViewHeader       android:id="@+id/header"       android:layout_width="match_parent"       android:layout_height="100dp"       android:layout_gravity="center_horizontal|top">       <TextView           android:layout_width="wrap_content"           android:layout_height="wrap_content"           android:layout_centerInParent="true"           android:text="header"/>   </com.bartoszlipinski.recyclerviewheader2.RecyclerViewHeader>

2.得到RecyclerViewHeader视图对象:

RecyclerViewHeader header = (RecyclerViewHeader) findViewById(R.id.header);

3.附加RecyclerViewHeader RecyclerView:

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view); // set LayoutManager for your RecyclerView header.attachTo(recyclerView);

4.导入项目

build.gradle

dependencies {    compile 'com.bartoszlipinski.recyclerviewheader:library:1.2.0'}

如果大家有什么疑问可以加这个开发的交流群:
群号:389489634
群二维码:

这里写图片描述

4 0