RecyclerView使用实例

1.什么是RecyclerView

RecyclerView是一个比listview更灵活的控件,推荐用于替代listview

2.使用RecyclerView

Demo代码下载
效果图:

2.1.activity_main.xml布局

在activity_main中加入RecyclerView

1
2
3
4
5
6
7
8
9
10
11
12
13
<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/recycle_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="false" />


</RelativeLayout>

2.2.MainAcitivty

在MainActivity中对该View进行设置

1
2
3
4
5
6
7
8
9
10
mRecyclerView = (RecyclerView) findViewById(R.id.recycle_view);
//设置RecyclerView垂直滚动
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(llm);
//初始化数据
initData();
//MyAdapter是一个继承自RecyclerView.Adapter的适配器
mAdapter = new MyAdapter(list);
mRecyclerView.setAdapter(mAdapter);

2.3.MyAdapter

MyAdapter是一个继承自RecyclerView.Adapter的适配器。继承RecyelerView.Adapter需要重载三个函数:

1
2
3
public int getItemCount();//获取Adapter中数据的数量
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i);//为每个RecyelerView中的每个Item生成一个View
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i);//设置item中的数据

2.3.1.onCreateViewHolder

onCreateViewHolder返回一个RecyclerView.ViewHolder,其中封装了一个view,该view即为item的视图

1
2
3
4
5
6
7
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recycle_item_layout, null);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
view.setLayoutParams(lp);
return new PersonViewHolder(view);
}

recycle_item_layout.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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rootView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">


<TextView
android:id="@+id/tvAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00ff00" />


<TextView
android:id="@+id/tvName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff00ff" />

</LinearLayout>

这里personView是一个继承自RecyclerView.ViewHolder的类

1
2
3
4
5
6
7
8
9
10
11
12
class PersonViewHolder extends RecyclerView.ViewHolder {
public TextView tvAge;
public TextView tvName;
public int position;

public PersonViewHolder(View itemView) {
super(itemView);

tvAge = (TextView) itemView.findViewById(R.id.tvAge);
tvName = (TextView) itemView.findViewById(R.id.tvName);
}
}

2.3.2.onBindViewHolder

数据填充,为第i个item填充响应的数据。这里的personList为MyAdapter的成员函数,一个含有Person数据的List,用于保存要在各item中显示的数据。通过MyAdapter的构造函数完成初始化:

1
2
3
4
5
6
7
8
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
PersonViewHolder holder = (PersonViewHolder) viewHolder;
holder.position = i;
Person person = personList.get(i);
holder.tvAge.setText(person.getAge() + "");
holder.tvName.setText(person.getName());
}

2.4.点击item时的响应函数

可在PersonViewHolder中设置

1
2
3
4
5
6
7
8
9
10
public PersonViewHolder(View itemView) {
super(itemView);

tvAge = (TextView) itemView.findViewById(R.id.tvAge);
tvName = (TextView) itemView.findViewById(R.id.tvName);

View rootView = itemView.findViewById(R.id.rootView);
rootView.setOnClickListener(XXX);
rootView.setOnLongClickListener(XXX);
}

参考链接:
使用RecyclerView替代ListView

本文首发于http://www.miaoyunze.com/,转载请注明出处