android Material Design 学习之八:SwipeRefreshLayout (下拉刷新)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/dhl_1986/article/details/80310439

滑动过程中还是需要刷新的,V4包中提供了MD风格的SwipeRefreshLayout 来实现这种刷新效果。

SwipeRefreshLayout可以配合RecyclerView 使用达到我们想要的效果。

添加布局文件

  <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_refresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        >
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        >
    </android.support.v7.widget.RecyclerView>
    </android.support.v4.widget.SwipeRefreshLayout>

SwipeRefreshLayout 把RecyclerView 包裹起来。

效果如下:



代码设置监听

先获取Id:

 swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swipe_refresh);

模拟网络延迟:

 swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
               new Handler().postDelayed(new Runnable() {
                   @Override
                   public void run() {
                       swipeRefreshLayout.setRefreshing(false);
                   }
               },2000);
            }
        });

设置下拉刷新颜色:这里是粉色

 swipeRefreshLayout.setColorSchemeResources(R.color.colorAccent);

效果图如下:


猜你喜欢

转载自blog.csdn.net/dhl_1986/article/details/80310439