如何将顶级商品添加到recyclerview?

时间:2019-11-12 16:07:45

标签: android android-recyclerview textview

我的布局如下-

enter image description here

具有以下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>

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

如何实现这种行为?

我已经尝试在recyclerview适配器的pos 0处添加一些视图,但是它无法正常工作-我希望获得其他解决方案。

2 个答案:

答案 0 :(得分:1)

您可以执行以下操作:

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

答案 1 :(得分:0)

您应该在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]
    }
}