android在RecyclerView上方添加移动标题

android在RecyclerView上方添加移动标题,android,android-recyclerview,android-coordinatorlayout,android-nestedscrollview,Android,Android Recyclerview,Android Coordinatorlayout,Android Nestedscrollview,我需要在我的应用程序中的RecyclerView上方添加一个标题。当用户滚动recyclerview时,标题必须移动(右图)。我已经搜索过了,但还没有找到一个最终的简单解决方案。实现它的最佳和最简单的方法是什么 图像积分:对于这一点,您有两种选择 第一个也是最简单的方法是,您可以在CoordinatorLayout中的折叠工具栏布局中添加ImageView,然后将app:layout\u behavior=“@string/appbar\u scrolling\u view\u behavio

我需要在我的应用程序中的
RecyclerView
上方添加一个标题。当用户滚动recyclerview时,标题必须移动(右图)。我已经搜索过了,但还没有找到一个最终的简单解决方案。实现它的最佳和最简单的方法是什么


图像积分:

对于这一点,您有两种选择

  • 第一个也是最简单的方法是,您可以在CoordinatorLayout中的折叠工具栏布局中添加ImageView,然后将
    app:layout\u behavior=“@string/appbar\u scrolling\u view\u 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>
    
    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
            }