使用起來超級(jí)方便的RecyclerView阿纤,支持頭尾,滾動(dòng)到尾部監(jiān)聽夷陋。

這是一個(gè)繼承自recyclerView欠拾,并且修改的recyclerView
支持頭尾添加,滾動(dòng)到尾部監(jiān)聽骗绕,方法鏈?zhǔn)秸{(diào)用藐窄。在瀑布流,Gird的形態(tài)下酬土,頭尾依然單獨(dú)占用一行荆忍,已經(jīng)做了修正
將這個(gè)自定義recyclerview的java文件放到你的項(xiàng)目里,就可以開始用了
圖片

如何使用

1.文章的最下方拷貝recycleView的代碼到你的項(xiàng)目里撤缴。

2.在布局里引用

<你的包名.PerfectRecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></你的包名.PerfectRecyclerView>

3.代碼中使用

1.
PerfectRecyclerView  mRecyclerView = (PerfectRecyclerView) findViewById(R.id.recyclerView);

2.

  //一個(gè)正常RecyclerView  adapter  下方有這個(gè)adapter代碼
  RvAdapter rvAdapter = new RvAdapter();

  //初始化刹枉,然后全程鏈?zhǔn)秸{(diào)用,注意調(diào)用順序
  //1.設(shè)置容器 2.設(shè)置adapter 
  // 頭尾隨意使用
  mRecyclerView.
                /**
                 *  設(shè)置容器
                 *  豎的list
                 *  setLayoutManagerVerticalLinear()
                 *  橫的list
                 *  .setLayoutManagerHorizontalLinear()
                 *  豎的Grid
                 *  setLayoutManagerVerticalGird(int行數(shù))
                 *  橫的Grid
                 *  setLayoutManagerHorizontalGird(int行數(shù))
                 *  豎 瀑布流
                 *  setLayoutManagerVerticalStaggered(int行數(shù))
                 *  橫 瀑布流
                 *  setLayoutManagerHorizontalStaggered(int行數(shù))
                 */
             
                //設(shè)置adapter
                .setAdapterPerfect(rvAdapter)

/**
*注意:如果添加了頭布局屈呕,
*那么ViewHolder內(nèi)的getPosition()方法返回的是position+1微宝,正確需要getPosition()-1
*getAdapterPosition() 將返回你的adapter內(nèi)的 position,所以假如調(diào)用刷新虎眨,需要 *getAdapterPosition()+1
*/
                 //添加頭布局
                .addHeaderView(R.layout.header_view)

                //添加腳布局
                .addFooterView(R.layout.footer_view)

                //滾動(dòng)到腳布局監(jiān)聽
                .addScrollBottom(new PerfectRecyclerView.OnScrollBottom() {
                        @Override
                        public void scrollBottom() {
                               //滑動(dòng)到底部監(jiān)聽
                              //這里注意:對(duì)list進(jìn)行判斷
                               if (list != null && list.size() > 0) {
                                  //加載數(shù)據(jù)蟋软,然后刷新
                                (注意:原notifyDataSetChanged()等一系列刷新方法已經(jīng)改變,下  
                                 方有說明)
                              }
                        }
                });

  ////////////////////////////////////////////////////////////////////////////////////////              
          /**
           *完整示例
          */
        mRecyclerView.setLayoutManagerVerticalLinear()
                .setAdapterPerfect(adapter)
                .addHeaderView(R.layout.header)
                .addFooterView(R.layout.foot)
                .addScrollBottom(new PerfectRecyclerView.OnScrollBottom() {
                    @Override
                    public void scrollBottom() {

                    }
                });

  ////////////////////////////////////////////////////////////////////////////////////////       
        
        /**
         *   一些返回方法
         */

        //返回容器
        RecyclerView.LayoutManager layoutManager = mRecyclerView.getLayoutManager();
        
        //返回頭布局view
        View header = mRecyclerView.getHeaderParent();

            //使用這個(gè)header進(jìn)行findViewById你的布局內(nèi)的子view
            //舉例
            TextView tv = header.findViewById(R.id.tv1);
        
        //返回為腳布局view  同頭布局一樣
        View footer = mRecyclerView.getFooterParent();
        

正常的RecyclerView Apapter

//和正常的adapter一樣
    class RvAdapter extends RecyclerView.Adapter<RvAdapter.RvHolder > {

        @Override
        public RvHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new RvHolder (LayoutInflater.from(parent.getContext()).inflate(R.layout.dialog_gallery, parent, false));
        }

        @Override
        public void onBindViewHolder(RvHolder  holder, int position) {
             //無論你添加了頭尾嗽桩,這里的position都是的正確的position
            holder.tv.setText(list.get(position));
        }

        @Override
        public int getItemCount() {
            return list == null ? 0 : list.size();
        }

        class RvHolder extends RecyclerView.ViewHolder {
            private TextView tv;
            public RvHolder (View itemView) {
                super(itemView);
                tv = itemView.findViewById(R.id.tv1);
            }
        }
    }

提供一個(gè)稍微簡易的adapter岳守,將它復(fù)制到你的項(xiàng)目里

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;


public abstract class PerfectAdapter<T extends RecyclerView.ViewHolder, E> extends RecyclerView.Adapter<T> {
    private int layout;
    private List<E> list;

    
    
    public PerfectAdapter(List<E> list, int layout) {
        this.list = list;
        this.layout = layout;
    }

    @Override
    public T onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(layout, viewGroup, false);
        return getHolder(view);
    }

    protected abstract T getHolder(View view);

    @Override
    public void onBindViewHolder(T t, int position) {
        E bean = list.get(position);
        bindView(t, position, bean);
    }

    protected abstract void bindView(T t, int position, E bean);

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }
}

如何使用這個(gè)簡易adapter

//1.先寫一個(gè)viewholder
     class RvHolder extends RecyclerView.ViewHolder {
            private TextView tv;
            public RvHolder (View itemView) {
                super(itemView);
                tv = itemView.findViewById(R.id.tv1);
            }
        }

//2.使用這個(gè)簡易的adapter
    BaseRvAdapter  rvAdapter = new BaseRvAdapter<RvHolder, JavaBean>(list,R.layout.item) {
            @Override
            protected RvHolder getHolder(View view) {
                return new RvHolder(view);
            }

            @Override
            protected void bindView(RvHolder rvHolder, int position, JavaBean bean) {
                     //無論你添加了頭尾,這里的position都是的正確的position
                      rvHolder.tv.setText(bean);
            }
        };

注意

刷新方法不再是你的 adapter.notifyDataSetChanged()
調(diào)用這個(gè)方法將不會(huì)再刷新你的RecyclerView

正確的刷新方法請(qǐng)使用
      //全局刷新
        mRecyclerView.notifyDataSetChanged();
      //某條目刷新
        mRecyclerView.notifyItemChanged(int);
      //插入某條目
        mRecyclerView.notifyItemInserted(int);
      //刪除某條目
        mRecyclerView.notifyItemRemoved(int);

ViewHolder內(nèi)的getPosition()與getAdapterPosition() 方法使用注意
如果你添加了頭布局碌冶,
當(dāng)你需要刷新某一條時(shí)候
那么正確的 getPosition() = getPosition()-1
正確的getAdapterPosition() = getAdapterPosition()+1

這里是這個(gè)RecyclerView的源碼湿痢,復(fù)制到你的項(xiàng)目里就可以使用了

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class PerfectRecyclerView extends RecyclerView {
    private View mHeaderView;
    private View mFooterView;
    private PerfectAdapter perfectAdapter;
    private Context context;
    private boolean loadMoreSwitch = true;



    public PerfectRecyclerView(Context context) {
        super(context);
    }

    public PerfectRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public PerfectRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;
    }

    public PerfectRecyclerView addHeaderView(@LayoutRes int id) {
        mHeaderView = LayoutInflater.from(getContext()).inflate(id, this, false);
        perfectAdapter.notifyItemInserted(0);
        return this;
    }

    public PerfectRecyclerView addFooterView(@LayoutRes int id) {
        mFooterView = LayoutInflater.from(getContext()).inflate(id, this, false);
        perfectAdapter.notifyItemInserted(perfectAdapter.getItemCount() - 1);
        return this;
    }

    public void closeLoadMore() {
        loadMoreSwitch = false;
    }

    public View getFooterParent() {
        return mFooterView;
    }

    public View getHeaderParent() {
        return mHeaderView;
    }

    public void notifyItemInserted(int i) {
        if (mHeaderView != null) i++;
        perfectAdapter.notifyItemInserted(i);
    }

    public void notifyItemChanged(int i) {
        if (mHeaderView != null) i++;
        perfectAdapter.notifyItemChanged(i);
    }

    public void notifyItemRemoved(int i) {
        if (mHeaderView != null) i++;
        perfectAdapter.notifyItemRemoved(i);
    }


    public void notifyDataSetChanged() {
        perfectAdapter.notifyDataSetChanged();
    }


    @Deprecated
    @Override
    public void setAdapter(Adapter adapter) {
        if (adapter != null) {
            perfectAdapter = new PerfectAdapter(adapter);
        }
        super.setAdapter(perfectAdapter);
    }

    public PerfectRecyclerView setAdapterPerfect(Adapter adapter) {
        this.setAdapter(adapter);
        return this;
    }

    @Override
    public void onScrolled(int dx, int dy) {
        super.onScrolled(dx, dy);
        if (onRollingRequestMore != null) {
            if (isSlideToBottom() && loadMoreSwitch) {
                onRollingRequestMore.scrollBottom();
            }
        }
    }

    @Override
    public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
        if (((RecyclerView) target).getChildAt(0).getY() == 0) {
            scollTop.isScollTop();
        }

        super.onNestedPreScroll(target, dx, dy, consumed);

    }


    private ScollTop scollTop;

    public PerfectRecyclerView scollTop(ScollTop scollTop) {
        this.scollTop = scollTop;
        return this;
    }

    public interface ScollTop {
        void isScollTop();
    }



    //VerticalLinear
    public PerfectRecyclerView setLayoutManagerVerticalLinear() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        layoutManager = linearLayoutManager;
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        this.setLayoutManager(linearLayoutManager);
        return this;
    }

    //HorizontalLinear
    public PerfectRecyclerView setLayoutManagerHorizontalLinear() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        layoutManager = linearLayoutManager;
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        this.setLayoutManager(linearLayoutManager);
        return this;
    }

    //VerticalGird
    public PerfectRecyclerView setLayoutManagerVerticalGird(int i) {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, i);
        gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
        layoutManager = gridLayoutManager;
        this.setLayoutManager(gridLayoutManager);
        return this;
    }

    //HorizontalGird
    public PerfectRecyclerView setLayoutManagerHorizontalGird(int i) {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, i);
        gridLayoutManager.setOrientation(GridLayoutManager.HORIZONTAL);
        layoutManager = gridLayoutManager;
        this.setLayoutManager(gridLayoutManager);
        return this;
    }

    //VerticalStaggeredGrid
    public PerfectRecyclerView setLayoutManagerVerticalStaggered(int i) {
        StaggeredGridLayoutManager sgl = new StaggeredGridLayoutManager(i, StaggeredGridLayoutManager.VERTICAL);
        layoutManager = sgl;
        this.setLayoutManager(sgl);
        return this;
    }

    /**
     * 平行瀑布流
     *
     * @param i 垂直每行 i 條
     * @return
     */
    public PerfectRecyclerView setLayoutManagerHorizontalStaggered(int i) {
        StaggeredGridLayoutManager sgl = new StaggeredGridLayoutManager(i, StaggeredGridLayoutManager.HORIZONTAL);
        layoutManager = sgl;
        this.setLayoutManager(sgl);
        return this;
    }

    private LayoutManager layoutManager;

    /**
     * @return LayoutManager
     */
    public LayoutManager getLayoutManager() {
        return layoutManager;
    }


    OnScrollBottom onRollingRequestMore;


    /**
     * 滑動(dòng)到底部監(jiān)聽
     *
     * @param onBottomLoad
     */
    public void addScrollBottom(OnScrollBottom onBottomLoad) {
        this.onRollingRequestMore = onBottomLoad;
    }

    public interface OnScrollBottom {
        void scrollBottom();
    }

    protected boolean isSlideToBottom() {
        if (computeVerticalScrollExtent() + computeVerticalScrollOffset() >= computeVerticalScrollRange())
            return true;
        return false;
    }

    private class PerfectAdapter extends Adapter<ViewHolder> {

        private Adapter adapter;
        private int Item_Normal = 0;
        private int Item_Header = 1;
        private int Item_Footer = 2;


        public PerfectAdapter(Adapter adapter) {
            this.adapter = adapter;
        }

        @Override
        public void onViewAttachedToWindow(ViewHolder holder) {
            super.onViewAttachedToWindow(holder);
            if (isStaggeredGridLayout(holder)) {
                handleLayoutIfStaggeredGridLayout(holder, holder.getLayoutPosition());
            }
        }

        @Override
        public void onAttachedToRecyclerView(RecyclerView recyclerView) {
            super.onAttachedToRecyclerView(recyclerView);
            LayoutManager manager = recyclerView.getLayoutManager();
            if (manager instanceof GridLayoutManager) {
                final GridLayoutManager gridManager = ((GridLayoutManager) manager);
                gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                    @Override
                    public int getSpanSize(int position) {
                        return getItemViewType(position) == Item_Footer | getItemViewType(position) == Item_Header ? gridManager.getSpanCount() : 1;
                    }
                });
            }
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            if (viewType == Item_Header) {
                return new PerfectViewHolder(mHeaderView);
            } else if (viewType == Item_Footer) {
                return new PerfectViewHolder(mFooterView);
            } else {
                return adapter.onCreateViewHolder(parent, viewType);
            }
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            int type = getItemViewType(position);
            if (type == Item_Header || type == Item_Footer) {
                return;
            }

            int realPosition = getRealItemPosition(position);
            adapter.onBindViewHolder(holder, realPosition);
        }

        @Override
        public int getItemCount() {
            int itemCount = adapter.getItemCount();
            if (null != mHeaderView) itemCount++;
            if (null != mFooterView) itemCount++;
            return itemCount;
        }

        @Override
        public int getItemViewType(int position) {
            if (null != mHeaderView && position == 0) return Item_Header;
            if (null != mFooterView && position == getItemCount() - 1) return Item_Footer;
            return Item_Normal;
        }

        private boolean isStaggeredGridLayout(ViewHolder holder) {
            ViewGroup.LayoutParams layoutParams = holder.itemView.getLayoutParams();
            if (layoutParams != null && layoutParams instanceof StaggeredGridLayoutManager.LayoutParams) {
                return true;
            }
            return false;
        }

        protected void handleLayoutIfStaggeredGridLayout(ViewHolder holder, int position) {
            if (getItemViewType(position) == Item_Footer | getItemViewType(position) == Item_Header) {
                StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
                p.setFullSpan(true);
            }
        }

        private int getRealItemPosition(int position) {
            if (null != mHeaderView) {
                return position - 1;
            }
            return position;
        }

        private class PerfectViewHolder extends ViewHolder {

            PerfectViewHolder(View itemView) {
                super(itemView);

            }
        }
    }


}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市种樱,隨后出現(xiàn)的幾起案子蒙袍,更是在濱河造成了極大的恐慌,老刑警劉巖嫩挤,帶你破解...
    沈念sama閱讀 206,602評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件害幅,死亡現(xiàn)場離奇詭異,居然都是意外死亡岂昭,警方通過查閱死者的電腦和手機(jī)以现,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,442評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來约啊,“玉大人邑遏,你說我怎么就攤上這事∏【兀” “怎么了记盒?”我有些...
    開封第一講書人閱讀 152,878評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵,是天一觀的道長外傅。 經(jīng)常有香客問我纪吮,道長,這世上最難降的妖魔是什么萎胰? 我笑而不...
    開封第一講書人閱讀 55,306評(píng)論 1 279
  • 正文 為了忘掉前任碾盟,我火速辦了婚禮,結(jié)果婚禮上技竟,老公的妹妹穿的比我還像新娘冰肴。我一直安慰自己,他們只是感情好榔组,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,330評(píng)論 5 373
  • 文/花漫 我一把揭開白布熙尉。 她就那樣靜靜地躺著,像睡著了一般搓扯。 火紅的嫁衣襯著肌膚如雪骡尽。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,071評(píng)論 1 285
  • 那天擅编,我揣著相機(jī)與錄音攀细,去河邊找鬼。 笑死爱态,一個(gè)胖子當(dāng)著我的面吹牛谭贪,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播锦担,決...
    沈念sama閱讀 38,382評(píng)論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼俭识,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼!你這毒婦竟也來了洞渔?” 一聲冷哼從身側(cè)響起套媚,我...
    開封第一講書人閱讀 37,006評(píng)論 0 259
  • 序言:老撾萬榮一對(duì)情侶失蹤缚态,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后堤瘤,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體玫芦,經(jīng)...
    沈念sama閱讀 43,512評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,965評(píng)論 2 325
  • 正文 我和宋清朗相戀三年本辐,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了桥帆。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,094評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡慎皱,死狀恐怖老虫,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情茫多,我是刑警寧澤祈匙,帶...
    沈念sama閱讀 33,732評(píng)論 4 323
  • 正文 年R本政府宣布,位于F島的核電站天揖,受9級(jí)特大地震影響菊卷,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜宝剖,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,283評(píng)論 3 307
  • 文/蒙蒙 一洁闰、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧万细,春花似錦扑眉、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,286評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至雪营,卻和暖如春弓千,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背献起。 一陣腳步聲響...
    開封第一講書人閱讀 31,512評(píng)論 1 262
  • 我被黑心中介騙來泰國打工洋访, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人谴餐。 一個(gè)月前我還...
    沈念sama閱讀 45,536評(píng)論 2 354
  • 正文 我出身青樓姻政,卻偏偏與公主長得像,于是被迫代替她去往敵國和親岂嗓。 傳聞我的和親對(duì)象是個(gè)殘疾皇子汁展,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,828評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容