#RecyclerView & RecyclerView.Adapter封装
开源项目:https://github.com/open-android/BaseRecyclerAndAdapter
- 爱生活,爱学习,更爱做代码的搬运工,分类查找更方便请下载黑马助手app
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
compile 'com.github.open-android:BaseRecyclerAndAdapter:0.5.13'
compile 'com.jakewharton:butterknife:8.4.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
- 网格RecyclerView(app:spanCount="2",spanCount取值范围[1,10])
<org.itheima.recycler.widget.ItheimaRecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:spanCount="2"/>
- 垂直滚动RecyclerView
<org.itheima.recycler.widget.ItheimaRecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
- 横向滚动RecyclerView(11:一行,12:二行.......,spanCount取值范围[11,19])
<org.itheima.recycler.widget.ItheimaRecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:spanCount="12"/>
- xml代码
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<org.itheima.recycler.widget.ItheimaRecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<org.itheima.recycler.header.RecyclerViewHeader
android:id="@+id/recycler_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|top">
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="header"/>
</org.itheima.recycler.header.RecyclerViewHeader>
</FrameLayout>
- java代码
RecyclerViewHeader header = (RecyclerViewHeader) findViewById(R.id.header);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
header.attachTo(recyclerView);
ItemClickSupport itemClickSupport = new ItemClickSupport(mRecyclerView);
//点击事件
itemClickSupport.setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
@Override
public void onItemClicked(RecyclerView recyclerView, int position, View v) {
Toast.makeText(recyclerView.getContext(), "我被点击了", Toast.LENGTH_SHORT).show();
}
});
//长按事件
itemClickSupport.setOnItemLongClickListener(new ItemClickSupport.OnItemLongClickListener() {
@Override
public boolean onItemLongClicked(RecyclerView recyclerView, int position, View v) {
Toast.makeText(recyclerView.getContext(), "我被长按了", Toast.LENGTH_SHORT).show();
return false;
}
});
adapter = new BaseRecyclerAdapter(recyclerView
, MyRecyclerViewHolder.class
, R.layout.item_reyclerview
, datas);
//@param recyclerView
//@param viewHolderClazz
//@param itemResId recyclerView条目的资源id
//@param datas recyclerView展示的数据集合(可以传null)
public static class MyRecyclerViewHolder extends BaseRecyclerViewHolder<DataBean> {
//换成你布局文件中的id
@BindView(R.id.tv_title)
TextView tvTitle;
public MyRecyclerViewHolder(ViewGroup parentView, int itemResId) {
super(parentView, itemResId);
}
/**
* 绑定数据的方法,在mData获取数据(mData声明在基类中)
*/
@Override
public void onBindRealData() {
tvTitle.setText(mData.title);
}
/**
* 给按钮添加点击事件(button改成你要添加点击事件的id)
* @param v
*/
@OnClick(R.id.button)
public void click(View v) {
}
}
- http请求初始化
ItheimaHttp.init(context, baseUrl);//使用前必须调用(如果调用过则不需要重复调用)
- xml布局
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<org.itheima.recycler.widget.ItheimaRecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>
- java代码实现布局
pullToLoadMoreRecyclerView = new PullToLoadMoreRecyclerView<Bean>(mSwipeRefreshLayout, mRecyclerView, MyRecyclerViewHolder.class) {
@Override
public int getItemResId() {
//recylerview item资源id
return R.layout.item_recylerview;
}
@Override
public String getApi() {
//接口
return "order/list";
}
//是否加载更多的数据,根据业务逻辑自行判断,true表示有更多的数据,false表示没有更多的数据,如果不需要监听可以不重写该方法
@Override
public boolean isMoreData(BaseLoadMoreRecyclerAdapter.LoadMoreViewHolder holder) {
System.out.println("isMoreData" + holder);
return true;
}
};
- 设置监听
pullToLoadMoreRecyclerView.setLoadingDataListener(new PullToLoadMoreRecyclerView.LoadingDataListener<Bean>() {
@Override
public void onRefresh() {
//监听下啦刷新,如果不需要监听可以不重新该方法
L.i("setLoadingDataListener onRefresh");
}
@Override
public void onStart() {
//监听http请求开始,如果不需要监听可以不重新该方法
L.i("setLoadingDataListener onStart");
}
@Override
public void onSuccess(Bean o ,,Headers headers) {
//监听http请求成功,如果不需要监听可以不重新该方法
L.i("setLoadingDataListener onSuccess: " + o);
}
@Override
public void onFailure() {
//监听http请求失败,如果不需要监听可以不重新该方法
L.i("setLoadingDataListener onFailure");
}
});
//添加头
pullToLoadMoreRecyclerView.putHeader(key,value);
//添加请求参数
pullToLoadMoreRecyclerView.putParam(key, value);
//开始请求
pullToLoadMoreRecyclerView.requestData();
//控制加状态,可以在监听中处理
loadMoreViewHolder.loading("加载中...");//默认文字:"加载中..."
loadMoreViewHolder.loadingFinish("没有更多数据");
//如果使用数字控制页数可以使用如下api
//设置参数的key
setCurPageKey("curPage");//当前页key
setPageSizeKey("pageSize");//每一页数量的key
//设置参数的值
setPageSize(1);//设置每一页数量
setTotalPage(20);//设置一共有多少页
//如果使用其他方式分页
可以设置setLoadingDataListener在onSuccess(Bean o)回调中处理分页
onSuccess回调在每一次加载数据成功后回调
class MyTypeAdapter extends BaseRecyclerAdapter {
private final int ITEM_TYPE_1 = 0;
private final int ITEM_TYPE_2 = 1;
public MyTypeAdapter(RecyclerView recyclerView, List datas) {
super(recyclerView, null, 0, datas);
}
@Override
public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//选择类型
if (viewType == ITEM_TYPE_2) {
return new MyTypeHolder2(parent, R.layout.item_type2);
}
return new MyTypeHolder1(parent, R.layout.item_type1);
}
@Override
public int getItemViewType(int position) {
//根据position返回类型
return position % 2 == 0 ? ITEM_TYPE_2 : ITEM_TYPE_1;
}
}
//@param isLoadMore 数据是否累加
//@param datas List数据
adapter.addDatas(true,datas);
adapter.clearAllData();
详细的使用方法在DEMO里面都演示啦,如果你觉得这个库还不错,请赏我一颗star吧~~~
欢迎关注微信公众号