代码主要包含两方面:
1、通用适配adapter;
2、对列表下拉刷新,上拉加载的封装
效果如下:
代码也包含了添加头部,自定义状态、设置下拉、上啦开关等,代码都是在别人基础上进行二次修改的,后面会给出参考文档,使用如下:
RefreshActivity.java
/**
* crate by yy on 2018-2-23
* describle:测试下拉刷新与上拉加载
*/
public class RefreshActivity extends AppCompatActivity implements xRecyclerView.OnRefreshListener {
@BindView(R.id.xrecyclerView)
xRecyclerView xrecyclerView;
RecycleAdapter recycleAdapter;
List<String> rankList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_refresh);
ButterKnife.bind(this);
for (int i = 0; i < 20; i++) {
rankList.add("正常内容" + i);
}
recycleAdapter = new RecycleAdapter(this, R.layout.energy_item, rankList);
xrecyclerView.setAdapter(this, recycleAdapter, new LinearLayoutManager(this));
xrecyclerView.setOnRefreshListener(this);
View header = LayoutInflater.from(this).inflate(R.layout.header, xrecyclerView, false);
xrecyclerView.addHeader(header);
}
@Override
public void refresh(SwipeRefreshLayout refreshLayout) {
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
rankList.clear();
for (int i = 0; i < 20; i++) {
rankList.add(0, "下拉刷新" + i);
}
runOnUiThread(new Runnable() {
@Override
public void run() {
recycleAdapter.notifyDataSetChanged();
xrecyclerView.refreshComplete(LoadingFooter.State.Normal, "");
}
});
}
}).start();
}
@Override
public void loadMore(RecyclerView recyclerView) {
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
if (rankList.size() > 40) {
xrecyclerView.refreshComplete(LoadingFooter.State.TheEnd, "");
} else {
for (int i = 0; i < 9; i++) {
rankList.add("加载更多" + i);
}
recycleAdapter.notifyDataSetChanged();
xrecyclerView.refreshComplete(LoadingFooter.State.Normal, "");
}
}
});
}
}).start();
}
}
xRecyclerView是一个继承自Linearlayout的实现类,里面包含了下拉刷新和上拉控件,实际操作中refresh 和loadmore方法里面都是调用网络加载逻辑,而我们的recycleAdapter 真的相当简单:
RecycleAdapter.java
/**
* Created by yy on 2018/2/23.
* 描述:
*/
public class RecycleAdapter extends CommonAdapter<String> {
public RecycleAdapter(Context context, int layoutId, List<String> datas) {
super(context, layoutId, datas);
}
@Override
protected void convert(ViewHolder holder, String s, int position) {
holder.setText(R.id.name, s);
holder.setImageResource(R.id.head, R.mipmap.headportrait);
}
}
在convert中拿到ViewHolder对象直接对控件进行赋值即可,如果没有的函数可以自行添加;如果是不同子布局,我们先看看 CommonAdapter就知道原理了:
/**
* Created by zhy on 16/4/9
* .
*/
public abstract class CommonAdapter<T> extends MultiItemTypeAdapter<T> {
protected Context mContext;
protected List<T> mDatas;
public CommonAdapter(final Context context, final int layoutId, List<T> datas) {
super(context, datas);
mContext = context;
mDatas = datas;
addItemViewDelegate(new ItemViewDelegate<T>() {
@Override
public int getItemViewLayoutId() {
return layoutId;
}
@Override
public boolean isForViewType(T item, int position) {
return true;
}
@Override
public void convert(ViewHolder holder, T t, int position) {
CommonAdapter.this.convert(holder, t, position);
}
});
}
protected abstract void convert(ViewHolder holder, T t, int position);
}
上述源码是张鸿洋写的,在构造函数中我们看到了这个addItemViewDelegate(),不管是单个还是多个,我们的子项都是这样添加的,那我们照样子画瓢就好,如下:
public class RecycleMultiItemAdapter extends MultiItemTypeAdapter<String> {
public RecycleMultiItemAdapter(Context context, List datas) {
super(context, datas);
addItemViewDelegate(new Item1Delegate());
addItemViewDelegate(new Item2Delegate());
}
public class Item1Delegate implements ItemViewDelegate<String> {
@Override
public int getItemViewLayoutId() {
return 0;
}
@Override
public boolean isForViewType(String item, int position) {
return false;
}
@Override
public void convert(ViewHolder holder, String s, int position) {
}
}
public class Item2Delegate implements ItemViewDelegate<String> {
@Override
public int getItemViewLayoutId() {
return 0;
}
@Override
public boolean isForViewType(String item, int position) {
return false;
}
@Override
public void convert(ViewHolder holder, String s, int position) {
}
}
}
然后根据我们bean提供不同类型的判断标准来确定加载那个delegate,其实在原生的android中是根据getItemViewType方法来返回不同类型的,然后再 onCreateViewHolder中返回不同的ViewHolder对象来完成,上面对它进行了封装。
关于xrecycleView的实现可以下载源码研究研究,除了上面的OnRefreshListener接口,我还根据功能不同分离了几个接口出来:
/**
* 聊天加载接口
*/
public interface ChartRefreshListener {
void refresh(SwipeRefreshLayout refreshLayout, RecyclerView recyclerView);
}
private ChartRefreshListener chartRefreshListener;
public void setchartRefreshListener(ChartRefreshListener chartRefreshListener) {
this.chartRefreshListener = chartRefreshListener;
}
/**
* 滚动监听接口
*/
public interface OnScrollListener {
void onScrollDistance(RecyclerView recyclerView, int distanceY);
void onScrolled(RecyclerView recyclerView, int dx, int dy);
}
private OnScrollListener onScrollRecyclerListener;
public void setOnScrollListener(OnScrollListener onScrollRecyclerListener) {
this.onScrollRecyclerListener = onScrollRecyclerListener;
}
/**
* 正常刷新接口
*/
public interface OnRefreshListener {
void refresh(SwipeRefreshLayout refreshLayout);
void loadMore(RecyclerView recyclerView);
}
private OnRefreshListener onRefreshListener;
public void setOnRefreshListener(OnRefreshListener onRefreshListener) {
this.onRefreshListener = onRefreshListener;
}
这个案例已合并到UIdemo项目中,
详情github:https://github.com/yangyong915/UIDemo