Let’s start with a rendering

Here is the use of third-party dependency library implementation, detailed use of the tutorial please visit github official document, function is very powerful, star more than 3.3K, citing the official word “this control does not rely on any parent layout, not for RecyclerView, ListView, Instead, any childView in a ViewGroup can use the scroll menu.” SwipeDelMenuLayout

build.gradle(Project)

 maven { url "https://jitpack.io" }
Copy the code

build.gradle(module)

Implementation 'com. Making. McXtzhang: SwipeDelMenuLayout: V1.3.0'Copy the code

The focus is on its use, mainly in the layout of the adapter, we use the following parent layout control SwipeMenuLayout to achieve the effect of the side slide. The two buttons are sideslip content.

<? The XML version = "1.0" encoding = "utf-8"? > <com.mcxtzhang.swipemenulib.SwipeMenuLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="60dp" android:clickable="true" android:orientation="horizontal"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"  android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="59dp"> <TextView android:id="@+id/tvName" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="202020"/> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#d0d0d0"/> </LinearLayout> <Button android:id="@+id/btnTop" android:layout_width="60dp" android:layout_height="match_parent" android:background="#BEBEBE" <Button android:id="@+id/btnDelete" Button android:id="@+id/btnDelete" Android :layout_width="60dp" Android :layout_height="match_parent" Android :background="#ff0000" Android :text=" delete" android:textColor="@android:color/white"/> </com.mcxtzhang.swipemenulib.SwipeMenuLayout>Copy the code

Adapter code, is common RecyclerView adapter, we just need to delete, top two click callback, write 2 corresponding delete, top method. NotifyDataSetChanged is notifyDataSetChanged, and notifyDataSetChanged is notifyDataSetChanged, and notifyDataSetChanged is notifyDataSetChanged.

class SampleAdapter(val list: ArrayList<String>): RecyclerView.Adapter<SampleAdapter.ViewHolder>() { inner class ViewHolder(view:View):RecyclerView.ViewHolder(view){ val tvName:TextView=view.findViewById(R.id.tvName) val btnTop:TextView=view.findViewById(R.id.btnTop) val btnDelete:TextView=view.findViewById(R.id.btnDelete) } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SampleAdapter.ViewHolder { val view=LayoutInflater.from(parent.context).inflate(R.layout.item_sample,parent,false) return ViewHolder(view) } override fun getItemCount()=list.size override fun onBindViewHolder(holder: SampleAdapter.ViewHolder, position: Int) {holder. TvName. SetText (list. Get (position)) / / delete button holder. BtnDelete. SetOnClickListener { OnItemClickDelete. Invoke (it, position)} / / holder. The top button btnTop. SetOnClickListener {onItemClickTop. Invoke (it, position)}} Delete fun delete(position: Int){list.removeat (position) notifyDataSetChanged()} Int){ list.add(0,list.removeAt(position)) notifyDataSetChanged() } lateinit var onItemClickDelete: (v: View, pos: Lateinit var onItemClickTop: (v: View, pos: Int) -> UnitCopy the code

activity_main.xml

<? The 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="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/rv_sample" android:layout_width="match_parent" android:layout_height="match_parent"/> </androidx.constraintlayout.widget.ConstraintLayout>Copy the code

MainAcivity

class MainActivity : AppCompatActivity() { private lateinit var rvSample:RecyclerView override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) rvSample=findViewById(R.id.rv_sample) / / layout manager val layoutManager = LinearLayoutManager (this) layoutManager. Orientation = LinearLayoutManager. VERTICAL Rvsample. layoutManager=layoutManager val list=ArrayList<String>() 10){list.add(" I am the first ${I} ")} val sampleAdapter= sampleAdapter (list) rvsampleadapter =sampleAdapter // SampleAdapter. OnItemClickTop = {v, pos - > sampleAdapter. The top (pos)} / / remove sampleAdapter onItemClickDelete = {v, pos - > sampleAdapter.delete(pos) } } }Copy the code