RecyclerView上方的android-add移动标头

问题描述 投票:0回答:1

我需要在我的应用中的RecyclerView上方添加标题。用户滚动recyclerview时,标头必须移动(右图)。我已经搜索过,但没有找到最终的直接解决方案。最佳和最简单的实施方法是什么?

enter image description here

图片来源:link

android android-recyclerview android-coordinatorlayout android-nestedscrollview
1个回答
0
投票

为此,您有2个选择。

  1. 最简单的方法是,您可以在CoordinatorLayout中的CollapsingToolbarLayout中添加ImageView,然后将app:layout_behavior="@string/appbar_scrolling_view_behavior"添加到RecyclerView中>
<androidx.coordinatorlayout.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">

        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content>

            <com.google.android.material.appbar.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="wrap_content"/>

                    <androidx.appcompat.widget.Toolbar
                        android:layout_width="match_parent"
                        android:layout_height="?actionBarSize"
                        app:layout_collapseMode="pin"/>

            </com.google.android.material.appbar.CollapsingToolbarLayout>

        </com.google.android.material.appbar.AppBarLayout>

        <androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/transparent"
            android:fillViewport="true"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

</androidx.coordinatorlayout.widget.CoordinatorLayout>
  1. 第二个选项在滚动时将具有相同的效果,是在ReyclerViewAdapter中它可以理解2种viewType,一种用于图像,另一种用于项目,这样,您必须将图像作为项目的第一项传递列表
class CustomAdapter(
            private var list: ArrayList<String>
    ) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {


        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
            return if (viewType == VIEW_TYPE_ONE) {
                ViewHolder1(LayoutInflater.from(context).inflate(R.layout.your_list_item_1, parent, false))
            } else ViewHolder2(LayoutInflater.from(context).inflate(R.layout.your_list_item_2, parent, false))
        }

        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            if (position == 0) {
                (holder as ViewHolder1).bind(position)
            } else {
                (holder as ViewHolder2).bind(position)
            }
        }

        override fun getItemCount(): Int {
            return list.size
        }

        override fun getItemViewType(position: Int): Int {
            return if (position == 0) {
                VIEW_TYPE_ONE
            } else VIEW_TYPE_TWO
        }


        private inner class ViewHolder1 internal constructor(itemView: View) : RecyclerView.ViewHolder(itemView) {

            var yourView: ImageView  = itemView.findViewById(R.id.yourView)

            fun bind(position: Int) {
                yourView.setImageResource(list[position])
            }
        }

        private inner class ViewHolder2 internal constructor(itemView: View) : RecyclerView.ViewHolder(itemView) {

            var yourView: TextView = itemView.findViewById(R.id.yourView)

            fun bind(position: Int) {
                yourView.text = list[position]
            }
        }

        companion object {
            private const val VIEW_TYPE_ONE = 1
            private const val VIEW_TYPE_TWO = 2
        }
© www.soinside.com 2019 - 2024. All rights reserved.