国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 中RecyclerView頂部刷新實現詳解

2019-10-22 18:25:46
字體:
來源:轉載
供稿:網友

Android 中RecyclerView頂部刷新實現詳解

1. RecyclerView頂部刷新的原理

RecyclerView頂部刷新的實現通常都是在RecyclerView外部再包裹一層布局。在這個外層布局中,還包含一個自定義的View,作為頂部刷新時的指示View。也就是說,外層布局中包含兩個child,一個頂部刷新View,一個RecyclerView,頂部刷新View默認是隱藏不可見的。在外層布局中對滑動事件進行處理,當RecyclerView滑動到頂部并繼續下滑的時候,根據滑動的距離決定頂部刷新View的顯示。當滑動距離超過某個設定的值的時候,執行頂部刷新操作。

2. RecyclerView頂部刷新的實現

RecyclerView頂部刷新的實現一般包含如下步驟。

  • 創建自定義的布局類,它可以繼承自已有的布局類,如LinearLayout,也可以直接繼承自ViewGroup。
  • 添加RecyclerView和頂部刷新View作為其child。
  • 重寫自定義的布局類的onMeasure(),onLayout(),dispatchTouchEvent(),onInterceptTouchEvent()等方法。

步驟3是其中最復雜的部分,需要在這些重寫的方法中,完成自身和child的測量,布局和滑動事件的處理。尤其是滑動事件的處理,需要對Android View的滑動機制有全面的了解才能實現。

Google在19.1之后的support library v4包中增加了SwipeRefreshLayout類。它繼承自ViewGroup,在它的內部包含了一個CircleImageView對象作為頂部刷新View,同時它實現了上述步驟3的全部功能。將SwipeRefreshLayout和RecyclerView結合在一起,可以輕松的實現頂部刷新功能。

3.1 SwipeRefreshLayout用法

在介紹SwipeRefreshLayout和RecyclerView結合實現頂部刷新功能之前,先介紹下SwipeRefreshLayout的用法。

SwipeRefreshLayout最重要的兩個方法是:setOnRefreshListener()和setRefreshing()。

setOnRefreshListener()方法用來設置頂部刷新事件的監聽,當需要執行頂部刷新時會調用此listener的onRefresh()方法,來獲取最新的數據。

setRefreshing()方法用來設置頂部刷新狀態。當數據獲取完成后,需要調用此方法表示刷新完成。

除此之外,SwipeRefreshLayout還提供了一些方法用來設置頂部刷新View進度條顏色,背景色等。

3.2 SwipeRefreshLayout結合RecyclerView實現頂部刷新

SwipeRefreshLayout結合RecyclerView實現頂部刷新功能非常簡單,只需要在SwipeRefreshLayout中包含一個RecyclerView作為其child即可。可以直接通過XML文件來布局。

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>

為了方便使用,可以對這里的布局設置通過代碼進行封裝,創建一個自定義的XSwipeRefreshLayout類來實現。代碼方式實現如下。由于布局非常簡單,代碼中就沒有引入布局文件了。

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

對XML方式實現的頂部刷新,要操作RecyclerView只需要通過findViewById()找到對應的RecyclerView對象,然后調用相應的方法即可。

對代碼方式實現的頂部刷新,需要在XSwipeRefreshLayout中增加操作內部RecyclerView的接口??梢杂袃煞N方式:一種是在XSwipeRefreshLayout中增加getRecyclerView()方法,返回內部的RecyclerView對象,然后在外部調用RecyclerView對象的方法。另一種是XSwipeRefreshLayout中增加RecyclerView對應的各種方法,然后透傳給內部的RecyclerView對象。這兩種方式的示例代碼如下。

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);  }  // 將需要用到的每個RecyclerView的方法都寫在這里  .....}

3. RecyclerView同時支持頂部刷新和底部刷新

在實際的應用中,頂部刷新通常都需要和底部刷新一起使用。要讓RecyclerView同時支持頂部刷新和底部刷新,只需要將上述頂部刷新實現中的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>

對應的代碼方式實現如下。

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);  }}

如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 潮州市| 道孚县| 东辽县| 渝北区| 永顺县| 乐东| 渭源县| 和龙市| 咸宁市| 贵定县| 武陟县| 乌什县| 凭祥市| 交城县| 盘锦市| 闽侯县| 淮滨县| 武功县| 介休市| 桃园县| 河曲县| 香格里拉县| 丹巴县| 中阳县| 江北区| 瓦房店市| 新泰市| 马尔康县| 崇义县| 枞阳县| 陵川县| 洛宁县| 贵南县| 长乐市| 云霄县| 商丘市| 宝坻区| 维西| 云龙县| 眉山市| 张家港市|