RecyclerView 将 MaterialToggleButtonGroup 添加到父 LinearLayoutManager

RecyclerView add MaterialToggleButtonGroup to parent LinearLayoutManager

我想制作一个水平 RecyclerView,使用其布局 LinearLayoutManager。我想实现这样的东西:

如您所见,需要 ToggleButtons 或正常的 MaterialButtons。为了让 ToggleButtonsLinearLayoutManager 中正常运行,我需要添加 MaterialButtonToggleGroup 作为子 ViewGroup,这将是 ToggleButtons 我要添加的父级使用自定义适配器。

当我尝试将 MaterialToggleButtonGroup 添加到 LinearLayoutManager 时,它给了我一个错误(我想它会,我的建议我知道是不正确的)。有什么想法吗?

你可以通过这样做来实现这个布局 这是代码

自定义适配器

class ItemAdapter(context: Context):RecyclerView.Adapter<ItemAdapter.ViewHolder>() {
private lateinit var mainView: View
private var mSelectedIndex = -1
private val list: Array<String> = context.resources.getStringArray(R.array.item_names)
class ViewHolder(mainView: View):RecyclerView.ViewHolder(mainView) {
    val itemName: TextView = mainView.findViewById(R.id.textView)
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    mainView = LayoutInflater.from(parent.context).inflate(R.layout.item_list,parent,false)
    return ViewHolder(mainView)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.itemName.text = list[position]
    holder.itemName.setOnClickListener {
        mSelectedIndex = position
        notifyDataSetChanged()
    }
    if (mSelectedIndex == position){
        holder.itemName.setBackgroundResource(R.drawable.rectangle_black)
        holder.itemName.setTextColor(Color.WHITE)
    }else{
        holder.itemName.setBackgroundResource(R.drawable.rectangle)
        holder.itemName.setTextColor(Color.BLACK)
    }
}

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

}

Activity

中的 Recycler 视图实现
class MainActivity : AppCompatActivity() {
private lateinit var mHorizontalRecyclerView: RecyclerView
private lateinit var mAdapter: ItemAdapter
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    mHorizontalRecyclerView = findViewById(R.id.rv_items_list)
    mAdapter = ItemAdapter(this)
    val manager = LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false)
    mHorizontalRecyclerView.adapter = mAdapter
    mHorizontalRecyclerView.layoutManager = manager
}

}

列表项布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    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="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:background="@drawable/rectangle"
        android:gravity="center"
        android:text="Popular"
        android:padding="20dp"
        android:textColor="@color/black"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

未选择自定义 TextView 背景

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="40dp"/>
<solid android:color="@color/white"/>

已选择自定义 TextView 背景

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="40dp"/>
<solid android:color="@color/white"/>