简体   繁体   English

RecyclerView 将 MaterialToggleButtonGroup 添加到父 LinearLayoutManager

[英]RecyclerView add MaterialToggleButtonGroup to parent LinearLayoutManager

I am looking to make a horizontal RecyclerView using its layout as LinearLayoutManager .我正在寻找使用其布局作为LinearLayoutManager来制作水平RecyclerView I want to implement something like this:我想实现这样的事情:

在此处输入图像描述

As you can see there needs to be ToggleButtons or just normal MaterialButtons .如您所见,需要有ToggleButtons或只是普通的MaterialButtons For the ToggleButtons to behave properly inside the LinearLayoutManager I would need to add MaterialButtonToggleGroup as a child ViewGroup which would be the parent to the ToggleButtons I would add using a Custom Adapter.为了让ToggleButtonsLinearLayoutManager中正常运行,我需要将MaterialButtonToggleGroup添加为子ViewGroup ,这将是我将使用自定义适配器添加的ToggleButtons的父级。

When I try to add a MaterialToggleButtonGroup to the LinearLayoutManager it gives me an error (which I imagined it would, what I'm suggesting I know is not correct).当我尝试将MaterialToggleButtonGroup添加到LinearLayoutManager时,它给了我一个错误(我想象它会,我建议我知道的不正确)。 Any ideas?有任何想法吗?

You can achieve this layout by doing this Here's code您可以通过执行此操作来实现此布局这是代码

Custom Adapter自定义适配器

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
}

} }

Recycler View Implementation in Activity Activity 中的 Recycler View 实现

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
}

} }

list item layout列表项布局

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

Custom TextView Background Unselected自定义 TextView 背景未选中

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

Custom TextView Background Selected定制 TextView 背景已选

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM