学习Android系统控件SwipeRefreshLayout

学习地址:麦子学院 Android 高级UI 课程


1布局文件中使用android.support.v4.widger.SwipeRefreshLayout

2.布局文件中的SwipeRefreshLayout只能包含一个child 这个Child必须是可滚动的view,如ListView,ScrollView

3.实例化SwipeRefreshLayout的Activity,要实现OnRefreshListener借口 ,实现onRefresh()方法,为SwipeRefreshLayout设置RefreshListener

4.调用SwipeRefreshLayout的setRefreshing(true)方法,以显示刷新过程动画


<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/generate_random"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/pull_to_random"
                android:textSize="15sp" />

            <TextView
                android:id="@+id/random"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#ff0000"
                android:textSize="15sp" />
        </LinearLayout>

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


在MainActivity中

public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {

    private SwipeRefreshLayout mSwipeRefreshLayout;
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh);
        mSwipeRefreshLayout.setOnRefreshListener(this);

        mTextView = (TextView) findViewById(R.id.random);

    }

    @Override
    public void onRefresh() {
        mSwipeRefreshLayout.setRefreshing(true);

        (new Handler()).postDelayed(new Runnable() {
            @Override
            public void run() {
                mSwipeRefreshLayout.setRefreshing(false);//停止动画
                int i = (int) (Math.random() * 100 + 1); // 产生一个1~100的整数
                mTextView.setText(String.valueOf(i));
            }
        },3000);
    }
}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值