Android下拉刷新SwipeRefreshLayout简单用法

以前一直都想用下拉刷新,感受上是庞大的工程,因此搁置了。如今学习了一下其实真的超级简单。android

看了《第一行代码》以及 https://www.jianshu.com/p/3c402a9e4b7d文章web

看上去是真的简单。SwipeRefreshLayout下嵌套一个控件app

1.布局代码

<android.support.v4.widget.SwipeRefreshLayout android:id="@+id/mswipeRefreshLayout" android:layout_width="match_parent" android:layout_height="match_parent" >

        <WebView android:id="@+id/mwebview" android:layout_width="match_parent" android:layout_height="match_parent"
            >
        </WebView>
</android.support.v4.widget.SwipeRefreshLayout>

混编的app,这里嵌套了一个webviewide

2.Activity代码

mSwipe = findViewById(R.id.mswipeRefreshLayout); private void setSwipe() { /* * 设置下拉刷新球颜色 */ mSwipe.setColorSchemeResources(R.color.swipeColor1,R.color.swipeColor2,R.color.swipeColor3,R.color.swipeColor4,R.color.swipeColor5); /* * 设置下拉刷新的监听 */ mSwipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { //刷新需执行的操做 } }); }

 咱们能够在res/values/colors.xml文件中设置咱们想要的颜色布局

<color name="swipeColor1">#3F51B5</color>
<color name="swipeColor2">#000000</color> ......

加载完毕后须要关闭下拉刷新球post

mSwipe.setRefreshing(false);

3.打开页面自动刷新

mSwipe.measure(0,0);
mSwipe.setRefreshing(true);

在onMeasure以前 单独使用setRefreshing(true)是没有效果的学习

 

还能够复写onWindowFocusChanged当页面得到焦点的时刷新ui

 @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); mSwipe.setRefreshing(true); }

 

也能够放到ui线程消息循环中排队spa

mSwipe.post(new Runnable() { @Override public void run() { mSwipe.setRefreshing(true); } });

 

在刷新结束后关闭刷新球线程

public void SwipeIsFinish(){ if (mSwipe.isRefreshing()) { mSwipe.setRefreshing(false); } }
相关文章
相关标签/搜索