[英]How to add top item to recyclerview?
I have the following layout -我有以下布局 -
with the following xml -与以下 xml -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="7dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/fragment_marketplace_marketplace_title"
android:textSize="30sp"
android:textStyle="bold" />
<SearchView
android:id="@+id/fragment_marketplace_searchview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:queryHint="Search..."
app:iconifiedByDefault="false"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="14dp"
android:layout_marginLeft="14dp"
android:layout_marginTop="15dp"
android:text="@string/fragment_marketplace_discover_products_from_myverte"
android:textSize="17sp"
android:textStyle="bold" />
<android.support.v7.widget.RecyclerView
android:id="@+id/fragment_marketplace_brands_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:orientation="horizontal"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
tools:listitem="@layout/fragment_marketplace_vendor_row_item" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="14dp"
android:layout_marginLeft="14dp"
android:background="@color/very_light_grey"
android:paddingTop="15dp"
android:text="@string/fragment_marketplace_featured_products"
android:textSize="17sp"
android:textStyle="bold" />
<android.support.v7.widget.RecyclerView
android:id="@+id/fragment_marketplace_products_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@color/very_light_grey"
tools:listitem="@layout/fragment_marketplace_products_row_item" />
</LinearLayout>
What I want is that the textview containing "Featured products" to be always at the top level of the recyclerview and scroll down when I am scrolling the items down.我想要的是包含“特色产品”的 textview 始终位于回收站视图的顶层,并在我向下滚动项目时向下滚动。
How can I achieve this behaviour?我怎样才能实现这种行为?
I have already tried to add some view at the pos 0 of the recyclerview adapter but that did not work correctly - I was hoping to get a different solution.我已经尝试在 recyclerview 适配器的 pos 0 添加一些视图,但这不能正常工作 - 我希望得到一个不同的解决方案。
you can do this:你可以这样做:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="7dp"
android:orientation="vertical">
...
<androidx.core.widget.NestedScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="14dp"
android:layout_marginLeft="14dp"
android:background="@color/very_light_grey"
android:paddingTop="15dp"
android:text="@string/fragment_marketplace_featured_products"
android:textSize="17sp"
android:textStyle="bold" />
<android.support.v7.widget.RecyclerView
android:id="@+id/fragment_marketplace_products_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@color/very_light_grey"
tools:listitem="@layout/fragment_marketplace_products_row_item" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</LinearLayout>
You should implement the getItemViewType
in your RecyclerView adapter.您应该在 RecyclerView 适配器中实现
getItemViewType
。 You also should adapt your onCreateViewHolder method accordingly:您还应该相应地调整您的 onCreateViewHolder 方法:
override fun getItemViewType(position: Int): Int = if (position == 0) return 0 else return 1
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder = if (viewType == 0) createHeaderViewHolder(parent) else createItemViewHolder(parent)
private fun createHeaderViewHolder(parent: ViewGroup) {
// ...
}
private fun createItemViewHolder(parent: ViewGroup) {
// ...
}
override fun getItemCount(): Int = data.size + 1
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
if (position > 0) {
val thisItem = data[position -1]
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.