繁体   English   中英

如何将顶级项目添加到recyclerview?

[英]How to add top item to recyclerview?

我有以下布局 -

在此处输入图像描述

与以下 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>

我想要的是包含“特色产品”的 textview 始终位于回收站视图的顶层,并在我向下滚动项目时向下滚动。

我怎样才能实现这种行为?

我已经尝试在 recyclerview 适配器的 pos 0 添加一些视图,但这不能正常工作 - 我希望得到一个不同的解决方案。

你可以这样做:

<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>

您应该在 RecyclerView 适配器中实现getItemViewType 您还应该相应地调整您的 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM