1. 程式人生 > >Android中頁面添加計時器進行的刷新

Android中頁面添加計時器進行的刷新

recycle padding except circle cycle 直接 context roi pos

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

Android中頁面添加計時器進行的刷新