android系统自带下拉刷新控件的实现

android系统自带的下拉刷新控件SwipeRefreshLayout位于android.support.v4.widget包下,实现步骤如下:

1.在布局文件中添加该控件,该控件一般作为父控件,而且只能包含有一个子控件,并且这个子控件是能够滑动的,比如scrollview,listview等

2.实现OnRefreshListener接口,并重写onRefresh函数


详细代码如下:

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/swipe_refresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ScrollView
        android:id="@+id/scrollview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        
        <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >
            
            <TextView 
                android:id="@+id/tip_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/pull_fresh"
                android:textSize="15sp"
                />
            <TextView 
                android:id="@+id/random_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#FF000000"
                android:textSize="15sp"
                />
            
        </LinearLayout>
        
    </ScrollView>

</android.support.v4.widget.SwipeRefreshLayout>

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.widget.TextView;


/**
 * MainActivity---系统下拉刷新控件的实现
 * @author seabear
 *
 */
public class MainActivity extends Activity implements OnRefreshListener{

	
	private SwipeRefreshLayout mSwipeRefreshLayout;
	private TextView mRandomText;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mSwipeRefreshLayout = (SwipeRefreshLayout)this.findViewById(R.id.swipe_refresh);
		mSwipeRefreshLayout.setOnRefreshListener(this);
		
		mRandomText = (TextView)this.findViewById(R.id.random_text);
		
	}


	@Override
	public void onRefresh() {
		mSwipeRefreshLayout.setRefreshing(true);
		
		(new Handler()).postDelayed(new Runnable() {
			
			@Override
			public void run() {
				//3秒后停止刷新
				mSwipeRefreshLayout.setRefreshing(false);
				int num = (int)(Math.random() * 100 + 1);
				String s = mRandomText.getText().toString();
				s = s + " " + num;
				mRandomText.setText(s);
				
			}
		}, 3000);
	}
}


  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值