Recycleview的简单使用

xml

activity_main.xml

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
<RelativeLayout 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="com.example.yangqiuyan.clickdemo.MainActivity">

<Button
android:id="@+id/add_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="add" />

<Button
android:id="@+id/remove_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/add_button"
android:text="remove" />

<android.support.v7.widget.RecyclerView
android:id="@+id/my_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/remove_button"
android:scrollbars="vertical" />

</RelativeLayout>

MainActivity.java

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
class MainActivity : AppCompatActivity() {

var mMyAdapter: MyAdapter? = null
var mLayoutManager: LinearLayoutManager? = null
var mRecyclerView: RecyclerView? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initData()
initView()
initListener()
}

fun initData() {
mMyAdapter = MyAdapter(this, getData())
mLayoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
}

fun initView() {
mRecyclerView = findViewById(R.id.my_recyclerview)
// 设置布局管理器
mRecyclerView!!.layoutManager = mLayoutManager
// 设置适配器
mRecyclerView!!.adapter = mMyAdapter
// 设置recycleview添加和移除动画
mRecyclerView!!.itemAnimator = DefaultItemAnimator()
}

fun getData(): ArrayList<String> {
var data = ArrayList<String>()
(0 until 25).mapTo(data) { "item" + it }
return data
}

fun initListener() {
add_button.setOnClickListener {
mMyAdapter!!.addNewItem()
}
remove_button.setOnClickListener {
mMyAdapter!!.deleteitem()
}
}
}

MyAdapter.java

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
class MyAdapter(context: Context, data: ArrayList<String>) : RecyclerView.Adapter<MyAdapter.ViewHolder>() {

var context: Context? = null
var data: ArrayList<String>? = null

init {
this.context = context
this.data = data
}

override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
var view = LayoutInflater.from(context).inflate(R.layout.recycleview_item, parent, false)
var viewHolder = ViewHolder(view)
return viewHolder
}

override fun getItemCount(): Int {
return data!!.size
}

override fun onBindViewHolder(holder: ViewHolder?, position: Int) {
holder!!.textview!!.text = data!![position]
}


class ViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView) {
var textview: TextView? = null

init {
textview = itemView?.findViewById(R.id.textview)
}
}

fun addNewItem() {
data!!.add(1, "new item")
notifyItemInserted(1)
}

fun deleteitem() {
data!!.removeAt(1)
notifyItemRemoved(1)
}
}

每个item的布局文件这里就不贴出来。

0%