手寫RecyclerView萬能適配器

適配器嚣鄙,大家都知道,不多說,直接上代碼:

package com.cn.happiness.mvplib.baseadpter;

import android.content.Context;

import android.support.v7.widget.RecyclerView;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.LinearLayout;

import com.cn.happiness.mvplib.R;

import com.cn.happiness.mvplib.baseadpter.listener.OnRecyclerItemClickListener;

import com.cn.happiness.mvplib.baseadpter.listener.RequestLoadMoreListener;

import com.cn.happiness.mvplib.baseadpter.view.FooterView;

import com.cn.happiness.mvplib.baseadpter.view.LoadType;

import com.cn.happiness.mvplib.util.NetworkUtils;

import java.util.ArrayList;

import java.util.List;

/**

* Created by Happiness on 2017/4/17.

*/

public abstract classBaseRecyclerAdapterextendsRecyclerView.Adapter {

protectedContextmContext;

protected intmLayoutResId;

protectedLayoutInflatermLayoutInflater;

protectedListmData;

private booleanmLoadingMoreEnable=false;

private booleanmNextLoadingEnable=false;

private booleanmHeadAndEmptyEnable;// headerView and emptyView

private booleanmFootAndEmptyEnable;// footerView and emptyView

public booleanmEmptyEnable,mErrorEnable;

private booleanmMultiItemTypeEnable;

privateLoadTypemLoadType;

private static final intVIEW_TYPE_HEADER=0x00001111;//header

public static final intVIEW_TYPE_CONTENT=0x00002222;//content

private static final intVIEW_TYPE_FOOTER=0x00003333;//footer

public static final intVIEW_TYPE_EMPTY=0x00004444;//empty

public static final intVIEW_TYPE_LOADING=0x00005555;//loading

private static final intVIEW_TYPE_NOT_NETWORK=0x00006666;//not network

private intmItemHeight;

private intmLastPosition= -1;

private intmViewType= -1;

privateRequestLoadMoreListenermRequestLoadMoreListener;

privateOnRecyclerItemClickListeneronRecyclerItemClickListener;

privateViewmFooterView,mEmptyView,mErrorView,mContentView,mLoadView;

privateLinearLayoutmHeaderView;

publicBaseRecyclerAdapter(Context context,List data, intlayoutResId) {

this.mContext= context;

this.mData= data ==null?newArrayList<>() : data;

if(layoutResId !=0) {

this.mLayoutResId= layoutResId;

}

this.mLayoutInflater= LayoutInflater.from(mContext);

this.mLoadType= LoadType.CUSTOM;

}

publicBaseRecyclerAdapter(Context context,List data) {

this(context,data,0);

}

/**

*@paramposition

*@return

*/

publicTgetItem(intposition) {

returnmData.get(position);

}

@Override

public intgetItemCount() {

//? ? ? ? int loadMoreCount = isLoadMore() ? 1 : 0;

//? ? ? ? int count = mData.size() + getHeaderViewCount() + getFooterViewCount() + loadMoreCount;

//? ? ? ? if (mData.size() == 0 && mEmptyView != null) {

//? ? ? ? ? ? if (count == 0 && (!mHeadAndEmptyEnable || !mFootAndEmptyEnable)) {

//? ? ? ? ? ? ? ? count += getEmptyViewCount();

//? ? ? ? ? ? } else if (mHeadAndEmptyEnable || mFootAndEmptyEnable) {

//? ? ? ? ? ? ? ? count += getEmptyViewCount();

//? ? ? ? ? ? }

//? ? ? ? ? ? if ((mHeadAndEmptyEnable && getHeaderViewCount() == 1 && count == 1) || count == 0) {

//? ? ? ? ? ? ? ? mEmptyEnable = true;

//? ? ? ? ? ? ? ? count += getEmptyViewCount();

//? ? ? ? ? ? }

//? ? ? ? }

//? ? ? ? return count;

if(mViewType==VIEW_TYPE_CONTENT) {

returnmData.size();

}else{

return1;

}

}

public voidchangeItemViewType(inttype) {

this.mViewType= type;

this.notifyDataSetChanged();

}

@Override

public intgetItemViewType(intposition) {

if(!NetworkUtils.isNetworkConnected(mContext)) {

returnVIEW_TYPE_NOT_NETWORK;

}

if(mViewType==VIEW_TYPE_LOADING) {

returnVIEW_TYPE_LOADING;

}else if(mViewType==VIEW_TYPE_EMPTY) {

returnVIEW_TYPE_EMPTY;

}

returnVIEW_TYPE_CONTENT;

//? ? ? ? if (mHeaderView != null && position == 0) {

//? ? ? ? ? ? return VIEW_TYPE_HEADER;

//? ? ? ? }

//? ? ? ? // emptyView position <=2 (header +? empty + footer)

//? ? ? ? // four situation? {@link #setEmptyView(header+empty+footer , empty? , empty+footer,header+empty)}? position <= 2

//? ? ? ? if (mData.size() == 0 && mEmptyView != null && mEmptyEnable && position <= 2) {

//

//? ? ? ? ? ? // three situation? {@link #setEmptyView(header + empty + footer , header + empty , empty+footer)}? position = 1

//? ? ? ? ? ? if ((mHeadAndEmptyEnable || mFootAndEmptyEnable) && position == 1) {

//

//? ? ? ? ? ? ? ? if (mHeaderView == null && mFooterView != null) { //empty+footer

//? ? ? ? ? ? ? ? ? ? return VIEW_TYPE_FOOTER;

//? ? ? ? ? ? ? ? } else if (mHeaderView != null && mEmptyView != null) {? //header + empty + footer , header + empty

//? ? ? ? ? ? ? ? ? ? return VIEW_TYPE_EMPTY;

//? ? ? ? ? ? ? ? }

//? ? ? ? ? ? }

//? ? ? ? ? ? //two situation? position = 0

//? ? ? ? ? ? else if (position == 0) {

//? ? ? ? ? ? ? ? if (mHeaderView == null) {

//? ? ? ? ? ? ? ? ? ? return VIEW_TYPE_EMPTY;

//? ? ? ? ? ? ? ? } else {

//? ? ? ? ? ? ? ? ? ? return VIEW_TYPE_HEADER;

//? ? ? ? ? ? ? ? }

//? ? ? ? ? ? } else if (position == 2 && mHeaderView != null && mFooterView != null) {

//? ? ? ? ? ? ? ? return VIEW_TYPE_FOOTER;

//? ? ? ? ? ? } else if (position == 1) {

//? ? ? ? ? ? ? ? if (mHeaderView != null) {

//? ? ? ? ? ? ? ? ? ? return VIEW_TYPE_EMPTY;

//? ? ? ? ? ? ? ? }

//? ? ? ? ? ? ? ? return VIEW_TYPE_FOOTER;

//? ? ? ? ? ? }

//? ? ? ? }

//? ? ? ? //position == mData.size() + getHeaderViewsCount()

//? ? ? ? else if (position == mData.size() + getHeaderViewCount()) {

//? ? ? ? ? ? if (mNextLoadingEnable) {

//? ? ? ? ? ? ? ? return VIEW_TYPE_LOADING;

//? ? ? ? ? ? }

//? ? ? ? ? ? return VIEW_TYPE_FOOTER;

//? ? ? ? }

//? ? ? ? //type content

//? ? ? ? else if (position - getHeaderViewCount() >= 0) {

//? ? ? ? ? ? if (mMultiItemTypeEnable)

//? ? ? ? ? ? ? ? return getMultiItemViewType(position - getHeaderViewCount());

//? ? ? ? ? ? return VIEW_TYPE_CONTENT;

//? ? ? ? }

//? ? ? ? return super.getItemViewType(position - getHeaderViewCount());

}

protected intgetMultiItemViewType(intposition) {

return super.getItemViewType(position);

}

@Override

public voidonBindViewHolder(finalRecyclerView.ViewHolder holder, intposition) {

mViewType= holder.getItemViewType();

switch(mViewType) {

caseVIEW_TYPE_CONTENT:

if(mData.isEmpty()) {

return;

}

convert((BaseViewHolder) holder,mData.get(holder.getLayoutPosition() - getHeaderViewCount()));

break;

}

}

//? ? @Override

//? ? public void onAttachedToRecyclerView(RecyclerView recyclerView) {

//? ? ? ? super.onAttachedToRecyclerView(recyclerView);

//? ? ? ? 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) {

//? ? ? ? ? ? ? ? ? ? int type = getItemViewType(position);

//? ? ? ? ? ? ? ? ? ? return (type == VIEW_TYPE_EMPTY || type == VIEW_TYPE_HEADER || type == VIEW_TYPE_FOOTER

//? ? ? ? ? ? ? ? ? ? ? ? ? ? || type == VIEW_TYPE_LOADING) ? gridManager.getSpanCount() : 1;

//? ? ? ? ? ? ? ? }

//? ? ? ? ? ? });

//? ? ? ? }

//? ? }

@Override

publicRecyclerView.ViewHolderonCreateViewHolder(ViewGroup parent, intviewType) {

BaseViewHolder baseViewHolder =null;

switch(viewType) {

caseVIEW_TYPE_HEADER:

baseViewHolder =newBaseViewHolder(mHeaderView);

break;

caseVIEW_TYPE_CONTENT:

baseViewHolder = onBaseViewHolder(parent);

initItemClickListener(baseViewHolder);

break;

caseVIEW_TYPE_FOOTER:

baseViewHolder =newBaseViewHolder(mFooterView);

break;

caseVIEW_TYPE_EMPTY:

mEmptyView=mLayoutInflater.inflate(R.layout.view_empty_material, null);

mEmptyView.setLayoutParams(newViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));

baseViewHolder =newBaseViewHolder(mEmptyView);

break;

caseVIEW_TYPE_LOADING:

mLoadView=mLayoutInflater.inflate(R.layout.view_loading_material, null);

mLoadView.setLayoutParams(newViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));

baseViewHolder =newBaseViewHolder(mLoadView);

//baseViewHolder = addLoadingView(mLoadType);

break;

caseVIEW_TYPE_NOT_NETWORK:

mErrorView=mLayoutInflater.inflate(R.layout.view_net_error_material, null);

mErrorView.setLayoutParams(newViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));

baseViewHolder =newBaseViewHolder(mErrorView);

break;

}

returnbaseViewHolder;

}

/**

*@paramhelper

*@paramitem

*/

protected abstract voidconvert(BaseViewHolder helper,Titem);

/**

*@paramloadType

*@return

*/

publicBaseViewHolderaddLoadingView(LoadType loadType) {

// mLoadView = new FooterView(mContext);

if(mLoadViewinstanceofFooterView) {

((FooterView)mLoadView).setLoadView(loadType);

}

return newBaseViewHolder(mLoadView);

}

/**

*@paramparent

*/

protectedBaseViewHolderonBaseViewHolder(ViewGroup parent) {

returncreateBaseViewHolder(parent,mLayoutResId);

}

/**

*@paramparent

*@paramlayoutResId

*@return

*/

protectedBaseViewHoldercreateBaseViewHolder(ViewGroup parent, intlayoutResId) {

if(mContentView==null) {

return newBaseViewHolder(getItemView(layoutResId,parent));

}

return newBaseViewHolder(mContentView);

}

/**

*@paramlayoutResId

*@paramparent

*@return

*/

protectedViewgetItemView(intlayoutResId,ViewGroup parent) {

View view =mLayoutInflater.inflate(layoutResId,parent, false);

if(mItemHeight!=0) {

view.getLayoutParams().height=mItemHeight;

}

returnview;

}

/**

*@parambaseViewHolder

*/

private voidinitItemClickListener(finalBaseViewHolder baseViewHolder) {

if(onRecyclerItemClickListener!=null) {

baseViewHolder.itemView.setOnClickListener(v ->onRecyclerItemClickListener.onItemClick(v,baseViewHolder.getLayoutPosition() - getHeaderViewCount()));

}

}

/**

* add loadMore interface

*/

public voidaddLoadMore() {

if(isLoadMore() && !mLoadingMoreEnable) {

mLoadingMoreEnable=true;

mRequestLoadMoreListener.onLoadMoreRequested();

}

}

/**

* is loaded more

*/

private booleanisLoadMore() {

returnmNextLoadingEnable&&mRequestLoadMoreListener!=null&&mData.size() >0;

}

/**

*@return

*/

public intgetHeaderViewCount() {

returnmHeaderView==null?0:1;

}

/**

*@return

*/

public intgetFooterViewCount() {

returnmFooterView==null?0:1;

}

/**

*@return

*/

public intgetEmptyViewCount() {

returnmEmptyView==null?0:1;

}

/**

*@paramposition

*@paramitem

*/

public voidadd(intposition,Titem) {

mData.add(position,item);

notifyItemInserted(position);

}

/**

*@paramdatas

*/

public voidaddAll(List datas) {

if(datas !=null) {

mData.addAll(datas);

notifyDataSetChanged();

}

}

/**

*@paramitem

*/

public voidadd(Titem) {

add(mData.size(),item);

}

/**

*@paramemptyView

*/

public voidaddEmptyView(View emptyView) {

addEmptyView(false, false,emptyView);

}

/**

*@paramisHeadAndEmpty

*@paramemptyView

*/

public voidaddEmpty(booleanisHeadAndEmpty,View emptyView) {

addEmptyView(isHeadAndEmpty, false,emptyView);

}

/**

*@paramfooter

*/

public voidaddFooterView(View footer) {

mNextLoadingEnable=false;

this.mFooterView= footer;

this.notifyDataSetChanged();

}

/**

*@paramheader

*/

public voidaddHeaderView(View header) {

if(mHeaderView==null) {

mHeaderView=newLinearLayout(mContext);

mHeaderView.setOrientation(LinearLayout.VERTICAL);

mHeaderView.setLayoutParams(newViewGroup.LayoutParams(

ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));

}

if(header !=null) {

try{

this.mHeaderView.addView(header);

}catch(RuntimeException e) {

this.mHeaderView.removeAllViews();

this.mHeaderView.addView(header);

}

this.notifyDataSetChanged();

}

}

/**

* add no more data view

*/

public voidaddNoMoreView() {

mNextLoadingEnable=false;

mFooterView=newFooterView(mContext);

((FooterView)mFooterView).setNoMoreView();

this.notifyDataSetChanged();

}

/**

* add a loadingView

*

*@paramloadingView

*/

public voidaddLoadingView(View loadingView) {

this.mLoadView= loadingView;

}

/**

*@paramisHeadAndEmpty

*@paramisFootAndEmpty

*@paramemptyView

*/

public voidaddEmptyView(booleanisHeadAndEmpty, booleanisFootAndEmpty,View emptyView) {

mHeadAndEmptyEnable= isHeadAndEmpty;

mFootAndEmptyEnable= isFootAndEmpty;

mEmptyView= emptyView;

mEmptyEnable=true;

}

/**

*@paramenable

*/

public voidopenLoadingMore(booleanenable) {

this.mNextLoadingEnable= enable;

}

/**

*@parammultiItemTypeEnable

*/

public voidopenMultiItemType(booleanmultiItemTypeEnable) {

this.mMultiItemTypeEnable= multiItemTypeEnable;

}

/**

*@paramdata

*/

public voidsetData(List data) {

this.mData= data;

//? ? ? ? if (mRequestLoadMoreListener != null) {

//? ? ? ? ? ? mNextLoadingEnable = true;

//? ? ? ? ? ? mFooterView = null;

//? ? ? ? }

//? ? ? ? mLastPosition = -1;

notifyDataSetChanged();

}

/**

*@paramitemHeight

*/

public voidsetItemHeight(intitemHeight) {

this.mItemHeight= itemHeight;

}

/**

*@paramdata

*/

public voidaddData(List data) {

this.mData.addAll(data);

notifyDataSetChanged();

}

/**

*@return

*/

publicListgetData() {

returnmData;

}

/**

*@paramonRecyclerViewItemClickListener

*/

public voidsetOnRecyclerItemClickListener(OnRecyclerItemClickListener onRecyclerViewItemClickListener) {

this.onRecyclerItemClickListener= onRecyclerViewItemClickListener;

}

/**

*@paramrequestLoadMoreListener

*/

public voidsetOnLoadMoreListener(RequestLoadMoreListener requestLoadMoreListener) {

this.mRequestLoadMoreListener= requestLoadMoreListener;

}

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末笔链,一起剝皮案震驚了整個濱河市舰蟆,隨后出現(xiàn)的幾起案子趣惠,更是在濱河造成了極大的恐慌,老刑警劉巖身害,帶你破解...
    沈念sama閱讀 221,273評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件信卡,死亡現(xiàn)場離奇詭異,居然都是意外死亡题造,警方通過查閱死者的電腦和手機傍菇,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,349評論 3 398
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來界赔,“玉大人丢习,你說我怎么就攤上這事』吹浚” “怎么了咐低?”我有些...
    開封第一講書人閱讀 167,709評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長袜腥。 經常有香客問我见擦,道長,這世上最難降的妖魔是什么羹令? 我笑而不...
    開封第一講書人閱讀 59,520評論 1 296
  • 正文 為了忘掉前任鲤屡,我火速辦了婚禮,結果婚禮上福侈,老公的妹妹穿的比我還像新娘酒来。我一直安慰自己,他們只是感情好肪凛,可當我...
    茶點故事閱讀 68,515評論 6 397
  • 文/花漫 我一把揭開白布堰汉。 她就那樣靜靜地躺著,像睡著了一般伟墙。 火紅的嫁衣襯著肌膚如雪翘鸭。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,158評論 1 308
  • 那天戳葵,我揣著相機與錄音就乓,去河邊找鬼。 笑死,一個胖子當著我的面吹牛档址,可吹牛的內容都是我干的盹兢。 我是一名探鬼主播,決...
    沈念sama閱讀 40,755評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼守伸,長吁一口氣:“原來是場噩夢啊……” “哼绎秒!你這毒婦竟也來了?” 一聲冷哼從身側響起尼摹,我...
    開封第一講書人閱讀 39,660評論 0 276
  • 序言:老撾萬榮一對情侶失蹤见芹,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后蠢涝,有當地人在樹林里發(fā)現(xiàn)了一具尸體玄呛,經...
    沈念sama閱讀 46,203評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,287評論 3 340
  • 正文 我和宋清朗相戀三年和二,在試婚紗的時候發(fā)現(xiàn)自己被綠了徘铝。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,427評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡惯吕,死狀恐怖惕它,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情废登,我是刑警寧澤淹魄,帶...
    沈念sama閱讀 36,122評論 5 349
  • 正文 年R本政府宣布,位于F島的核電站堡距,受9級特大地震影響甲锡,放射性物質發(fā)生泄漏。R本人自食惡果不足惜羽戒,卻給世界環(huán)境...
    茶點故事閱讀 41,801評論 3 333
  • 文/蒙蒙 一缤沦、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧半醉,春花似錦疚俱、人聲如沸劝术。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,272評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽养晋。三九已至衬吆,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間绳泉,已是汗流浹背逊抡。 一陣腳步聲響...
    開封第一講書人閱讀 33,393評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人冒嫡。 一個月前我還...
    沈念sama閱讀 48,808評論 3 376
  • 正文 我出身青樓拇勃,卻偏偏與公主長得像,于是被迫代替她去往敵國和親孝凌。 傳聞我的和親對象是個殘疾皇子方咆,可洞房花燭夜當晚...
    茶點故事閱讀 45,440評論 2 359

推薦閱讀更多精彩內容