1.对原生的SwipeRefreshLayout 进行自定义
public class AutoSwipeRefreshLayout extends SwipeRefreshLayout {
public AutoSwipeRefreshLayout(Context context) {
super(context);
}
public AutoSwipeRefreshLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* 自动刷新
*/
public void autoRefresh() {
try {
Field mCircleView = SwipeRefreshLayout.class.getDeclaredField("mCircleView");
mCircleView.setAccessible(true);
View progress = (View) mCircleView.get(this);
progress.setVisibility(VISIBLE);
Method setRefreshing = SwipeRefreshLayout.class.getDeclaredMethod("setRefreshing", boolean.class, boolean.class);
setRefreshing.setAccessible(true);
setRefreshing.invoke(this, true, true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
2.使用时直接调用autoRefresh()方法可以进行自动刷新
3.需要添加正在刷新或者正在加载的提示的情况: 可以再布局中,添加Textview实现
xml文件如下
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:id="@+id/tv_loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="@dimen/dp_5" />
android:id="@+id/refresh_list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:id="@+id/tv_no_content"
style="@style/title_second_setting"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:layout_marginTop="@dimen/dp_20"
android:text="@string/no_content"
android:visibility="gone" />
android:id="@+id/lv_detail"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
4.实现代码:
refreshsetOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
tv_loading.setText(R.string.data_loading);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// 添加需要刷新的数据
getdata();
refresh_order.setRefreshing(false);
if (total_page==0){
tv_loading.setVisibility(View.GONE);
tv_no_content.setVisibility(View.VISIBLE);
}
}
}, 3000);
}
});