The RecycleView slides to the bottom and loads more. The recycleview slides.

Source: Internet
Author: User

The RecycleView slides to the bottom and loads more. The recycleview slides.

Android. support. the v7 package provides a new component: RecycleView, which provides a flexible list of attempts to display large datasets. It supports partial refresh, display animation, and other functions, and can be used to replace ListView and GridView.

However, some problems have been encountered during the use process. Basically, the mobile phone page will now have more functions to slide to the bottom, while RecycleView does not provide methods such as addFooter of ListView, so it is still a bit difficult to implement. After a period of exploration, we finally found more ways to implement RecycleView loading.

This article mainly uses RecycleView'sItemViewTypeTo load more functions at the bottom of the RecycleView activity. The layout of the list, grid, and waterfall stream is supported.

 

Create two la s based on ItemViewType: one for displaying normal content, the other for loading layout, and then listening to the rolling events of RecycleView. When sliding to the bottom, add the loading layout, the layout of the list is very simple and I will not discuss it again. However, for grid layout and waterfall flow layout, we need to solve the problem of "loading layout only occupies one column,

1. For grid layout,GridLayoutManagerProvidesSetSpanSizeLookup ()To set the number of columns that each entry can occupy. The default value is 1.

2. For waterfall streams,StaggeredGridLayoutManagerProvidesStaggeredGridLayoutManager. LayoutParamsInternal static class, which has a setFullSpan () method to set entries to span all columns.

  

3. The implementation of the base class is as follows: subclass only needs to override the following methods:

OnCreateNormalViewHolder (ViewGroup parent );

OnBindNormalViewHolder (RecyclerView. ViewHolder holder, int position );

    

1/** 2 * Created by sunwei on 2015/12/4. 3 * Email: lx_sunwei@163.com. 4 * Description: Sliding recycleView to the bottom to load more 5 */6 public abstract class BaseLoadingAdapter <T> extends RecyclerView. adapter <RecyclerView. viewHolder> {7 8 private static final String TAG = "BaseLoadingAdapter"; 9 10 // whether 11 private boolean mIsLoading = false is being loaded; 12 // normal entry 13 private static final int TYPE_NORMAL_ITEM = 0; 14 // load the entry 15 private static final int TYPE_LOADING_ITEM = 1; 16 // load viewHolder 17 private LoadingViewHolder mLoadingViewHolder; 18 private StaggeredGridLayoutManager variable; 19 // dataset 20 private CircularArray <T> mTs; 21 22 public BaseLoadingAdapter (RecyclerView recyclerView, CircularArray <T> ts) {23 mTs = ts; 24 25 setSpanCount (recyclerView); 26 27 setScrollListener (recyclerView); 28} 29 30 private OnLoadingListener mOnLoadingListener; 31 32/** 33 * load more interfaces 34 */35 public interface OnLoadingListener {36 void loading (); 37} 38 39/** 40 * set the listening interface 41*42 * @ param onLoadingListener 43 */44 public void setOnLoadingListener (OnLoadingListener onLoadingListener) {45 mOnLoadingListener = onLoadingListener; 46} 47 48/** 49 * loaded 50 */51 public void setLoadingC Omplete () {52 mIsLoading = false; 53 mTs. removeFromEnd (1); 54 policyitemremoved (mTs. size ()-1); 55} 56 57/** 58 * No more data 59 */60 public void setLoadingNoMore () {61 mLoadingViewHolder. progressBar. setVisibility (View. GONE); 62 mLoadingViewHolder. tvLoading. setText ("data loaded! "); 63} 64 65/** 66 * loading failed 67 */68 public void setLoadingError () {69 if (mLoadingViewHolder! = Null) {70 mLoadingViewHolder. progressBar. setVisibility (View. GONE); 71 mLoadingViewHolder. tvLoading. setText ("loading failed, Click Reload! "); 72 73 mLoadingViewHolder. tvLoading. setOnClickListener (new View. OnClickListener () {74 @ Override 75 public void onClick (View v) {76 if (mOnLoadingListener! = Null) {77 mLoadingViewHolder. progressBar. setVisibility (View. VISIBLE); 78 mLoadingViewHolder. tvLoading. setText ("loading"); 79 80 mOnLoadingListener. loading (); 81} 82} 83 }); 84} 85} 86 87/** 88 * @ return Whether it is possible for the child view of this layout to 89 * scroll up. override this if the child view is a custom view. 90 */91 private boolean canScrollDown (RecyclerView recyclerView ){ 92 return ViewCompat. canScrollVertically (recyclerView, 1 ); 93} 94 95/** 96 * set the number of columns occupied by each entry 97*98 * @ param recyclerView recycleView 99 */100 private void setSpanCount (RecyclerView recyclerView) {101 RecyclerView. layoutManager layoutManager = recyclerView. getLayoutManager (); 102 if (layoutManager instanceof GridLayoutManager) {103 final GridLayoutManager gridLayoutManager = (GridLayoutManager) lay OutManager; 104 gridLayoutManager. setSpanSizeLookup (new GridLayoutManager. spanSizeLookup () {105 @ Override106 public int getSpanSize (int position) {107 int type = getItemViewType (position); 108 if (type = TYPE_NORMAL_ITEM) {109 return 1; 110} else {111 return gridLayoutManager. getSpanCount (); 112} 113} 114}); 115} 116 if (layoutManager instanceof StaggeredGridLayoutManager) {117 mStaggeredGridLay OutManager = (StaggeredGridLayoutManager) layoutManager; 119} 120 121/** 122 * listen for Rolling events 123*124 * @ param recyclerView recycleView126 */125 private void setScrollListener (RecyclerView recyclerView) {128 recyclerView. addOnScrollListener (new RecyclerView. onScrollListener () {129 @ Override130 public void onScrollStateChanged (RecyclerView recyclerView, int newState) {131 super. onScrollStateChanged (rec YclerView, newState); 132} 133 134 @ Override135 public void onScrolled (RecyclerView recyclerView, int dx, int dy) {136 super. onScrolled (recyclerView, dx, dy); 137 138 if (! CanScrollDown (recyclerView) {139 if (! MIsLoading) {140 mIsLoading = true; 141 mTs. addLast (null); 142 notifyItemInserted (mTs. size ()-1); 143 if (mOnLoadingListener! = Null) {144 mOnLoadingListener. loading (); 145} 146} 147} 148} 149 }); 150} 151 152/** 153 * Create viewHolder154 * 155 * @ param parent viewGroup156 * @ return viewHolder157 */158 public abstract RecyclerView. viewHolder onCreateNormalViewHolder (ViewGroup parent); 159 160/** 161 * bind viewHolder162 * 163 * @ param holder viewHolder164 * @ param position position165 */166 public abstract void onBindNormalViewHolder (RecyclerView. viewHolder holder, int position); 167 168/** 169 * loading layout 170 */171 private class LoadingViewHolder extends RecyclerView. viewHolder {172 public ProgressBar progressBar; 173 public TextView tvLoading; 174 public LinearLayout llyLoading; 175 176 public LoadingViewHolder (View view) {177 super (view); 178 179 progressBar = (ProgressBar) view. findViewById (R. id. progress_loading); 180 tvLoading = (Te XtView) view. findViewById (R. id. TV _loading); 181 llyLoading = (LinearLayout) view. findViewById (R. id. lly_loading); 182} 183} 184 185 @ Override186 public int getItemViewType (int position) {187 T = mTs. get (position); 188 if (t = null) {189 return TYPE_LOADING_ITEM; 190} else {191 return TYPE_NORMAL_ITEM; 192} 193} 194 195 @ Override196 public RecyclerView. viewHolder onCreateViewHolder (ViewGroup parent, Int viewType) {197 if (viewType = TYPE_NORMAL_ITEM) {198 return onCreateNormalViewHolder (parent); 199} else {200 View view = LayoutInflater. from (parent. getContext ()). inflate (201 R. layout. loading_view, parent, false); 202 mLoadingViewHolder = new LoadingViewHolder (view); 203 return mLoadingViewHolder; 204} 205} 206 207 @ Override208 public void onBindViewHolder (RecyclerView. viewHolder holder, int posi Tion) {209 int type = getItemViewType (position); 210 if (type = TYPE_NORMAL_ITEM) {211 onBindNormalViewHolder (holder, position); 212} else {213 if (mStaggeredGridLayoutManager! = Null) {214 StaggeredGridLayoutManager. layoutParams layoutParams = 215 new StaggeredGridLayoutManager. layoutParams (216 ViewGroup. layoutParams. MATCH_PARENT, 217 ViewGroup. layoutParams. WRAP_CONTENT); 218 layoutParams. setFullSpan (true); 219 220 mLoadingViewHolder. llyLoading. setLayoutParams (layoutParams); 221} 222} 223 224 @ Override226 public int getItemCount () {225 return mTs. size (); 228} 229}

 

4. A simple Column

    

1/** 2 * Created by sunwei on 2015/12/4. 3 * Email: lx_sunwei@163.com. 4 * Description: load more 5 */6 public class DesignLoaderMoreAdapter extends BaseLoadingAdapter <DesignItem> {7 8 private CircularArray <DesignItem> mDesignItems; 9 10 public DesignLoaderMoreAdapter (RecyclerView recyclerView, circularArray <DesignItem> datas) {11 super (recyclerView, datas); 12 13 mDesignItems = datas; 14} 15 16 // normal entry 17 public class DesignViewHolder extends RecyclerView. viewHolder {18 public TextView textView; 19 public CardView cardView; 20 public DesignViewHolder (View view) {21 super (view); 22 textView = (TextView) view. findViewById (R. id. TV _design); 23 cardView = (CardView) view. findViewById (R. id. cardView_designer); 24 25} 26} 27 28 @ Override29 public RecyclerView. viewHolder onCreateNormalViewHolder (ViewGroup parent) {30 View = LayoutInflater. from (parent. getContext ()). inflate (31 R. layout. list_item_design, parent, false); 32 return new DesignViewHolder (view); 33} 34 35 @ Override36 public void onBindNormalViewHolder (RecyclerView. viewHolder holder, int position) {37 DesignViewHolder viewHolder = (DesignViewHolder) holder; 38 DesignItem designItem = mDesignItems. get (position); 39 if (position = 10) {40 // set the entry size of the waterfall stream to 41 LinearLayout. layoutParams lp = new LinearLayout. layoutParams (260,360); 42 lp. setMargins (10, 40, 10, 80); 43 viewHolder. cardView. setLayoutParams (lp); 44} 45 46 viewHolder. textView. setText (designItem. name); 47} 48}

 


 

Project address: https://github.com/lxsunwei/MaterialDesignDemo

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.