android 列表无限滚动
时间: 2023-07-22 20:21:20 浏览: 164
实现 Android 列表无限滚动的方法如下:
1. 实现一个继承自 RecyclerView.Adapter 的适配器类,重写 getItemViewType() 方法,根据当前位置返回不同的 ViewType。
2. 在 RecyclerView 的 LayoutManager 中重写 generateDefaultLayoutParams() 方法,设置高度为 WRAP_CONTENT。
3. 在 RecyclerView 的 OnScrollListener 中监听滚动事件,当滑动到列表底部时,加载更多数据并刷新列表。
具体代码实现可以参考以下示例:
```
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int TYPE_ITEM = 0;
private static final int TYPE_LOADING = 1;
private List<Item> mItemList;
private boolean mIsLoading = false;
public MyAdapter(List<Item> itemList) {
mItemList = itemList;
}
@Override
public int getItemViewType(int position) {
return position < mItemList.size() ? TYPE_ITEM : TYPE_LOADING;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_ITEM) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);
return new ItemViewHolder(view);
} else {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.loading_layout, parent, false);
return new LoadingViewHolder(view);
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof ItemViewHolder) {
ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
Item item = mItemList.get(position);
// 绑定数据到 ItemViewHolder
} else {
LoadingViewHolder loadingViewHolder = (LoadingViewHolder) holder;
// 显示正在加载的视图
}
}
@Override
public int getItemCount() {
return mItemList.size() + (mIsLoading ? 1 : 0);
}
public void setLoading(boolean isLoading) {
mIsLoading = isLoading;
if (isLoading) {
notifyItemInserted(getItemCount() - 1);
} else {
notifyItemRemoved(getItemCount() - 1);
}
}
public static class ItemViewHolder extends RecyclerView.ViewHolder {
// ItemViewHolder 中的视图
public ItemViewHolder(View itemView) {
super(itemView);
// 初始化 ItemViewHolder 中的视图
}
}
public static class LoadingViewHolder extends RecyclerView.ViewHolder {
// LoadingViewHolder 中的视图
public LoadingViewHolder(View itemView) {
super(itemView);
// 初始化 LoadingViewHolder 中的视图
}
}
}
public class MyLayoutManager extends LinearLayoutManager {
private static final int VISIBLE_THRESHOLD = 5;
private boolean mIsLoading = false;
private OnLoadMoreListener mOnLoadMoreListener;
public MyLayoutManager(Context context) {
super(context);
}
public MyLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
public void setOnLoadMoreListener(OnLoadMoreListener listener) {
mOnLoadMoreListener = listener;
}
public void setLoading(boolean isLoading) {
mIsLoading = isLoading;
}
@Override
public RecyclerView.LayoutParams generateDefaultLayoutParams() {
return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
@Override
public void onScrolled(int dx, int dy) {
super.onScrolled(dx, dy);
if (!mIsLoading && getItemCount() - getChildCount() <= findFirstVisibleItemPosition() + VISIBLE_THRESHOLD) {
setLoading(true);
if (mOnLoadMoreListener != null) {
mOnLoadMoreListener.onLoadMore();
}
}
}
public interface OnLoadMoreListener {
void onLoadMore();
}
}
public class MyActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private MyAdapter mAdapter;
private MyLayoutManager mLayoutManager;
private List<Item> mItemList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
mRecyclerView = findViewById(R.id.recycler_view);
mAdapter = new MyAdapter(mItemList);
mRecyclerView.setAdapter(mAdapter);
mLayoutManager = new MyLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
mLayoutManager.setOnLoadMoreListener(new MyLayoutManager.OnLoadMoreListener() {
@Override
public void onLoadMore() {
// 加载更多数据
mAdapter.setLoading(false);
}
});
// 初始加载数据
mAdapter.notifyDataSetChanged();
}
}
```
阅读全文