android widget - SwipeRefreshLayout

https://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html

效果是这样的:



上个代码吧,使用比较简单:

package com.ali.yunos.androiddemo_n.activities;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.ali.yunos.androiddemo_n.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class SwipeRefreshActivity extends Activity {

    private SwipeRefreshLayout mSwipeLayout;
    private ListView mLV;
    private List<String> mData;
    private ArrayAdapter<String> mAdapter;

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

        mSwipeLayout = (SwipeRefreshLayout)findViewById(R.id.sample_swipe);
        mLV = (ListView)findViewById(R.id.swipe_listview);
        
        initData();
        mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mData);
        mLV.setAdapter(mAdapter);

        mSwipeLayout.setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_green_light, android.R.color.holo_orange_light, android.R.color.holo_red_light);
        mSwipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mData.add(0, "添加新的item" + new Random().nextInt());
                        mAdapter.notifyDataSetChanged();
                        //停止刷新动画
                        mSwipeLayout.setRefreshing(false);
                    }
                }, 3000);
            }
        });
    }

    private void initData() {
        mData = new ArrayList<String>();
        for (int i = 0; i < 50; i++) {
            mData.add("Item#" + i);
        }
    }
}

布局文件:

<?xml version="1.0" encoding="utf-8"?>

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

    <ListView
        android:id="@+id/swipe_listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </ListView>
</android.support.v4.widget.SwipeRefreshLayout>


注意 SwipeRefreshLayout套在ListView外面,目前支持ListView和GridView。

To add the swipe to refresh widget to an existing app, add SwipeRefreshLayout as the parent of a single ListView or GridView. Remember that SwipeRefreshLayout only supports a single ListViewor GridView child.

然后监听swipe动作做出响应。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值