public class SmartRecyclerView extends FrameLayout {
public static final int VIEW_REFRESH_ERROR = 0;
public static final int VIEW_LOAD_MORE_ERROR = 1;
private Context mContext;
private SwipeRefreshLayout mSwipeRefreshLayout;
private RecyclerView mRecyclerView;
private BaseQuickAdapter mAdapter;
private View mEmptyView;
private View mErrorView;
private int mEmptyLayoutId;
private int mErrorLayoutId;
private LayoutInflater mInflater;
public SmartRecyclerView(@NonNull Context context) {
this(context, null);
}
public SmartRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
this.mContext = context;
initWidget();
mInflater = LayoutInflater.from(context);
TypedArray ta = mContext.obtainStyledAttributes(attrs, R.styleable.common_SmartRecyclerView);
mEmptyLayoutId = ta.getResourceId(R.styleable.common_SmartRecyclerView_common_empty_layout, -1);
mErrorLayoutId = ta.getResourceId(R.styleable.common_SmartRecyclerView_common_error_layout, -1);
ta.recycle();
}
private void initWidget() {
View smartRecyclerView = LayoutInflater.from(mContext).inflate(R.layout.common_smart_recyclerview_layout, null);
addView(smartRecyclerView, new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
mSwipeRefreshLayout = smartRecyclerView.findViewById(R.id.swipe_refresh);
mRecyclerView = smartRecyclerView.findViewById(R.id.recycler_view);
}
public void setAdapter(BaseQuickAdapter adapter) {
adapter.bindToRecyclerView(mRecyclerView);
this.mAdapter = adapter;
}
public void setEmptyView(View emptyView) {
mEmptyView = emptyView;
}
public void setErrorView(View errorView) {
setErrorView(errorView, null);
}
/**
* 刷新失败view添加点击事件
*
* @param errorView
* @param onClickListener
*/
public void setErrorView(View errorView, OnClickListener onClickListener) {
mErrorView = errorView;
mErrorView.setOnClickListener(onClickListener);
}
public void setOnLoadmoreListener(RequestLoadMoreListener listener) {
mAdapter.setOnLoadMoreListener(listener, mRecyclerView);
}
public void setOnRefreshListener(OnRefreshListener listener) {
mSwipeRefreshLayout.setOnRefreshListener(listener);
}
/**
* 刷新成功
*
* @param data
*/
public void finishRefresh(@Nullable List data, boolean success) {
mAdapter.setNewData(data);
if (mSwipeRefreshLayout.isRefreshing()) mSwipeRefreshLayout.setRefreshing(false);
if (success) {
if (CollectionUtil.isEmpty(data) & mEmptyView != null) {
mAdapter.setEmptyView(mEmptyView);
}
} else {
if (mErrorView != null) {
mAdapter.setEmptyView(mErrorView);
}
}
}
...
/**
* view的点击事件
*
* @param viewType view类型,目前就定义这两种:刷新失败的view;加载更多失败的view
* @param listener
*/
public void setLoadViewClickListener(int viewType, OnClickListener listener) {
if (viewType == VIEW_REFRESH_ERROR) {
//刷新失败点击事件
if (mErrorView != null) {
mErrorView.setOnClickListener(listener);
}
} else if (viewType == VIEW_LOAD_MORE_ERROR) {
//加载更多失败点击事件
LoadMoreView loadMoreView = mAdapter.getLoadMoreView();
if (loadMoreView != null && loadMoreView instanceof SimpleLoadMoreView) {
((SimpleLoadMoreView) loadMoreView).setLoadFailClickListener(listener);
}
}
}
public void setHasFixedSize(boolean hasFixedSize) {
mRecyclerView.setHasFixedSize(hasFixedSize);
}
public void setLayoutManager(RecyclerView.LayoutManager layout) {
mRecyclerView.setLayoutManager(layout);
if (layout != null) {
//inflate要求recyclerView先设定LayoutManager
if (mEmptyLayoutId > -1) {
mEmptyView = mInflater.inflate(mEmptyLayoutId, mRecyclerView, false);
}
if (mErrorLayoutId > -1) {
mErrorView = mInflater.inflate(mErrorLayoutId, mRecyclerView, false);
}
}
}
public void addItemDecoration(RecyclerView.ItemDecoration decor) {
mRecyclerView.addItemDecoration(decor);
}
/**
* 加载结束,默认加底部加载完成view
* 刷新,加载更多的时候,列表全部加载完成
*/
public void loadEnd() {
loadEnd(true);
}
/**
* 加载结束
*
* @param gone 是否显示加载完成的底部view
*/
public void loadEnd(boolean gone) {
if (mSwipeRefreshLayout.isRefreshing()) mSwipeRefreshLayout.setRefreshing(false);
if (mAdapter != null) {
mAdapter.loadMoreEnd(gone);
}
}
/**
* 加载更多结束
*
* @param success 加载更多是否成功
*/
public void loadMoreComplete(boolean success) {
if (mSwipeRefreshLayout.isRefreshing()) mSwipeRefreshLayout.setRefreshing(false);
if (mAdapter != null) {
mAdapter.loadMoreComplete(success);
}
}
attrs.xml中
使用:
smartRefresh.setHasFixedSize(true);
smartRefresh.setLayoutManager(new LinearLayoutManager(this));
smartRefresh.addItemDecoration(new SimpleSpaceItemDecoration(UiUtil.dip2px(this, 1), RecyclerView.VERTICAL));
smartRefresh.setLoadViewClickListener(VIEW_REFRESH_ERROR, new View.OnClickListener() {
@Override
public void onClick(View view) {
//刷新失败view点击事件
refresh();
}
});
mList = new ArrayList<>();
mList.add("1");
mList.add("2");
mList.add("3");
mList.add("4");
mAdapter = new MyAdapter(R.layout.xxx_item, mList);
smartRefresh.setAdapter(mAdapter);
private void initRefresh() {
smartRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
refresh();
}
});
}
private void refresh() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
int random = new Random().nextInt(10);
if (!CollectionUtil.isEmpty(mList) && mList.size() < 6) {
mList.add("a");
}
if (random % 3 == 0) {
smartRefresh.finishRefresh(mList, true);
ToastUtil.showToast(ListActivity.this, " == " + random);
} else if (random % 3 == 1) {
smartRefresh.finishRefresh(null, false);
ToastUtil.showToast(ListActivity.this, " 加载失败 == " + random);
} else if (random % 3 == 2) {
smartRefresh.finishRefresh(new ArrayList(), true);
ToastUtil.showToast(ListActivity.this, " 无数据 == " + random);
}
}
}, 1200);
}