Skip to content

Instantly share code, notes, and snippets.

@costular
Last active June 20, 2022 12:05
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save costular/45fc8035f866e9df3b6ba7e77def7ec9 to your computer and use it in GitHub Desktop.
Save costular/45fc8035f866e9df3b6ba7e77def7ec9 to your computer and use it in GitHub Desktop.
Bottom sheet menu with recyclerview faster thanks to Kotlin
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
android:id="@+id/document_sign_type_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="8dp"
android:clipToPadding="false"
xmlns:android="http://schemas.android.com/apk/res/android" />
data class BottomMenuItem(
val resId: Int,
val name: String,
val action: () -> Unit
)
import android.content.Context
import android.support.design.widget.BottomSheetDialog
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
/**
* Created by costular on 29/08/17.
*/
class BottomSheetMenu(private val context: Context,
private val items: List<BottomMenuItem>) {
private val bottomSheetDialog: BottomSheetDialog = BottomSheetDialog(context)
init {
val view = LayoutInflater.from(context).inflate(R.layout.bottom_sheet_menu, null)
bottomSheetDialog.setContentView(view)
with(view) {
bottom_sheet_recycler.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
bottom_sheet_recycler.adapter = BottomSheetMenuAdapter(items)
}
}
fun show() {
bottomSheetDialog.show()
}
}
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
/**
* Created by costular on 29/08/17.
*/
class BottomSheetMenuAdapter(private val items: List<BottomMenuItem>) : RecyclerView.Adapter<BottomSheetMenuAdapter.BottomSheetMenuViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): BottomSheetMenuViewHolder {
return BottomSheetMenuViewHolder(LayoutInflater.from(parent!!.context).inflate(R.layout.item_context_bottom, parent, false))
}
override fun getItemCount(): Int = items.size
override fun onBindViewHolder(holder: BottomSheetMenuViewHolder, position: Int) {
holder.bind(items[position])
}
class BottomSheetMenuViewHolder(val view: View) : RecyclerView.ViewHolder(view) {
fun bind(item: BottomMenuItem) {
with(view) {
bottom_menu_title.text = item.name
bottom_menu_icon.setImageResource(item.resId)
setOnClickListener { item.action() }
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/fragment_history_menu_bottom"
android:layout_width="match_parent"
android:layout_height="48dp"
android:focusable="true"
android:background="?attr/selectableItemBackground"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<ImageView
android:id="@+id/bottom_menu_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/bottom_menu_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="72dp"
android:layout_marginRight="8dp"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
fun showMenu() {
val items = arrayListOf(
BottomMenuItem(R.drawable.ic_document_automatic, "Edit", {
// edit action
}),
BottomMenuItem(R.drawable.ic_document_manual, "Delete", {
// delete action
})
)
BottomSheetMenu(activity, items)
.show()
}
@Afaquejaya
Copy link

Define globally in Your Adapter :

 var onItemClick : ((ItemViewModel) -> Unit)? = null

Inside onBindViewHolder add below code

       holder.imageView.setImageResource(itemsViewModel.image)
       holder.itemView.setOnClickListener(){
                val itemsViewModel = mList[position]
                onItemClick?.invoke(itemsViewModel)
            }

Then in your MainActivity call your onItemClick

        //Define Globally
        private lateinit var yourAdapter: YourAdapter
    
           // ArrayList of class ItemsViewModel
            val data = ArrayList<ItemViewModel>()
    
            // This loop will create 20 Views containing
            // the image with the count of view
            for (i in 1..20) {
                data.add(ItemViewModel(R.drawable.ic_calendar, "Scheme " + i))
            }
    
         
            yourAdapter= YourAdapter(data)
            // This will pass the ArrayList to our Adapter
            var adapter = calViewAdapter
    
            // Setting the Adapter with the recyclerview
            recyclerview.adapter = adapter
    
            yourAdapter.onItemClick = {
                 Toast.makeText(this@MainActivity , "Item clicked ", Toast.LENGTH_SHORT).show()
                //Do whatever you want to
            }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment