Android之XRecyclerView使用詳解

XRecyclerView是實現(xiàn)了下拉刷新、滾動到底部加載更多和自定義添加header等多個特點于一身的第三方庫,使用方式和RecyclerView完全一致,不需要額外的layout,不需要寫特殊的adater(所以個人感覺使用非常方便)。 加載效果內(nèi)置了AVLoadingIndicatorView上的所有效果,可以根據(jù)需要指定。

項目地址:https://github.com/jianghejie/XRecyclerView

效果展示:


首先添加在項目buid.gradle上添加依賴地址

//1.6.0 is the main

implementation 'com.jcodecraeer:xrecyclerview:1.6.0'

創(chuàng)建方式和平常的RecyclerView一樣

LinearLayoutManagerlayoutManager=newLinearLayoutManager(getActivity());

layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

mRecyclerView.setLayoutManager(layoutManager);

mRecyclerView.setAdapter(mAdapter);

下拉刷新、上拉加載更多:

上拉和下拉都是默認就可以使用的(如果需要關閉該功能需要代碼中設置),XRecyclerView中提供一個回調(diào)去觸發(fā)下拉和上拉事件

mRecyclerView.setLoadingListener(newXRecyclerView.LoadingListener() {

? ? ? ?@Override

? ? ? ?publicvoidonRefresh() {

? ? ? ? ? ? ?//refresh data here

? ? ? ?}

? ? ? ?@Override

? ? ? ?publicvoidonLoadMore() {

? ? ? ? ? ? ?//load more data here

? ? ? ?}

});

1.5.7版本的新功能

mRecyclerView

? ? ? ? ?.getDefaultRefreshHeaderView()//獲取默認的頭部布局

? ? ? ? ?.setRefreshTimeVisible(true);//展示刷新時間,false為不展示

? ? ? ? ? //if you are not sure that you are 100% going to

? ? ? ? ? //have no data load back from server anymore,do not use this

? ? ? ? ?@Deprecated

? ? ? ? ?public void setEmptyView(ViewemptyView) {

? ? ? ? ? ? ? ? ?...

? ? ? ? ?}

1.5.6版本新功能,解決內(nèi)存泄漏問題,使用下面的代碼釋放XRecyclerView的內(nèi)存


//any time,when you finish your activity or fragment,call this below

if(mRecyclerView!=null){?

? ? ? ? ? mRecyclerView.destroy();//這行代碼將完全釋放XR的內(nèi)存

? ? ? ? ? mRecyclerView=null;

}

1.5.3版本新功能,可以在粘性滾動布局使用XR

<com.jcodecraeer.xrecyclerview.StickyScrollLinearLayout

? ? ?android:id="@+id/StickyScrollLinearLayout"

? ? ?android:orientation="vertical"

? ? ?android:layout_width="match_parent"

? ? ?android:layout_height="match_parent">

? ? ?<com.jcodecraeer.xrecyclerview.XRecyclerView

? ? ? ? ? android:id="@+id/XRecyclerView"

? ? ? ? ? android:layout_width="match_parent"

? ? ? ? ? android:layout_height="match_parent">

? ? ?</com.jcodecraeer.xrecyclerview.StickyScrollLinearLayout>

finalStickyScrollLinearLayouts=

(StickyScrollLinearLayout) findViewById(R.id.StickyScrollLinearLayout);

s.addOnLayoutChangeListener(

? ? ? ? ?newView.OnLayoutChangeListener() {

? ? ? ? @Override

? ? ? ? public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {

? ? ? ? if(s.getContentView()!=null)

? ? ? ? return;

? ? ? ?//放在這里是為了等初始化結束后再添加,防止 height 獲取 =0

? ? ? ?//add from here just in cause they height==0

? ? ? s.setInitInterface(

? ? ? newStickyScrollLinearLayout.StickyScrollInitInterface() {

? ? ? @Override

? ? ? publicViewsetTopView() {

? ? ? returntopView;

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }

? ? ?@Override

? ? ?publicViewsetTabView() {

? ? ?returntabView;

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }

? ? @Override

? ? publicViewsetContentView() {

? ? returncontent;

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ? ? );

????}

????}

通知移除item、通知插入item,記得使用XRecyclerview內(nèi)部的功能

listData.remove(pos);

mRecyclerView.notifyItemRemoved(listData,pos);

當然當下拉刷新或上拉加載更多結束后,你必須通知RecyclerView,你可以使用

mRecyclerView.loadMoreComplete();//通知上拉加載結束

mRecyclerView.refreshComplete();//通知下拉刷新結束

最好控制一個屏幕中item的數(shù)量是集合的長度減2(list.size-2)的時候進行刷新

mRecyclerView.setLimitNumberToCallLoadMore(2);//默認是 1

效果如下:


手動調(diào)用刷新(使用refresh() 替代了先前的setRefreshing()方法)

mRecyclerView.refresh();

自定義下拉和上拉的樣式style

下拉刷新和上拉加載更多可以更好的自定義

自定義加載樣式

使用內(nèi)置的AVLoadingIndicatorView,在AVLoadingIndicatorView庫中提供了所有的效果,此外我們添加了系統(tǒng)樣式,你可以使用

mRecyclerView.setRefreshProgressStyle(int style);

mRecyclerView.setLaodingMoreProgressStyle(int style);

分別設置下拉刷新進度樣式和上拉加載更多進度樣式

下面展示一些常用進度樣式效果:

mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);


mRecyclerView.setLaodingMoreProgressStyle(ProgressStyle.SquareSpin);


BallPulse 效果


?在ProgressStyle class中我們可以看到所有的效果,你可以選擇自己喜歡的在自己的項目代碼中使用

publicclassProgressStyle{

? ? ?publicstaticfinalintSysProgress=-1;

? ? ?publicstaticfinalintBallPulse=0;

? ? ?publicstaticfinalintBallGridPulse=1;

? ? ?publicstaticfinalintBallClipRotate=2;

? ? ?publicstaticfinalintBallClipRotatePulse=3;

? ? ?publicstaticfinalintSquareSpin=4;

? ? ?publicstaticfinalintBallClipRotateMultiple=5;

? ? ?publicstaticfinalintBallPulseRise=6;

? ? ?publicstaticfinalintBallRotate=7;

? ? ?publicstaticfinalintCubeTransition=8;

? ? ?publicstaticfinalintBallZigZag=9;

? ? ?publicstaticfinalintBallZigZagDeflect=10;

? ? ?publicstaticfinalintBallTrianglePath=11;

? ? ?publicstaticfinalintBallScale=12;

? ? ?publicstaticfinalintLineScale=13;

? ? ?publicstaticfinalintLineScaleParty=14;

? ? ?publicstaticfinalintBallScaleMultiple=15;

? ? ?publicstaticfinalintBallPulseSync=16;

? ? ?publicstaticfinalintBallBeat=17;

? ? ?publicstaticfinalintLineScalePulseOut=18;

? ? ?publicstaticfinalintLineScalePulseOutRapid=19;

? ? ?publicstaticfinalintBallScaleRipple=20;

? ? ?publicstaticfinalintBallScaleRippleMultiple=21;

? ? ?publicstaticfinalintBallSpinFadeLoader=22;

? ? ?publicstaticfinalintLineSpinFadeLoader=23;

? ? ?publicstaticfinalintTriangleSkewSpin=24;

? ? ?publicstaticfinalintPacman=25;

? ? ?publicstaticfinalintBallGridBeat=26;

? ? ?publicstaticfinalintSemiCircleSpin=27;

}

刷新箭頭icon


如果你不喜歡的話也可以使用下面代碼替換掉

mRecyclerView.setArrowImageView(R.drawable.iconfont_downgrey);

使下拉刷新和上拉加載更多失效

mRecyclerView.setPullRefreshEnabled(false);

或者

mRecyclerView.setPullRefreshEnabled(true);

Viewheader=LayoutInflater.from(this).inflate(R.layout.recyclerview_header, (ViewGroup)findViewById(android.R.id.content),false);

給XRecyclerView添加頭部布局,只需調(diào)用addHeaderView().

mRecyclerView.addHeaderView(header);

當然你喜歡的話也可以添加多個頭部布局

Viewheader=LayoutInflater.from(this).inflate(R.layout.recyclerview_header, (ViewGroup)findViewById(android.R.id.content),false);

Viewheader1=LayoutInflater.from(this).inflate(R.layout.recyclerview_header1, (ViewGroup)findViewById(android.R.id.content),false);

mRecyclerView.addHeaderView(header);

mRecyclerView.addHeaderView(header1);

優(yōu)化:

XRecyclerView底部默認會有一小段空白區(qū)域,因為它本身默認有footView,如果想要去除底部空白可以使用:getDefaultFootView().removeAllViews()方法

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容