1+ package com.example.slidemenulayout
2+
3+ import androidx.appcompat.app.AppCompatActivity
4+ import android.os.Bundle
5+ import android.util.Log
6+ import android.view.View
7+ import android.view.ViewGroup
8+ import android.widget.TextView
9+ import androidx.recyclerview.widget.RecyclerView
10+ import com.afollestad.materialdialogs.MaterialDialog
11+ import com.afollestad.materialdialogs.input.input
12+ import com.davidsu.slidemenulayout.SlideMenuLayout
13+ import kotlinx.android.synthetic.main.activity_main.*
14+ import kotlinx.android.synthetic.main.item.view.*
15+
16+ class MainActivity : AppCompatActivity () {
17+
18+ data class Bean (
19+ val icon : Int ,
20+ val title : String ,
21+ var content : String
22+ )
23+
24+ inner class Adapter : RecyclerView .Adapter <RecyclerView .ViewHolder >() {
25+
26+ override fun getItemCount (): Int {
27+ return mData.size
28+ }
29+
30+ override fun onBindViewHolder (holder : RecyclerView .ViewHolder , position : Int ) {
31+ val slideMenuLayout = holder.itemView as SlideMenuLayout
32+ holder.itemView.ivIcon.setImageResource(mData[position].icon)
33+ holder.itemView.tvTitle.text = mData[position].title
34+ holder.itemView.tvContent.text = mData[position].content
35+ slideMenuLayout.closeImmediate()
36+
37+
38+ slideMenuLayout.getMenuView<View >()?.findViewById<TextView >(R .id.tvDelete)?.setOnClickListener {
39+ mData.removeAt(position)
40+ notifyItemChanged(position)
41+ }
42+
43+ slideMenuLayout.getMenuView<View >()?.findViewById<TextView >(R .id.tvUpdate)?.setOnClickListener {
44+ MaterialDialog (this @MainActivity).show {
45+ input { _, charSequence ->
46+ title = " Input content"
47+ mData[position].content = charSequence.toString()
48+ notifyItemChanged(position)
49+ }
50+ }
51+
52+ }
53+
54+ }
55+
56+ override fun onCreateViewHolder (parent : ViewGroup , viewType : Int ): RecyclerView .ViewHolder {
57+ return object :
58+ RecyclerView .ViewHolder (layoutInflater.inflate(R .layout.item, parent, false )) {}
59+ }
60+
61+ }
62+
63+ private val mData = arrayListOf<Bean >(
64+ Bean (R .drawable.ic_launcher_foreground, " Danny" , " Hello,how are you." ),
65+ Bean (R .drawable.ic_launcher_foreground, " Cathy" , " Are you free tonight?" ),
66+ Bean (R .drawable.ic_launcher_foreground, " Mom" , " Go home,you bastard." ),
67+ Bean (R .drawable.ic_launcher_foreground, " Danny" , " Hello,how are you." ),
68+ Bean (R .drawable.ic_launcher_foreground, " Cathy" , " Are you free tonight?" ),
69+ Bean (R .drawable.ic_launcher_foreground, " Mom" , " Go home,you bastard." ),
70+ Bean (R .drawable.ic_launcher_foreground, " Danny" , " Hello,how are you." ),
71+ Bean (R .drawable.ic_launcher_foreground, " Cathy" , " Are you free tonight?" ),
72+ Bean (R .drawable.ic_launcher_foreground, " Mom" , " Go home,you bastard." ),
73+ Bean (R .drawable.ic_launcher_foreground, " Danny" , " Hello,how are you." ),
74+ Bean (R .drawable.ic_launcher_foreground, " Cathy" , " Are you free tonight?" ),
75+ Bean (R .drawable.ic_launcher_foreground, " Mom" , " Go home,you bastard." ),
76+ Bean (R .drawable.ic_launcher_foreground, " Danny" , " Hello,how are you." ),
77+ Bean (R .drawable.ic_launcher_foreground, " Cathy" , " Are you free tonight?" ),
78+ Bean (R .drawable.ic_launcher_foreground, " Mom" , " Go home,you bastard." ),
79+ Bean (R .drawable.ic_launcher_foreground, " Danny" , " Hello,how are you." ),
80+ Bean (R .drawable.ic_launcher_foreground, " Cathy" , " Are you free tonight?" ),
81+ Bean (R .drawable.ic_launcher_foreground, " Mom" , " Go home,you bastard." ),
82+ Bean (R .drawable.ic_launcher_foreground, " Danny" , " Hello,how are you." ),
83+ Bean (R .drawable.ic_launcher_foreground, " Cathy" , " Are you free tonight?" ),
84+ Bean (R .drawable.ic_launcher_foreground, " Mom" , " Go home,you bastard." )
85+ )
86+
87+ override fun onCreate (savedInstanceState : Bundle ? ) {
88+ super .onCreate(savedInstanceState)
89+ setContentView(R .layout.activity_main)
90+ recyclerView.adapter = Adapter ()
91+ }
92+ }
0 commit comments