深入RecyclerView一(开始使用)

RecyclerView是2014年Google I/O发布的控件,关于这个控件我只想说两个字: 太艺术了
阅读能力强的可以直接看官方参考: 传送门(需翻墙)

那我们开始吧!
首先,要把RecyclerView跑起来,写一个简单的Demo

新建一个项目,布局文件这么写:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>

RecyclerView同ListView一样,需要一个适配器adapter,RecyclerView的适配器继承自RecyclerView.Adapter,
这样,我们先写这个适配器需要填充的item,再写Adapter

recycler_item_list.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?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:orientation="vertical"
android:padding="20dp">
<TextView
android:id="@+id/textName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp" />
<TextView
android:id="@+id/textNum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp" />
</LinearLayout>

RvAdapter:

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
package com.liompei.recyclerviewdemo;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
import java.util.Map;
/**
* Created by BLM on 2016/7/20.
*/
public class RvAdapter extends RecyclerView.Adapter<RvAdapter.RvHolder> {
private Context context;
private List<Map<String, String>> mapList;
public RvAdapter(Context context, List<Map<String, String>> mapList) {
this.context = context;
this.mapList = mapList;
}
@Override
public RvHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new RvHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, parent, false));
}
@Override
public void onBindViewHolder(RvHolder holder, int position) {
holder.textName.setText(mapList.get(position).get("name"));
holder.textNum.setText(mapList.get(position).get("num"));
}
@Override
public int getItemCount() {
return mapList.size();
}
class RvHolder extends RecyclerView.ViewHolder {
private TextView textName, textNum;
public RvHolder(View itemView) {
super(itemView);
textName = (TextView) itemView.findViewById(R.id.textName);
textNum = (TextView) itemView.findViewById(R.id.textNum);
}
}
}

RvAdapter中,实例化传入了context和mapList(数据源),首先是RvHolder,此ViewHolder继承自RecyclerView.ViewHolder,并实现构造方法,并获取到控件;onCreateViewHolder负责实例化每个Item的视图,所以我在onCreateViewHolder()中绑定了对应布局,onBindViewHolder()中对holder控件进行填充.getItemCount()返回条目数量.

最后是activity:

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
package com.liompei.recyclerviewdemo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private RvAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this)); //必备
adapter = new RvAdapter(this, getData());
recyclerView.setAdapter(adapter); //必备
}
private List<Map<String, String>> getData() {
List<Map<String, String>> mapList = new ArrayList<>();
for (int i = 0; i < 100; i++) {
Map<String, String> map = new HashMap<>();
map.put("name", "liompei" + i);
map.put("num", "" + i);
mapList.add(map);
}
return mapList;
}
}

recyclerView使用时,必须执行setLayoutManager和setAdapter

setLayoutManager: 设置布局管理器,
LayoutManager已知子类LinearLayoutManager,StaggeredGridLayoutManager,间接子类GridLayoutManager.参见官网介绍: 传送门

setAdapter: 设置适配器,适配器数据来自模拟的getData()方法

ok,跑一下项目:

此时RecyclerView没有分割线,查看recyclerView的方法你会发现它也没有类似ListView的item点击事件方法,也并不好看.

源码地址:
https://github.com/liompei/RecyclerViewDemo