看到了Android版知乎实现了这种效果,就自己也实现了一下。
先来一张效果图
实现方式:
方法一:
①在onWindowFocusChanged()方法中,设置为刷新状态为true
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
mSwipeRefreshLayout.setRefreshing(true);
}
②在获取数据完成后设置刷新状态为false
if (mSwipeRefreshLayout.isRefreshing()) {
mSwipeRefreshLayout.setRefreshing(false);
}
方法二:
①调用mSwipeRefreshLayout.measure()方法后,设置刷新状态为true
//手动调用,通知系统去测量
mSwipeRefreshLayout.measure(0,0);
mSwipeRefreshLayout.setRefreshing(true);
②在获取数据完成后设置刷新状态为false
if (mSwipeRefreshLayout.isRefreshing()) {
mSwipeRefreshLayout.setRefreshing(false);
}
说明:
方法一和方法二的第一步的目的,都是为了在SwipeRefreshLayout绘制完成之后,再设置刷新状态为true,否则大多数情况下,SwipeRefreshLayout刷新球会不显示。
源码:
package org.raphets.swiperefreshlayoutdemo;
import android.graphics.Color;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private SwipeRefreshLayout mSwipeRefreshLayout;
private TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.srl);
mTextView = (TextView) findViewById(R.id.tv);
//设置刷新球颜色
mSwipeRefreshLayout.setColorSchemeColors(Color.BLUE, Color.RED, Color.YELLOW);
mSwipeRefreshLayout.setProgressBackgroundColorSchemeColor(Color.parseColor("#BBFFFF"));
//手动调用,通知系统去测量
// mSwipeRefreshLayout.measure(0,0);
mSwipeRefreshLayout.setRefreshing(true);
getData();
}
private void getData() {
new Thread() {
@Override
public void run() {
super.run();
//模拟网络请求
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//在UI线程中更新UI
runOnUiThread(new Runnable() {
@Override
public void run() {
mTextView.setText("首次进入自动刷新");
if (mSwipeRefreshLayout.isRefreshing()) {
mSwipeRefreshLayout.setRefreshing(false);
}
}
});
}
}.start();
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
mSwipeRefreshLayout.setRefreshing(true);
}
}
您可能感兴趣的文章:Android 使用SwipeRefreshLayout控件仿抖音做的视频下拉刷新效果Android SwipeRefreshLayout仿抖音app静态刷新android使用SwipeRefreshLayout实现ListView下拉刷新上拉加载Android SwipereFreshLayout下拉刷新Android SwipeRefreshLayout下拉刷新组件示例android组件SwipeRefreshLayout下拉小球式刷新效果Android SwipeRefreshLayout下拉刷新源码解析Android下拉刷新SwipeRefreshLayout控件使用方法Android官方下拉刷新控件SwipeRefreshLayout使用详解Android自定义SwipeRefreshLayout高仿微信朋友圈下拉刷新Android下拉刷新控件SwipeRefreshLayout源码解析SwipeRefreshLayout+RecyclerView实现上拉刷新和下拉刷新功能