android控件SwipeRefreshLayout实现下拉刷新

新手写博文,写的不好,还请见谅。

Google为我们推出的控件可以简单的实现下拉刷新的功能,但是这里要注意的是,我们需要引入v4的jar包,并且在xml文件引用SwipeRefreshLayout。
1.xml文件引用SwipeRefreshLayout,代码如下

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

        <ListView
            android:id="@+id/listview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp" />

    </android.support.v4.widget.SwipeRefreshLayout>
    2.Java代码实现
    package com.alpha.swiperefresh;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

/**
* SwipeRefreshLayout实现下拉刷新
*
* @author Simon
*
*/
public class MainActivity extends Activity implements
SwipeRefreshLayout.OnRefreshListener {
private ListView listView;
private SimpleAdapter simpleAdapter;
private SwipeRefreshLayout swipeRefreshLayout;

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swiperefresh);
    swipeRefreshLayout.setOnRefreshListener(this);
    // 顶部刷新的样式
    swipeRefreshLayout.setColorScheme(android.R.color.holo_red_light,
            android.R.color.holo_green_light,
            android.R.color.holo_blue_bright,
            android.R.color.holo_orange_light);
    listView = (ListView) findViewById(R.id.listview1);
    listView.setAdapter(new SimpleAdapter(this, getData(),
            R.layout.listview,
            new String[] { "images", "title", "content" }, new int[] {
                    R.id.imageview1, R.id.textview1, R.id.textview2 }));
}

/**
 * 用集合适配数据
 * 
 * @return
 */

private List<Map<String, Object>> getData() {
    Map<String, Object> map = new HashMap<>();
    List<Map<String, Object>> list = new ArrayList<>();
    for (int i = 0; i < 20; i++) {
        map.put("images", R.drawable.voice);
        map.put("title", "標題");
        map.put("content", "內容");
        list.add(map);
    }

    return list;

}

@Override
public void onRefresh() {
    // TODO Auto-generated method stub
    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            swipeRefreshLayout.setRefreshing(false);

        }
    }, 3000);
}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AnjoyZhang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值