Android中页面添加计时器进行的刷新

亦余心之所善兮,虽九死其犹未悔。这篇文章主要讲述Android中页面添加计时器进行的刷新相关的知识,希望能为你提供帮助。
【Android中页面添加计时器进行的刷新】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文件如下

< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

< TextView
android:id="@+id/tv_loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="@dimen/dp_5" />

< com.shzx.zcyg.customview.AutoSwipeRefreshLayout
android:id="@+id/refresh_list"
android:layout_width="match_parent"
android:layout_height="wrap_content">

< RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

< TextView
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.support.v7.widget.RecyclerView
android:id="@+id/lv_detail"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

< /RelativeLayout>

< /com.shzx.zcyg.customview.AutoSwipeRefreshLayout>


< /LinearLayout>

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);
}
});




























































































    推荐阅读