Search code examples
javaandroidandroid-recyclerviewandroid-togglebutton

RecyclerView add MaterialToggleButtonGroup to parent LinearLayoutManager


I am looking to make a horizontal RecyclerView using its layout as LinearLayoutManager. I want to implement something like this:

enter image description here

As you can see there needs to be ToggleButtons or just normal 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.

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). Any ideas?


Solution

  • 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

    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

    <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

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