我们已经把首页列表的
Item
布局完成,接下来我们就开始加载首页数据,我们这一篇主要是做封装,具体网络请求放在下一篇
引入依赖
//刷新分页组件
api 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0'
api 'com.scwang.smartrefresh:SmartRefreshHeader:1.1.0'
//viewmodel and livedata
api 'androidx.lifecycle:lifecycle-extensions:2.1.0'
//paging分页组件
api 'androidx.paging:paging-runtime:2.1.0'
我们首页有下拉刷新和上拉加载,我们使用
SmartRefreshLayout
实现,分页使用paging
,viewmodel
和livedata
我们导入了一个库lifecycle-extensions
,当然你也可以分开导入
封装刷新组件
我们知道列表在加载的时候,如果没有数据,我们一般会展示一个空的布局,所以我们先自定义一个
EmptyView
,布局很简单,
然后我们创建一个
EmptyView
来加载这个xml,同时添加几个set
方法
public class EmptyView extends LinearLayout {
private ImageView icon;
private TextView title;
private Button action;
public EmptyView(@NonNull Context context) {
this(context, null);
}
public EmptyView(@NonNull Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public EmptyView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}
public EmptyView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int style) {
super(context, attrs, defStyleAttr, style);
setOrientation(VERTICAL);
setGravity(Gravity.CENTER);
LayoutInflater.from(context).inflate(R.layout.layout_empty_view, this, true);
icon = findViewById(R.id.empty_icon);
title = findViewById(R.id.empty_text);
action = findViewById(R.id.empty_action);
}
public void setEmptyIcon(@DrawableRes int iconRes) {
icon.setImageResource(iconRes);
}
public void setTitle(String text) {
if (TextUtils.isEmpty(text)) {
title.setVisibility(GONE);
} else {
title.setText(text);
title.setVisibility(VISIBLE);
}
}
public void setButton(String text, OnClickListener listener) {
if (TextUtils.isEmpty(text)) {
action.setVisibility(GONE);
} else {
action.setText(text);
action.setVisibility(VISIBLE);
action.setOnClickListener(listener);
}
}
}
接着就是编写我们的刷新布局了,由三部分组成,从上往下分别是头部刷新布局,列表,底部刷新布局,
SmartRefreshLayout
都给我们提供了,我们只需要拿来直接使用即可
封装列表Fragment
接下来我们就封装一个
AbsListFragment
,这个是所有列表数据Fragment
的基类,里面包括上拉加载/下拉刷新等操作,
public abstract class AbsListFragment extends Fragment implements OnRefreshListener, OnLoadMoreListener {
private LayoutRefreshViewBinding mBinding;
protected RecyclerView mRecyclerView;
protected SmartRefreshLayout mRefreshLayout;
protected EmptyView mEmptyView;
protected PagedListAdapter mAdapter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mBinding = LayoutRefreshViewBinding.inflate(inflater, container, false);
mRecyclerView = mBinding.recyclerView;
mRefreshLayout = mBinding.refreshLayout;
mEmptyView = mBinding.emptyView;
mRefreshLayout.setEnableRefresh(true);
mRefreshLayout.setEnableLoadMore(true);
mRefreshLayout.setOnRefreshListener(this);
mRefreshLayout.setOnLoadMoreListener(this);
mAdapter = getAdapter();
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
mRecyclerView.setItemAnimator(null);
//默认给列表中的Item 一个 10dp的ItemDecoration
DividerItemDecoration decoration = new DividerItemDecoration(getContext(), LinearLayoutManager.VERTICAL);
decoration.setDrawable(ContextCompat.getDrawable(getContext(), R.drawable.list_divider));
mRecyclerView.addItemDecoration(decoration);
return mBinding.getRoot();
}
@Override
public void onRefresh(@NonNull RefreshLayout refreshLayout) {
}
@Override
public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
}
public abstract PagedListAdapter getAdapter();
}
我们先设置一些基本属性方法,包括上拉加载监听,下拉刷新监听,
RecyclerView
设置适配器等不管是下拉刷新还是上拉加载都有回调,我们把这些回调的处理也写在这个
Fragment
中,方便我们后续使用
public void finishRefresh(boolean hasData) {
//当前列表数据
PagedList currentList = mAdapter.getCurrentList();
//是否有数据
hasData = hasData || currentList != null && currentList.size() > 0;
RefreshState state = mRefreshLayout.getState();
if (state.isFooter && state.isOpening) {
mRefreshLayout.finishLoadMore();
} else if (state.isHeader && state.isOpening) {
mRefreshLayout.finishRefresh();
}
if (hasData) {
mEmptyView.setVisibility(View.GONE);
} else {
mEmptyView.setVisibility(View.VISIBLE);
}
}
我们这里还需要把数据展示在我们的列表上,通过
PagedListAdapter
的submitList
方法可以实现
public void submitList(PagedList pagedList) {
if (pagedList.size() > 0) {
mAdapter.submitList(pagedList);
}
finishRefresh(pagedList.size()>0);
}
OK,到目前我们这个
AbsListFragment
基本写完了,接下来我们让我们的HomeFragment
实现它即可原创文章 92获赞 320访问量 149万+ 关注 他的留言板 展开阅读全文
作者:Greathfs