【Android】使用recyclerView+adapter实现滑动布局,并添加点击和长按事件
本文基于《第一行代码(第三版)》,记录一下使用kotlin语言建立recyclerView+adapter的滑动布局,并在adapter中设定监听器,已达到在Activity中使用adapter时自主设定点击和长按事件。
新建fruit类、fruit的布局
fruit类
1
| class Fruit(val name:String, val bitmap: Bitmap)
|
自定义布局
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="60dp">
<ImageView android:id="@+id/fruitImage" android:layout_width="40dp" android:layout_height="40dp" android:layout_gravity="center_vertical" android:layout_marginLeft="10dp"/>
<TextView android:id="@+id/fruitName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="10dp" />
</LinearLayout>
|
定义adapter适配器及监听接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| package com.example.adaptertest
import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.ImageView import android.widget.TextView import androidx.recyclerview.widget.RecyclerView
class FruitAdapter(val fruitsList: List<Fruit>): RecyclerView.Adapter<FruitAdapter.ViewHolder>(){ inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view){ val fruitView = view val fruitImage : ImageView = view.findViewById(R.id.fruitImage) val fruitName : TextView = view.findViewById(R.id.fruitName) } interface OnItemClickListener { fun onItemClick(view: View?, position: Int) fun onItemLongClick(view: View?, position: Int) } private var onItemClickListener: OnItemClickListener? = null
fun setOnItemClickListener(onItemClickListener: OnItemClickListener?) { this.onItemClickListener = onItemClickListener } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { val view = LayoutInflater.from(parent.context).inflate(R.layout.fruit_item, parent, false) val viewHolder = ViewHolder(view)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) { val fruit = fruitsList[position] holder.fruitImage.setImageBitmap(fruit.imgBitmap) holder.fruitName.text = fruit.name if (onItemClickListener != null) { holder.fruitView.setOnClickListener { val layoutPos = holder.layoutPosition onItemClickListener!!.onItemClick(holder.fruitView, layoutPos) } holder.fruitView.setOnLongClickListener { val layoutPos = holder.layoutPosition onItemClickListener!!.onItemLongClick(holder.fruitView, layoutPos) false } } }
override fun getItemCount() = fruitsList.size }
|
在Activity里使用recyclerView
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| class MainActivity : AppCompatActivity() {
private val fruitList = ArrayList<Fruit>()
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) initFruits() val layoutManager = LinearLayoutManager(this) recyclerView.layoutManager = layoutManager val adapter = FruitAdapter(fruitList) recyclerView.adapter = adapter
adapter.setOnItemClickListener(object : FruitAdapter.OnItemClickListener{ override fun onItemClick(view: View?, position: Int) { Toast.makeText(this@MainActivity,"点击事件", Toast.LENGTH_SHORT).show() } override fun onItemLongClick(view: View?, position: Int) { Toast.makeText(this@MainActivity,"长按事件", Toast.LENGTH_SHORT).show() fruitList.removeAt(position) adapter.notifyDataSetChanged() }
}) }
private fun initFruits() { repeat(2) { fruitList.add(Fruit("Apple", R.drawable.apple_pic)) fruitList.add(Fruit("Banana", R.drawable.banana_pic)) fruitList.add(Fruit("Orange", R.drawable.orange_pic)) fruitList.add(Fruit("Watermelon", R.drawable.watermelon_pic)) fruitList.add(Fruit("Pear", R.drawable.pear_pic)) fruitList.add(Fruit("Grape", R.drawable.grape_pic)) fruitList.add(Fruit("Pineapple", R.drawable.pineapple_pic)) fruitList.add(Fruit("Strawberry", R.drawable.strawberry_pic)) fruitList.add(Fruit("Cherry", R.drawable.cherry_pic)) fruitList.add(Fruit("Mango", R.drawable.mango_pic)) } }
}
|
改变recyclerView的布局为横向排布或网格排布