文章目录
效果图
该项目是我在学习第一行代码里面使用Kotlin写RecyclerView的时候写的,有兴趣的可以下载下来研究一下,效果如下:
例子下载
https://download.csdn.net/download/ljp345775/12450945
代码实现 (以网格为例)
1. 首先编写listView需要的item的布局item.xml ,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/linerlayout_water_selector"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
android:padding="5dp">
<ImageView
android:id="@+id/iv_item_lv"
android:layout_width="match_parent"
android:src="@drawable/apple_pic"
android:layout_height="50dp" />
<TextView
android:id="@+id/tv_item_lv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="apple"
android:textSize="20dp" />
</LinearLayout>
效果如下:
2. 编写RecyclerView的适配器adapter(需要继承 RecyclerView.Adapter),代码如下:
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.example.kotlindemo1.R
import com.example.kotlindemo1.beans.Fruit
/**
* @作者: ljp
* @时间: 2020/5/23 9:19
* @描述:
**/
class RecycGridAdapter(val context: Context, var data: List<Fruit>) :
RecyclerView.Adapter<RecycGridAdapter.VH>() {
//内部类 继承
inner class VH(v: View) : RecyclerView.ViewHolder(v) {
val tv_item_lv: TextView = v.findViewById(R.id.tv_item_lv)
val iv_item_lv: ImageView = v.findViewById(R.id.iv_item_lv)
}
//在该方法中创建ViewHolder
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
val view = LayoutInflater.from(context).inflate(R.layout.item4, parent, false)
var vh = VH(view)
vh.itemView.setOnClickListener {
//拿到用户点击的位置
var position = vh.adapterPosition
var fruit = data[position]
Toast.makeText(context, "你点击的了-》" + fruit.name, Toast.LENGTH_SHORT).show()
}
vh.itemView.setOnLongClickListener {
//拿到用户点击的位置
var position = vh.adapterPosition
var fruit = data[position]
Toast.makeText(context, "你长按了-》" + fruit.name, Toast.LENGTH_SHORT).show()
true
}
return vh
}
override fun getItemCount(): Int {
return data.size
}
//在该方法中数据绑定
override fun onBindViewHolder(holder: VH, position: Int) {
var fruit = data[position]
holder.iv_item_lv.setImageResource(fruit.ImgId)
holder.tv_item_lv.text = fruit.name
}
}
3. 主界面中布局代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".activity.RecycGridActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
4. activity中的代码如下:
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.kotlindemo1.R
import com.example.kotlindemo1.adapter.RecycGridAdapter
import com.example.kotlindemo1.adapter.RecycVerticalAdapter
import com.example.kotlindemo1.beans.Fruit
import kotlinx.android.synthetic.main.activity_recycvertical.*
class RecycGridActivity : AppCompatActivity() {
private lateinit var adapter: RecycGridAdapter
private var data = ArrayList<Fruit>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_recycgrid)
supportActionBar?.title = "RecyclerView网格"
initData()
//布局管理 3:代表网格中的列数
var manager = GridLayoutManager(this,3)
//初始化适配器
adapter = RecycGridAdapter(this, data)
recycView.layoutManager = manager
recycView.adapter = adapter
}
private fun initData() {
repeat(2) {
data.add(Fruit("apple", R.drawable.apple_pic))
data.add(Fruit("banana", R.drawable.banana_pic))
data.add(Fruit("orange", R.drawable.orange_pic))
data.add(Fruit("watermelon", R.drawable.watermelon_pic))
data.add(Fruit("pear", R.drawable.pear_pic))
data.add(Fruit("grape", R.drawable.grape_pic))
data.add(Fruit("pineapple", R.drawable.pineapple_pic))
data.add(Fruit("strawberry", R.drawable.strawberry_pic))
data.add(Fruit("cherry", R.drawable.cherry_pic))
data.add(Fruit("mango", R.drawable.mango_pic))
}
}
}
其他界面主要代码
RecyclerView中主要控制布局就是布局管理者 主要有:
线性布局管理: LinearLayoutManager
网格布局管理:GridLayoutManager
瀑布流布局管理:StaggeredGridLayoutManager
RecyclerView纵向
//布局管理
var manager = LinearLayoutManager(this)
//设置成纵向
manager.orientation = LinearLayoutManager.VERTICAL
RecyclerView横向
//布局管理
var manager = LinearLayoutManager(this)
//设置成横向
manager.orientation = LinearLayoutManager.HORIZONTAL
RecyclerView瀑布流
//3:代表有3列,第二个参数代表纵向或者横向
var manager = StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL)
好了,其他具体的代码在例子中都有,有兴趣的小伙伴可以下载下来研究一下。