Android 中RecyclerView頂部刷新實(shí)現(xiàn)詳解
創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的淮北網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
1. RecyclerView頂部刷新的原理
RecyclerView頂部刷新的實(shí)現(xiàn)通常都是在RecyclerView外部再包裹一層布局。在這個(gè)外層布局中,還包含一個(gè)自定義的View,作為頂部刷新時(shí)的指示View。也就是說(shuō),外層布局中包含兩個(gè)child,一個(gè)頂部刷新View,一個(gè)RecyclerView,頂部刷新View默認(rèn)是隱藏不可見的。在外層布局中對(duì)滑動(dòng)事件進(jìn)行處理,當(dāng)RecyclerView滑動(dòng)到頂部并繼續(xù)下滑的時(shí)候,根據(jù)滑動(dòng)的距離決定頂部刷新View的顯示。當(dāng)滑動(dòng)距離超過(guò)某個(gè)設(shè)定的值的時(shí)候,執(zhí)行頂部刷新操作。
2. RecyclerView頂部刷新的實(shí)現(xiàn)
RecyclerView頂部刷新的實(shí)現(xiàn)一般包含如下步驟。
步驟3是其中最復(fù)雜的部分,需要在這些重寫的方法中,完成自身和child的測(cè)量,布局和滑動(dòng)事件的處理。尤其是滑動(dòng)事件的處理,需要對(duì)Android View的滑動(dòng)機(jī)制有全面的了解才能實(shí)現(xiàn)。
Google在19.1之后的support library v4包中增加了SwipeRefreshLayout類。它繼承自ViewGroup,在它的內(nèi)部包含了一個(gè)CircleImageView對(duì)象作為頂部刷新View,同時(shí)它實(shí)現(xiàn)了上述步驟3的全部功能。將SwipeRefreshLayout和RecyclerView結(jié)合在一起,可以輕松的實(shí)現(xiàn)頂部刷新功能。
3.1 SwipeRefreshLayout用法
在介紹SwipeRefreshLayout和RecyclerView結(jié)合實(shí)現(xiàn)頂部刷新功能之前,先介紹下SwipeRefreshLayout的用法。
SwipeRefreshLayout最重要的兩個(gè)方法是:setOnRefreshListener()和setRefreshing()。
setOnRefreshListener()方法用來(lái)設(shè)置頂部刷新事件的監(jiān)聽,當(dāng)需要執(zhí)行頂部刷新時(shí)會(huì)調(diào)用此listener的onRefresh()方法,來(lái)獲取最新的數(shù)據(jù)。
setRefreshing()方法用來(lái)設(shè)置頂部刷新狀態(tài)。當(dāng)數(shù)據(jù)獲取完成后,需要調(diào)用此方法表示刷新完成。
除此之外,SwipeRefreshLayout還提供了一些方法用來(lái)設(shè)置頂部刷新View進(jìn)度條顏色,背景色等。
3.2 SwipeRefreshLayout結(jié)合RecyclerView實(shí)現(xiàn)頂部刷新
SwipeRefreshLayout結(jié)合RecyclerView實(shí)現(xiàn)頂部刷新功能非常簡(jiǎn)單,只需要在SwipeRefreshLayout中包含一個(gè)RecyclerView作為其child即可。可以直接通過(guò)XML文件來(lái)布局。
XML布局如下。
<android.support.v4.widget.SwipeRefreshLayout android:id="@+id/refresh_layout" android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="wrap_content"> </android.support.v7.widget.RecyclerView> </android.support.v4.widget.SwipeRefreshLayout>
為了方便使用,可以對(duì)這里的布局設(shè)置通過(guò)代碼進(jìn)行封裝,創(chuàng)建一個(gè)自定義的XSwipeRefreshLayout類來(lái)實(shí)現(xiàn)。代碼方式實(shí)現(xiàn)如下。由于布局非常簡(jiǎn)單,代碼中就沒(méi)有引入布局文件了。
public class XSwipeRefreshLayout extends SwipeRefreshLayout { private RecyclerView mRecyclerView; public XSwipeRefreshLayout(Context context) { super(context); init(context); } public XSwipeRefreshLayout(Context context, AttributeSet attrs) { super(context, attrs); init(context); } private void init(Context context) { mRecyclerView = new RecyclerView(context); addView(mRecyclerView); } }
3.3 操作RecyclerView
對(duì)XML方式實(shí)現(xiàn)的頂部刷新,要操作RecyclerView只需要通過(guò)findViewById()找到對(duì)應(yīng)的RecyclerView對(duì)象,然后調(diào)用相應(yīng)的方法即可。
對(duì)代碼方式實(shí)現(xiàn)的頂部刷新,需要在XSwipeRefreshLayout中增加操作內(nèi)部RecyclerView的接口。可以有兩種方式:一種是在XSwipeRefreshLayout中增加getRecyclerView()方法,返回內(nèi)部的RecyclerView對(duì)象,然后在外部調(diào)用RecyclerView對(duì)象的方法。另一種是XSwipeRefreshLayout中增加RecyclerView對(duì)應(yīng)的各種方法,然后透?jìng)鹘o內(nèi)部的RecyclerView對(duì)象。這兩種方式的示例代碼如下。
public class XSwipeRefreshLayout extends SwipeRefreshLayout { private RecyclerView mRecyclerView; public XSwipeRefreshLayout(Context context) { super(context); init(context); } public XSwipeRefreshLayout(Context context, AttributeSet attrs) { super(context, attrs); init(context); } private void init(Context context) { mRecyclerView = new RecyclerView(context); addView(mRecyclerView); } public RecyclerView getRecyclerView() { return mRecyclerView; } }
public class XSwipeRefreshLayout extends SwipeRefreshLayout { private RecyclerView mRecyclerView; public XSwipeRefreshLayout(Context context) { super(context); init(context); } public XSwipeRefreshLayout(Context context, AttributeSet attrs) { super(context, attrs); init(context); } private void init(Context context) { mRecyclerView = new RecyclerView(context); addView(mRecyclerView); } public RecyclerView.Adapter getAdapter() { return mRecyclerView.getAdapter(); } public void setAdapter(RecyclerView.Adapter adapter) { mRecyclerView.setAdapter(adapter); } public void setLayoutManager(RecyclerView.LayoutManager layout) { mRecyclerView.setLayoutManager(layout); } // 將需要用到的每個(gè)RecyclerView的方法都寫在這里 ..... }
3. RecyclerView同時(shí)支持頂部刷新和底部刷新
在實(shí)際的應(yīng)用中,頂部刷新通常都需要和底部刷新一起使用。要讓RecyclerView同時(shí)支持頂部刷新和底部刷新,只需要將上述頂部刷新實(shí)現(xiàn)中的RecyclerView換成上一篇文章中XRecyclerView即可。
XML布局如下。
<android.support.v4.widget.SwipeRefreshLayout android:id="@+id/refresh_layout" android:layout_width="match_parent" android:layout_height="wrap_content"> <cnx.ccpat.testapp.XRecyclerView android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="wrap_content"> </cnx.ccpat.testapp.XRecyclerView> </android.support.v4.widget.SwipeRefreshLayout>
對(duì)應(yīng)的代碼方式實(shí)現(xiàn)如下。
public class XSwipeRefreshLayout extends SwipeRefreshLayout { private XRecyclerView mRecyclerView; public XSwipeRefreshLayout(Context context) { super(context); init(context); } public XSwipeRefreshLayout(Context context, AttributeSet attrs) { super(context, attrs); init(context); } private void init(Context context) { mRecyclerView = new XRecyclerView(context); addView(mRecyclerView); } }
如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
當(dāng)前題目:Android中RecyclerView頂部刷新實(shí)現(xiàn)詳解
URL標(biāo)題:http://jinyejixie.com/article18/ppspgp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、營(yíng)銷型網(wǎng)站建設(shè)、手機(jī)網(wǎng)站建設(shè)、微信小程序、ChatGPT、品牌網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)