Android开发笔记: RecyclerView 总结

关于 RecyclerView

RecyclerView 是 Android Jetpack 的一部分,用于高效的显示列表型数据(替代原有的 ListView),可以更方便的显示单项、响应事件。

顾名思义,当列表项滚动出屏幕时,RecyclerView 会回收这些元素,并对新进入屏幕的列表项重用该视图。因此,它可以显著提高性能,降低功耗以及改善响应能力。

使用 RecyclerView

1. 在项目中引入 RecyclerView 库

编辑 build.gradle 文件

1
2
3
4
5
dependencies {
...
implementation 'androidx.recyclerview:recyclerview:1.2.1'
...
}

注:上述代码中的 recyclerview 版本不一定是最新版本,请根据实际情况调整。

2. 在布局文件中引入 RecyclerView 控件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:paddingHorizontal="10dp"
android:orientation="vertical">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycle_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>

</LinearLayout>

3. 编辑单列表项的样式

1
2
3
4
5
6
<!--list_item.xml-->
<TextView
android:id="@+id/item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>

4. 实现 RecyclerView Adapter 类

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
private class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{

private List<String> list;

public MyAdapter(List<String> ls) {
this.list = ls;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//TODO
}
});
return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
String str = list.get(position);
holder.mTextView.setText(str);
}

@Override
public int getItemCount() {
return list == null ? 0 : list.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {
TextView mTextView;
public ViewHolder(View itemView) {
super(itemView);
mTextView = (TextView) itemView.findViewById(R.id.item_title);
}
}
}

Kotlin 代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class MyAdapter(val list: List<String>):RecyclerView.Adapter<MyAdapter.ViewHolder>() 
{
inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val mTextView: TextView = view.findViewById(R.id.item_title)
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.list_item, parent, false)
val viewHolder = ViewHolder(view)
viewHolder.itemView.setOnClickListener {
//TODO
}
return ViewHolder(view)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val str = list[position]
holder.mTextView.text = list
}

override fun getItemCount() = fruitList.size
}

5. 在 Activity(或Fragment)中实现 RecyclerView

Java 代码:

1
2
3
4
5
6
7
8
9
10
private RecyclerView.Adapter mAdapter;
private RecyclerView mRecyclerView;

private void initRecycler() {
mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
mAdapter = new MyAdapter(list);//传入list
mRecyclerView = (RecyclerView) findViewById(R.id.recycle_view);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
}

Kotlin 代码:

1
2
3
4
5
6
7
8
private val list = ArrayList<String>()

private fun initRecycler(){
val layoutManager = StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL)
recycle_view.layoutManager = layoutManager
val adapter = MyAdapter(list)
recycle_view.adapter = adapter
}

关于该文章的反馈:请移步“联系”