【问题标题】:Adding a horizontal Adapter in a vertical one在垂直适配器中添加水平适配器
【发布时间】:2021-12-12 05:35:57
【问题描述】:

我需要使用 CocatAdapter 将水平适配器“添加”到垂直适配器。

【问题讨论】:

    标签: android android-recyclerview adapter


    【解决方案1】:

    这是我出来的解决方案:

    假设您有一个适配器(垂直方向),它由其他几个适配器组成(

    其中一些,水平方向)。如果是这样的话,你可以在 Concat Adapter 和我的类之间使用一个很好的组合。

    类是->

    class HorizontalListAdapter<T>(
        private val adapter: ListAdapter<T, *>,
    ) : RecyclerView.Adapter<HorizontalListAdapter.ViewHolder<T>>() {
        override fun onCreateViewHolder(
            parent: ViewGroup,
            viewType: Int,
        ): ViewHolder<T> = ViewHolder(
            parent.inflate(
                R.layout.horizontal_generic_recycler_layout),
                adapter
        )
    
        class ViewHolder<T>(view: View, private val adapter: ListAdapter<T, *>) :
            RecyclerView.ViewHolder(view) {
            fun bind() {
                itemView.findViewById<RecyclerView>(R.id.recycler_horizontal_generic).adapter = adapter
            }
        }
    
        override fun getItemViewType(position: Int): Int = R.layout.horizontal_generic_recycler_layout
    
        fun submitList(list: List<T>) {
            val currentAdapterListItems = adapter.currentList
            if (list != currentAdapterListItems) {
                adapter.submitList(list)
                notifyItemChanged(0)
            }
        }
    
        override fun onBindViewHolder(holder: ViewHolder<T>, position: Int) {
            holder.bind()
        }
        override fun getItemCount(): Int = 1
    }
    

    “通用”xml 是 ->

    <?xml version="1.0" encoding="utf-8"?>
    <layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recycler_horizontal_generic"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:paddingHorizontal="4dp"
                app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
                app:layout_constraintTop_toTopOf="parent" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </layout>
    

    最后但同样重要的是,您的 horizontal recycler -&gt; 适配器

    class Adapter(private val callback: Callback) :
        ListAdapter<ItemProductPresentation, ViewHolder>(ProductPresentationDiffCallback) {
    
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder =
            ViewHolder(
                ListItemProductPresentationBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                ),
                callback
            )
    
        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            holder.bind(getItem(position))
        }
    
        class ViewHolder(
            private val binding: ListItemProductPresentationBinding,
            private val callback: Callback,
        ) : RecyclerView.ViewHolder(binding.root) {
            fun bind(input: ItemProductPresentation) {
                binding.imgChecked.isVisible = input.selected
            }
        }
    
        interface Callback { fun onPresentationSelected(presentationItem: ItemProductPresentation) }
    }
    
    object ProductPresentationDiffCallback : DiffUtil.ItemCallback<ItemProductPresentation>() {
        override fun areItemsTheSame(
                oldItem: ItemProductPresentation,
                newItem: ItemProductPresentation
        ): Boolean = oldItem.presentation == newItem.presentation
    
        override fun areContentsTheSame(
                oldItem: ItemProductPresentation,
                newItem: ItemProductPresentation
        ): Boolean = oldItem == newItem
    }
    
    

    所以,在你的视图中 ->

        private val adapter = ConcatAdapter()
        ....
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            horizontalAdapter = HorizontalListAdapter(ProductPresentationAdapter(this))
            adapter.addAdapter(horizontalAdapter)
            binding.recycler.adapter = adapter
        }
    

    查看的 xml ->

    <?xml version="1.0" encoding="utf-8"?>
    <layout 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">
    
            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/lyt_constraint"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/bkg_white_ffffff_rounded_12">
    
               <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/recycler"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
            </androidx.constraintlayout.widget.ConstraintLayout>
    </layout>
    

    这样,Horizo​​ntalListAdapter 类基本上适应了一个 ListAdapter(你可以让这个类也允许另一种 Adapters ),它应该位于一个更大的垂直滚动适配器内的水平适配器中。在这个例子中,我传递了一个回调,它在视图中工作得很好。

    希望这会有所帮助。

    谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多