这篇文章参考:
https://www.journaldev.com/24041/android-recyclerview-load-more-endless-scrolling
这一篇文章讲的是RecyclerView的endless scrolling
我们都见过APP向上滑动然后又一个旋转小圈圈(ProgressBar)在那转呀转,转完之后就加载出更多的Item。是不是很神奇,那接下来我们讲解一下是如何实现的吧。
其实就是在RecyclerView的Adapter中的数据末尾加上null,通过null来作为提示显示圆圈和加载更多。
首先是旋转小圆圈,我们在Resource的layout文件夹中创建circle_wait.xml, root为linearLayout:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android" android:layout_width ="match_parent" android:layout_height ="wrap_content" android:id ="@+id/progress_linear_layout" android:orientation ="vertical" > <ProgressBar android:layout_width ="wrap_content" android:layout_height ="wrap_content" android:id ="@+id/upload_circle" android:layout_gravity ="center_horizontal" /> </LinearLayout >
RecyclerView其中包含的View的Layout我就掠过了
然后我们需要在RecyclerView中的Adapter中加上null的情况:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 package com.journaldev.androidrecyclerviewloadmore;import android.support.annotation.NonNull;import android.support.v7.widget.RecyclerView;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ProgressBar;import android.widget.TextView;import java.util.List;public class RecyclerViewAdapter extends RecyclerView .Adapter <RecyclerView .ViewHolder > { private final int VIEW_TYPE_ITEM = 0 ; private final int VIEW_TYPE_LOADING = 1 ; public List<String> mItemList; public RecyclerViewAdapter (List<String> itemList) { mItemList = itemList; } @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder (@NonNull ViewGroup parent, int viewType) { if (viewType == VIEW_TYPE_ITEM) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_row, parent, false ); return new ItemViewHolder(view); } else { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_loading, parent, false ); return new LoadingViewHolder(view); } } @Override public void onBindViewHolder (@NonNull RecyclerView.ViewHolder viewHolder, int position) { if (viewHolder instanceof ItemViewHolder) { populateItemRows((ItemViewHolder) viewHolder, position); } else if (viewHolder instanceof LoadingViewHolder) { showLoadingView((LoadingViewHolder) viewHolder, position); } } @Override public int getItemCount () { return mItemList == null ? 0 : mItemList.size(); } @Override public int getItemViewType (int position) { return mItemList.get(position) == null ? VIEW_TYPE_LOADING : VIEW_TYPE_ITEM; } private class ItemViewHolder extends RecyclerView .ViewHolder { TextView tvItem; public ItemViewHolder (@NonNull View itemView) { super (itemView); tvItem = itemView.findViewById(R.id.tvItem); } } private class LoadingViewHolder extends RecyclerView .ViewHolder { ProgressBar progressBar; public LoadingViewHolder (@NonNull View itemView) { super (itemView); progressBar = itemView.findViewById(R.id.progressBar); } } private void showLoadingView (LoadingViewHolder viewHolder, int position) { } private void populateItemRows (ItemViewHolder viewHolder, int position) { String item = mItemList.get(position); viewHolder.tvItem.setText(item); } public List<String> getListFromAdapter (int index) { return mItemList; } }
接下来就是MainActivity里面,将Adapter绑定到RecyclerView上,并且为RecyclerView添加上Scroll Listner:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 package com.journaldev.androidrecyclerviewloadmore;import android.os.Handler;import android.support.annotation.NonNull;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.support.v7.widget.LinearLayoutManager;import android.support.v7.widget.RecyclerView;import java.util.ArrayList;public class MainActivity extends AppCompatActivity { RecyclerView recyclerView; RecyclerViewAdapter recyclerViewAdapter; ArrayList<String> rowsArrayList = new ArrayList<>(); boolean isLoading = false ; @Override protected void onCreate (Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); recyclerView = findViewById(R.id.recyclerView); populateData(); initAdapter(); initScrollListener(); } private void populateData () { int i = 0 ; while (i < 10 ) { rowsArrayList.add("Item " + i); i++; } } private void initAdapter () { recyclerViewAdapter = new RecyclerViewAdapter(rowsArrayList); recyclerView.setAdapter(recyclerViewAdapter); } private void initScrollListener () { recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged (@NonNull RecyclerView recyclerView, int newState) { super .onScrollStateChanged(recyclerView, newState); } @Override public void onScrolled (@NonNull RecyclerView recyclerView, int dx, int dy) { super .onScrolled(recyclerView, dx, dy); LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager(); if (!isLoading) { if (linearLayoutManager != null && linearLayoutManager.findLastCompletelyVisibleItemPosition() == rowsArrayList.size() - 1 ) { rowsArrayList = recyclerView.getListFromAdapter(); rowsArrayList.add(null ); recyclerView.getAdapter().notifyItemInserted(rowsArrayList.size() - 1 ); isLoading = true ; isLoading = false ; } } } }); } }