GeorgeYang'Blog

my technology blog

简述RecyclerView添加HeaderFooter,下拉刷新自动加载功能

阅读:637 创建时间:16-04-24 10:52:30 tags:android,RecyclerView

本文主要简单叙述RecyclerView的一些事,并不会贴上完全代码,只是想表达个人的看法,如需代码,请看文章末尾的参考

0x00 如何给RecyclerView添加HeaderFooter

重写getItemViewType,给header和footer定义viewtype,都不能和itemd的ViewType一样

 private List list;
 public int getItemCount() {
     return headerCount + itemCount + footerCount;
 }

 public int getItemViewType(int position) {
     if (position>headerCount) {
         return TYPE_HEAD;
     } else if (position>headerCount+itemCount) {
         return TYPE_FOOT;
     } else {
         return TYPE_ITEM;
     }
 }

0x01 GridLayoutManager或StaggeredGridLayoutManager的HeaderFooter

由于GridLayoutManager和StaggeredGridLayoutManager会把每个item的宽度都限制住(LinearLayoutManager刚好占满所需宽度),所以我们需要对GridLayoutManager和StaggeredGridLayoutManager进行特殊处理才能让HeaderFooter占满所需宽度

 public yourAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
     private GridSpanSizeLookup mGridSpanSizeLookup;
     private GridLayoutManager gridManager;

     public void onAttachedToRecyclerView(RecyclerView recyclerView) {
         super.onAttachedToRecyclerView(recyclerView);
         RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();

         if (manager instanceof GridLayoutManager) {
             gridManager = ((GridLayoutManager) manager);
             if (mGridSpanSizeLookup == null) {
                 mGridSpanSizeLookup = new GridSpanSizeLookup();
             }
             gridManager.setSpanSizeLookup(mGridSpanSizeLookup);
         } else if (manager instanceof StaggeredGridLayoutManager.LayoutParams) {
             if (isHeader(position) || isFooter(position)) {
                 StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
                 p.setFullSpan(true);
             }
         }
     }

     class GridSpanSizeLookup extends GridLayoutManager.SpanSizeLookup {
         @Override
         public int getSpanSize(int position) {
             if (isHeader(position) || isFooter(position)) {
                 return gridManager.getSpanCount();
             }
             return 1;
         }
     }
 }

0x02 下拉刷新功能

下拉刷新最好的办法就是在外部嵌套SwipeRefreshLayout

 <android.support.v4.widget.SwipeRefreshLayout  
     xmlns:android="http://schemas.android.com/apk/res/android"  
     xmlns:tools="http://schemas.android.com/tools"  
     android:id="@+id/swipe_refresh_widget"  
     android:layout_width="match_parent"  
     android:layout_height="match_parent" >

     <android.support.v7.widget.RecyclerView  
         android:id="@android:id/list"  
         android:layout_width="match_parent"  
         android:layout_height="match_parent"  
         android:cacheColorHint="@null"  
         android:scrollbars="vertical" />

 </android.support.v4.widget.SwipeRefreshLayout>

设置刷新:

 mSwipeRefreshWidget = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_widget);  
   mRecyclerView = (RecyclerView) findViewById(android.R.id.list);

   mSwipeRefreshWidget.setColorScheme(R.color.color1, R.color.color2,  
     R.color.color3, R.color.color4);  
   mSwipeRefreshWidget.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
             @Override
             public void onRefresh() {

             }
         );  
   ....

0x03 滑到最底时自动加载更多

博客出处

这段代码比较简单,和listview的加载更多一样

 recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
             @Override
             public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                 super.onScrolled(recyclerView, dx, dy);
                 int lastVisibleItem = ((LinearLayoutManager) mLayoutManager).findLastVisibleItemPosition();
                 int totalItemCount = mLayoutManager.getItemCount();
                 //lastVisibleItem >= totalItemCount - 4 表示剩下4个item自动加载,各位自由选择
                 // dy>0 表示向下滑动
                 if (lastVisibleItem >= totalItemCount - 4 && dy > 0) {
                     if(isLoadingMore){
                          Log.d(TAG,"ignore manually update!");
                     } else{
                          loadPage();//这里多线程也要手动控制isLoadingMore
                         isLoadingMore = false;
                     }
                 }
             }
         });

参考:

http://my.oschina.net/liucundong/blog/528166

http://www.mobile-open.com/2015/89803.html

http://www.jianshu.com/p/4feb0c16d1b5