关于recyclerview
recyclerview在android 5.0以来被引入,以前经常使用的listview 继承的是abslistview,而recyclerview则直接继承 viewgroup,并实现了scrollingview 和 nestedscrollingchild接口,recyclerview相比listview,是一次彻底的改变。
recyclerview比listview更先进更灵活,对于很多的视图它就是一个容器,可以有效的重用和滚动。当数据动态变化的时候请使用它。
recyclerview使用起来很方便因为它提供:
1、它为item的定位提供一个layoutmanager
2、为item的操作提供一个缺省的animations
3、还可以灵活地定义这个小部件的自定义布局管理器和动画
实现上拉刷新和下拉刷新布局文件:
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/swipe_refresh_widget"
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cachecolorhint="@null"
android:scrollbars="vertical" />
在activity中引用这个布局并初始化
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
mswiperefreshwidget = (swiperefreshlayout) findviewbyid(r.id.swipe_refresh_widget);
mrecyclerview = (recyclerview) findviewbyid(android.r.id.list);
mswiperefreshwidget.setcolorscheme(r.color.color1, r.color.color2,
r.color.color3, r.color.color4);
mswiperefreshwidget.setonrefreshlistener(this);
// 这句话是为了,第一次进入页面的时候显示加载进度条
mswiperefreshwidget.setprogressviewoffset(false, 0, (int) typedvalue
.applydimension(typedvalue.complex_unit_dip, 24, getresources()
.getdisplaymetrics()));
mrecyclerview.setonscrolllistener(new recyclerview.onscrolllistener() {
@override
public void onscrollstatechanged(recyclerview recyclerview,
int newstate) {
super.onscrollstatechanged(recyclerview, newstate);
if (newstate == recyclerview.scroll_state_idle
&& lastvisibleitem + 1 == adapter.getitemcount()) {
mswiperefreshwidget.setrefreshing(true);
// 此处在现实项目中,请换成网络请求数据代码,sendrequest .....
handler.sendemptymessagedelayed(0, 3000);
}
}
@override
public void onscrolled(recyclerview recyclerview, int dx, int dy) {
super.onscrolled(recyclerview, dx, dy);
lastvisibleitem = mlayoutmanager.findlastvisibleitemposition();
}
});
mrecyclerview.sethasfixedsize(true);
mlayoutmanager = new linearlayoutmanager(this);
mrecyclerview.setlayoutmanager(mlayoutmanager);
mrecyclerview.setitemanimator(new defaultitemanimator());
adapter = new sampleadapter();
mrecyclerview.setadapter(adapter);
// 此处在现实项目中,请换成网络请求数据代码,sendrequest .....
handler.sendemptymessagedelayed(0, 3000);
}
swiperefreshlayout里面需要注意的api:
1、setonrefreshlistener(onrefreshlistener listener) 设置下拉监听,当用户下拉的时候会去执行回调
2、setcolorschemecolors(int... colors) 设置 进度条的颜色变化,最多可以设置4种颜色
3、setprogressviewoffset(boolean scale, int start, int end) 调整进度条距离屏幕顶部的距离
4、setrefreshing(boolean refreshing) 设置swiperefreshlayout当前是否处于刷新状态,一般是在请求数据的时候设置为true,在数据被加载到view中后,设置为false。
recyclerview的实现:
第一种,下拉刷新和上拉刷新都用swiperefreshlayout 自带的进度条
mrecyclerview = (recyclerview) findviewbyid(android.r.id.list);
mrecyclerview.setonscrolllistener(new recyclerview.onscrolllistener() {
@override
public void onscrollstatechanged(recyclerview recyclerview,
int newstate) {
super.onscrollstatechanged(recyclerview, newstate);
if (newstate == recyclerview.scroll_state_idle
&& lastvisibleitem + 1 == adapter.getitemcount()) {
mswiperefreshwidget.setrefreshing(true);
// 此处在现实项目中,请换成网络请求数据代码,sendrequest .....
handler.sendemptymessagedelayed(0, 3000);
}
}
@override
public void onscrolled(recyclerview recyclerview, int dx, int dy) {
super.onscrolled(recyclerview, dx, dy);
lastvisibleitem = mlayoutmanager.findlastvisibleitemposition();
}
});
mrecyclerview.sethasfixedsize(true);
mlayoutmanager = new linearlayoutmanager(this);
mrecyclerview.setlayoutmanager(mlayoutmanager);
mrecyclerview.setitemanimator(new defaultitemanimator());
adapter = new sampleadapter();
mrecyclerview.setadapter(adapter);
第二种实现下拉刷新用swiperefreshlayout 自带的进度条, 上拉刷新用类似listview的刷新 提示“加载中”等信息。
我们可以给recyclerview 也添加一个类似footerview的item。
我们在adapter中实现:
public class sampleadapter extends recyclerview.adapter {
private list list;
private static final int type_item = 0;
private static final int type_footer = 1;
public list getlist() {
return list;
}
public sampleadapter() {
list = new arraylist();
}
// recyclerview的count设置为数据总条数+ 1(footerview)
@override
public int getitemcount() {
return list.size() + 1;
}
@override
public int getitemviewtype(int position) {
// 最后一个item设置为footerview
if (position + 1 == getitemcount()) {
return type_footer;
} else {
return type_item;
}
}
@override
public void onbindviewholder(viewholder holder, final int position) {
if (holder instanceof itemviewholder) {
((itemviewholder) holder).textview.settext(string.valueof(list
.get(position)));
}
}
@override
public viewholder oncreateviewholder(viewgroup parent, int viewtype) {
if (viewtype == type_item) {
view view = layoutinflater.from(parent.getcontext()).inflate(
r.layout.list_item_text, null);
view.setlayoutparams(new layoutparams(layoutparams.match_parent,
layoutparams.wrap_content));
return new itemviewholder(view);
}
// type == type_footer 返回footerview
else if (viewtype == type_footer) {
view view = layoutinflater.from(parent.getcontext()).inflate(
r.layout.footerview, null);
view.setlayoutparams(new layoutparams(layoutparams.match_parent,
layoutparams.wrap_content));
return new footerviewholder(view);
}
return null;
}
class footerviewholder extends viewholder {
public footerviewholder(view view) {
super(view);
}
}
class itemviewholder extends viewholder {
textview textview;
public itemviewholder(view view) {
super(view);
textview = (textview) view.findviewbyid(r.id.text);
}
}
}
这样我们就可以针对footerview的布局做一些处理了,比如提示“加载中,”,“已经全部加载”等信息。更加灵活一点