recyclerview分页加载显示
时间: 2023-09-02 15:12:22 浏览: 103
RecyclerView 分页加载显示可以通过以下步骤实现:
1. 在 RecyclerView 中设置一个滚动监听(OnScrollListener),以便在滚动过程中检测是否需要加载更多数据。
2. 定义一个接口(OnLoadMoreListener),当 RecyclerView 滚动到底部时会调用这个接口的方法,以便在这个方法中加载更多数据。
3. 在 RecyclerView 的适配器中添加一个 Loading View,用于显示正在加载的状态。
4. 当需要加载更多数据时,显示 Loading View,并调用 OnLoadMoreListener 接口的方法来加载更多数据。
5. 加载完成后,隐藏 Loading View,并更新 RecyclerView 的数据集。
以下是一个简单的示例代码:
```java
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final int VIEW_TYPE_ITEM = 0;
private final int VIEW_TYPE_LOADING = 1;
private List<MyData> mDataList;
private OnLoadMoreListener mOnLoadMoreListener;
private boolean mIsLoading = false;
public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
mOnLoadMoreListener = onLoadMoreListener;
}
@Override
public int getItemViewType(int position) {
return mDataList.get(position) == null ? VIEW_TYPE_LOADING : VIEW_TYPE_ITEM;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == VIEW_TYPE_LOADING) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_loading, parent, false);
return new LoadingViewHolder(view);
} else {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_data, parent, false);
return new DataViewHolder(view);
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof DataViewHolder) {
MyData data = mDataList.get(position);
((DataViewHolder) holder).bindData(data);
} else if (holder instanceof LoadingViewHolder) {
((LoadingViewHolder) holder).showLoading();
if (!mIsLoading && mOnLoadMoreListener != null) {
mIsLoading = true;
mOnLoadMoreListener.onLoadMore();
}
}
}
@Override
public int getItemCount() {
return mDataList.size();
}
public void setData(List<MyData> dataList) {
mDataList = dataList;
notifyDataSetChanged();
}
public void addData(List<MyData> dataList) {
mDataList.addAll(dataList);
notifyDataSetChanged();
mIsLoading = false;
}
public static class DataViewHolder extends RecyclerView.ViewHolder {
private TextView mTextView;
public DataViewHolder(View itemView) {
super(itemView);
mTextView = itemView.findViewById(R.id.text_view);
}
public void bindData(MyData data) {
mTextView.setText(data.getText());
}
}
public static class LoadingViewHolder extends RecyclerView.ViewHolder {
private ProgressBar mProgressBar;
public LoadingViewHolder(View itemView) {
super(itemView);
mProgressBar = itemView.findViewById(R.id.progress_bar);
}
public void showLoading() {
mProgressBar.setVisibility(View.VISIBLE);
}
}
public interface OnLoadMoreListener {
void onLoadMore();
}
}
```
在 Activity 或 Fragment 中,你需要设置 RecyclerView 的 LayoutManager 和 Adapter,并在 Adapter 中设置 OnLoadMoreListener 接口。
```java
public class MyFragment extends Fragment implements MyAdapter.OnLoadMoreListener {
private RecyclerView mRecyclerView;
private MyAdapter mAdapter;
private LinearLayoutManager mLayoutManager;
private List<MyData> mDataList = new ArrayList<>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_my, container, false);
mRecyclerView = view.findViewById(R.id.recycler_view);
mAdapter = new MyAdapter();
mAdapter.setOnLoadMoreListener(this);
mLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
loadData();
return view;
}
private void loadData() {
// load data from server or database
// add the loaded data to mDataList
mAdapter.setData(mDataList);
}
@Override
public void onLoadMore() {
// load more data from server or database
// add the loaded data to mDataList
mAdapter.addData(mDataList);
}
}
```
这样,当用户滑动到 RecyclerView 的底部时,就会触发 onLoadMore() 方法来加载更多数据。同时,RecyclerView 会自动显示 Loading View 来提示用户正在加载数据。
阅读全文