RecyclerView的使用之多种Item加载布局

时间:2022-01-13 08:10:53

本文给大家介石介绍下如何利用recyclerview实现多item布局的加载,多item布局的加载的意思就是在开发过程中list的每一项可能根据需求的不同会加载不同的layout。

下面给大家展示下演示效果图:

 RecyclerView的使用之多种Item加载布局

* 图片资源版权归属于facebook dribbble

recyclerview实现加载不同的layout的核心就是在adapter的oncreateviewholder里面去根据需求而加载不同的布局。

具体的实现步骤:(以android studio作为开发工具)

1:gradle配置 build.gradle

这里cardview也是一种新的布局容器,上一篇有介绍。

compile 'com.android.support:recyclerview-v7:23.1.1'
compile 'com.android.support:cardview-v7:23.1.1'

2:建立列表的布局 activity_recyclerview.xml

?
1
2
3
4
5
6
7
8
9
10
11
<?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="match_parent">
<android.support.v7.widget.recyclerview
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rv_list"
/>
</linearlayout>

由于需要多种item layout的加载,我们需要建立2个item布局

3:建立列表item项的布局(1) item1.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
29
30
31
32
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.cardview
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:id="@+id/cv_item"
android:foreground="?android:attr/selectableitembackground"
card_view:cardcornerradius="4dp"
card_view:cardbackgroundcolor="#ffffff"
card_view:cardelevation="4dp"
>
<linearlayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<imageview
android:id="@+id/iv_item1_pic"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_weight="1"
android:background="@mipmap/lighthouse"
/>
<textview
android:id="@+id/tv_item1_text"
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</linearlayout>
</android.support.v7.widget.cardview>

4:建立列表item项的布局(2) item2.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
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.cardview
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:foreground="?android:attr/selectableitembackground"
card_view:cardcornerradius="4dp"
card_view:cardbackgroundcolor="#e040fb"
card_view:cardelevation="4dp"
>
<linearlayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<textview
android:id="@+id/tv_item2_text"
android:padding="20dp"
android:textcolor="#ffffff"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</linearlayout>
</android.support.v7.widget.cardview>

*最重要的部分 adapter

5:建立recyclerview的adapter,recyclerviewadapter.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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;
/**
* created by lijizhou on 2016/2/21.
*/
public class recyclerviewadapter extends recyclerview.adapter<recyclerview.viewholder> {
private layoutinflater mlayoutinflater;
private context context;
private string [] titles;
//建立枚举 2个item 类型
public enum item_type {
item1,
item2
}
public recyclerviewadapter(context context,string[] titles){
this.titles = titles;
this.context = context;
mlayoutinflater = layoutinflater.from(context);
}
@override
public recyclerview.viewholder oncreateviewholder(viewgroup parent, int viewtype) {
//加载item view的时候根据不同type加载不同的布局
if (viewtype == item_type.item1.ordinal()) {
return new item1viewholder(mlayoutinflater.inflate(r.layout.item1, parent, false));
} else {
return new item2viewholder(mlayoutinflater.inflate(r.layout.item2, parent, false));
}
}
@override
public void onbindviewholder(recyclerview.viewholder holder, int position) {
if (holder instanceof item1viewholder) {
((item1viewholder) holder).mtextview.settext(titles[position]);
} else if (holder instanceof item2viewholder) {
((item2viewholder) holder).mtextview.settext(titles[position]);
}
}
//设置item类型,可以*发挥,这里设置item position单数显示item1 偶数显示item2
@override
public int getitemviewtype(int position) {
//enum类提供了一个ordinal()方法,返回枚举类型的序数,这里item_type.item1.ordinal()代表0, item_type.item2.ordinal()代表1
return position % 2 == 0 ? item_type.item1.ordinal() : item_type.item2.ordinal();
}
@override
public int getitemcount() {
return titles == null ? 0 : titles.length;
}
//item1 的viewholder
public static class item1viewholder extends recyclerview.viewholder{
textview mtextview;
public item1viewholder(view itemview) {
super(itemview);
mtextview=(textview)itemview.findviewbyid(r.id.tv_item1_text);
}
}
//item2 的viewholder
public static class item2viewholder extends recyclerview.viewholder{
textview mtextview;
public item2viewholder(view itemview) {
super(itemview);
mtextview=(textview)itemview.findviewbyid(r.id.tv_item2_text);
}
}
}

ok,adapter建立好了,那么最后一步就是在activity里面进行相关操作

6:列表页面的类文件 recyclerviewactivity.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
import android.os.bundle;
import android.support.v7.app.appcompatactivity;
import android.support.v7.widget.gridlayoutmanager;
import android.support.v7.widget.linearlayoutmanager;
import android.support.v7.widget.recyclerview;
/**
* created by lijizhou on 2016/2/21.
*/
public class recyclerviewactivity extends appcompatactivity {
private recyclerview mrecyclerview;
//item 显示所需(仅供demo)
private string[] title = {"blog : http://blog.csdn.net/leejizhou.",
"a good laugh and a long sleep are the best cures in the doctor's book.",
"all or nothing, now or never ",
"be nice to people on the way up, because you'll need them on your way down.",
"be confident with yourself and stop worrying what other people think. do what's best for your future happiness!",
"blessed is he whose fame does not outshine his truth.",
"create good memories today, so that you can have a good past"
};
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_recyclerview);
mrecyclerview=(recyclerview)findviewbyid(r.id.rv_list);
//这里根据上一个页面的传入值来加载list或grid,上一个页面仅仅2个按钮,参考演示demo
if (getintent().getintextra("type", 0) == 1){
//list
linearlayoutmanager layoutmanager = new linearlayoutmanager(this);
layoutmanager.setorientation(linearlayoutmanager.vertical);
mrecyclerview.setlayoutmanager(layoutmanager);
}else if(getintent().getintextra("type", 0) == 2){
//grid
mrecyclerview.setlayoutmanager(new gridlayoutmanager(this, 2));
}
//recyclerview设置adapter
mrecyclerview.setadapter(new recyclerviewadapter(this, title));
}
}

ok,这样recyclerview的多item布局的加载就实现,关于recyclerview的使用之多种item加载布局就给大家介绍这么多,希望对大家有所帮助!