最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android 中RecyclerView頂部刷新實(shí)現(xiàn)詳解

 更新時(shí)間:2017年10月13日 14:45:17   作者:ccpat  
這篇文章主要介紹了Android 中RecyclerView頂部刷新實(shí)現(xiàn)詳解的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下

Android 中RecyclerView頂部刷新實(shí)現(xiàn)詳解

1. RecyclerView頂部刷新的原理

RecyclerView頂部刷新的實(shí)現(xiàn)通常都是在RecyclerView外部再包裹一層布局。在這個(gè)外層布局中,還包含一個(gè)自定義的View,作為頂部刷新時(shí)的指示View。也就是說,外層布局中包含兩個(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)距離超過某個(gè)設(shè)定的值的時(shí)候,執(zhí)行頂部刷新操作。

2. RecyclerView頂部刷新的實(shí)現(xiàn)

RecyclerView頂部刷新的實(shí)現(xiàn)一般包含如下步驟。

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

步驟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()方法用來設(shè)置頂部刷新事件的監(jiān)聽,當(dāng)需要執(zhí)行頂部刷新時(shí)會(huì)調(diào)用此listener的onRefresh()方法,來獲取最新的數(shù)據(jù)。

setRefreshing()方法用來設(shè)置頂部刷新狀態(tài)。當(dāng)數(shù)據(jù)獲取完成后,需要調(diào)用此方法表示刷新完成。

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

3.2 SwipeRefreshLayout結(jié)合RecyclerView實(shí)現(xiàn)頂部刷新

SwipeRefreshLayout結(jié)合RecyclerView實(shí)現(xiàn)頂部刷新功能非常簡(jiǎn)單,只需要在SwipeRefreshLayout中包含一個(gè)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>

為了方便使用,可以對(duì)這里的布局設(shè)置通過代碼進(jìn)行封裝,創(chuàng)建一個(gè)自定義的XSwipeRefreshLayout類來實(shí)現(xiàn)。代碼方式實(shí)現(xiàn)如下。由于布局非常簡(jiǎn)單,代碼中就沒有引入布局文件了。

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只需要通過findViewById()找到對(duì)應(yīng)的RecyclerView對(duì)象,然后調(diào)用相應(yīng)的方法即可。

對(duì)代碼方式實(shí)現(xiàn)的頂部刷新,需要在XSwipeRefreshLayout中增加操作內(nèi)部RecyclerView的接口??梢杂袃煞N方式:一種是在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);
  }
}

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

相關(guān)文章

  • 加載頁面遮擋耗時(shí)操作任務(wù)頁面--第三方開源之AndroidProgressLayout

    加載頁面遮擋耗時(shí)操作任務(wù)頁面--第三方開源之AndroidProgressLayout

    AndroidProgressLayout實(shí)現(xiàn)為界面添加圓形進(jìn)度條。調(diào)用setprogress()方法顯示和隱藏進(jìn)度條,這篇文章主要介紹了加載頁面遮擋耗時(shí)操作任務(wù)頁面--第三方開源之AndroidProgressLayout的相關(guān)資料,需要的朋友可以參考下
    2015-11-11
  • Android開發(fā)之滑動(dòng)數(shù)值選擇器NumberPicker用法示例

    Android開發(fā)之滑動(dòng)數(shù)值選擇器NumberPicker用法示例

    這篇文章主要介紹了Android開發(fā)之滑動(dòng)數(shù)值選擇器NumberPicker用法,結(jié)合實(shí)例形式分析了Android滑動(dòng)數(shù)值選擇器NumberPicker的功能、相關(guān)函數(shù)、事件監(jiān)聽、界面布局等操作技巧,需要的朋友可以參考下
    2019-03-03
  • Android 中對(duì)JSON數(shù)據(jù)解析實(shí)例代碼

    Android 中對(duì)JSON數(shù)據(jù)解析實(shí)例代碼

    這篇文章主要介紹了Android 中對(duì)JSON數(shù)據(jù)解析實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • Android實(shí)現(xiàn)的RecyclerView適配器

    Android實(shí)現(xiàn)的RecyclerView適配器

    這篇文章主要介紹了Android實(shí)現(xiàn)的RecyclerView適配器的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下
    2021-03-03
  • Android開發(fā)筆記之Android中數(shù)據(jù)的存儲(chǔ)方式(二)

    Android開發(fā)筆記之Android中數(shù)據(jù)的存儲(chǔ)方式(二)

    我們?cè)趯?shí)際開發(fā)中,有的時(shí)候需要儲(chǔ)存或者備份比較復(fù)雜的數(shù)據(jù)。這些數(shù)據(jù)的特點(diǎn)是,內(nèi)容多、結(jié)構(gòu)大,比如短信備份等,通過本文給大家介紹Android開發(fā)筆記之Android中數(shù)據(jù)的存儲(chǔ)方式(二),對(duì)android數(shù)據(jù)存儲(chǔ)方式相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧
    2016-01-01
  • ComposeDesktop開發(fā)桌面端多功能APK工具

    ComposeDesktop開發(fā)桌面端多功能APK工具

    這篇文章主要為大家介紹了ComposeDesktop開發(fā)桌面端多功能APK工具實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • Android使用原生組件WebView加載網(wǎng)頁和數(shù)據(jù)的方法

    Android使用原生組件WebView加載網(wǎng)頁和數(shù)據(jù)的方法

    這篇文章主要介紹了Android使用原生組件WebView加載網(wǎng)頁和數(shù)據(jù)的方法的相關(guān)資料,需要的朋友可以參考下
    2016-09-09
  • Android開發(fā)基礎(chǔ)實(shí)現(xiàn)最簡(jiǎn)單的視頻播放示例

    Android開發(fā)基礎(chǔ)實(shí)現(xiàn)最簡(jiǎn)單的視頻播放示例

    這篇文章主要為大家介紹了Android開發(fā)基礎(chǔ)實(shí)現(xiàn)最簡(jiǎn)單的視頻播放示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • Kotlin語言使用WebView示例介紹

    Kotlin語言使用WebView示例介紹

    隨著后臺(tái)技術(shù)的不斷發(fā)展,App前端的應(yīng)用都布置了Web頁面的界面,這個(gè)界面就是由WebView組件渲染出來的。WebView由如下優(yōu)點(diǎn):可以直接顯示和渲染W(wǎng)eb頁面或者網(wǎng)頁;可以直接調(diào)用網(wǎng)絡(luò)上或者本地的html文件,也可以和JavaScript交互使用
    2022-09-09
  • Android Studio 創(chuàng)建自定義控件的方法

    Android Studio 創(chuàng)建自定義控件的方法

    這篇文章主要介紹了Android Studio 創(chuàng)建自定義控件的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06

最新評(píng)論

门头沟区| 磐安县| 平顺县| 大田县| 新绛县| 安龙县| 石楼县| 隆回县| 区。| 北海市| 娄烦县| 永安市| 文昌市| 开江县| 镇康县| 遵化市| 朝阳市| 普兰店市| 马龙县| 宁河县| 佛教| 张北县| 阿拉尔市| 沂源县| 灵宝市| 象山县| 小金县| 青海省| 车致| 阳朔县| 抚顺市| 运城市| 陇南市| 兴文县| 遂昌县| 白城市| 石渠县| 阿拉善右旗| 大名县| 韶关市| 芮城县|