BaseRecyclerViewAdapterHelper万能适
2020-06-24 本文已影响0人
A然后呢
适配器
public class Adapter_rec1 extends BaseQuickAdapter<DataBean,BaseViewHolder> {
private Context context;
public Adapter_rec1(Context context, @Nullable List<DataBean> data) {
super(R.layout.layout_item1, data);
this.context=context;
}
@Override
protected void convert(BaseViewHolder helper, DataBean item) {
helper.setText(R.id.MyTV1,item.getTitle()); //通过id
ImageView view = helper.getView(R.id.IMage1);
Glide.with(context).load(item.getImage()).into(view);
helper.addOnClickListener(R.id.IMage1); //添加子元素点击事件
}
}
绑定适配器,监听点击事件
adapter_rec1 = new Adapter_rec1(getActivity(), arrayList);
mMyRec.setLayoutManager(new GridLayoutManager(getActivity(),2));
mMyRec.setAdapter(adapter_rec1);
adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//这个是整个item的点击事件,我们可以在这里做一些操作
Toast.makeText(MainActivity.this,"我是第"+page+"页的第" + position + "位置",Toast.LENGTH_SHORT).show();
}
});
adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
@Override
public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
//看清楚这个是某一个控件的点击方法,不要搞混
Toast.makeText(MainActivity.this,"我是控件第"+page+"页的第" + position + "位置",Toast.LENGTH_SHORT).show();
}
});
上拉加载更多
//上拉刷新
adapter_rec1.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
@Override
public void onLoadMoreRequested() {
adapter_rec1.loadMoreComplete(); //新数据加入完成
adapter_rec1.loadMoreEnd(); //没有更多数据了
adapter_rec1.loadMoreFail(); //加载失败,点击重试
}
});
下拉刷新加入下拉刷新控件SwipeRefreshLayout
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.SwipeRefreshLayout>
refreshLayout=findViewById(R.id.swipe_refresh);
refreshLayout.setColorSchemeResources(R.color.colorAccent);//这个方法是设置SwipeRefreshLayout刷新圈颜色
下拉刷新实现
View view= LayoutInflater.from(MainActivity.this)
.inflate(R.layout.headview_layout,null);
adapter.addHeaderView(view);//这是设置头部的方法,一个方法搞定,不需要跟以前一样去适配器里面判断一大堆,很方便
// adapter.addFooterView();有头当然有尾也是一个方法搞定
//开启动画(默认为渐显效果)
adapter.openLoadAnimation();
//这个方法有下面五个显示动画
// adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
// public static final int ALPHAIN = 0x00000001;
// public static final int SCALEIN = 0x00000002;
// public static final int SLIDEIN_BOTTOM = 0x00000003;
// public static final int SLIDEIN_LEFT = 0x00000004;
// public static final int SLIDEIN_RIGHT = 0x00000005;
//自动触发下拉刷新
recyclerView.postDelayed(new Runnable() {
@Override
public void run() {
//这个方法是让一进入页面的时候实现网络请求,有个缓冲的效果
refreshLayout.setRefreshing(true);
//模拟一下网络请求
page=1;//page为页数
loadDate(page);
}
}, 2000);
//手动刷新 触发SwipeRefreshLayout的下拉刷新方法
refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
isLoadMore = false;
handler.postDelayed(new Runnable() {
@Override
public void run() {
//模拟一下网络请求
page=1;
loadDate(page);
}
}, 2000);
}
});