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

转载于:https://www.cnblogs.com/zhaohaixia/p/7760841.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值