Android SwipeRefreshLayout 包含ListView 上拉刷新 下拉加载

SwipeRefreshLayout 上拉刷新 下拉加载

直接上图
这里写图片描述


public class MainActivity extends Activity implements OnRefreshListener,
        OnLoadListener {

    private RefreshLayout swipeLayout;
    private ListView listView;
    private MyAdapter adapter;
    private ArrayList<HashMap<String, String>> list;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        setData();
        setListener();
        swipeLayout.initRefresh();
        onRefresh();
    }
    /**
     * 初始化布局
     */
    @SuppressLint({ "InlinedApi", "InflateParams" })
    private void initView() {
        swipeLayout = (RefreshLayout) findViewById(R.id.swipe_container);
        swipeLayout.initRefresh();
    }

    /**
     * 添加数据
     */
    private void setData() {
        list = new ArrayList<HashMap<String, String>>();
        listView = (ListView) findViewById(R.id.list);
        adapter = new MyAdapter(this, list);
        listView.setAdapter(adapter);
    }

    /**
     * 设置监听
     */
    private void setListener() {
        swipeLayout.setOnRefreshListener(this);
        swipeLayout.setOnLoadListener(this);
    }
    /**
     * 上拉刷新
     */
    @Override
    public void onRefresh() {
        swipeLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                // 更新数据
                // 更新完后调用该方法结束刷新
                list.clear();
                for (int i = 0; i < 15; i++) {
                    list.add(new HashMap<String, String>());
                }
                adapter.notifyDataSetChanged();
                swipeLayout.cancelRefresh();
            }
        }, 2000);

    }

    /**
     * 加载更多
     */
    @Override
    public void onLoad() {
        swipeLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                // 更新数据
                // 更新完后调用该方法结束刷新
                swipeLayout.cancelRefresh();
                for (int i = 0; i < 5; i++) {
                    list.add(new HashMap<String, String>());
                }
                adapter.notifyDataSetChanged();
            }
        }, 2000);
    }

}

food

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="11dp" >

        <ProgressBar
            android:id="@+id/pull_to_refresh_load_progress"
            style="@android:style/Widget.ProgressBar.Small.Inverse"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:indeterminate="true" />

        <TextView
            android:id="@+id/pull_to_refresh_loadmore_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_toRightOf="@+id/pull_to_refresh_load_progress"
            android:gravity="center"
            android:text="加载更多" />
    </RelativeLayout>

</RelativeLayout>

mainActivity

package com.example.swiperefreshlayoutdemo;

import java.util.ArrayList;
import java.util.HashMap;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.View;
import android.widget.ListView;

import com.example.swiperefreshlayoutdemo.RefreshLayout.OnLoadListener;
/**
 * 谷歌自带刷新demo
 * @author seven2729
 * @e-mail 731739299@qq.com
 * @version v1.0
 * @copyright 2010-2015
 * @create-time 2015年5月14日 下午2:19:56
 *
 */
public class MainActivity extends Activity implements OnRefreshListener,
        OnLoadListener {

    private RefreshLayout swipeLayout;
    private ListView listView;
    private MyAdapter adapter;
    private ArrayList<HashMap<String, String>> list;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        setData();
        setListener();
        swipeLayout.initRefresh();
        onRefresh();
    }
    /**
     * 初始化布局
     */
    @SuppressLint({ "InlinedApi", "InflateParams" })
    private void initView() {
        swipeLayout = (RefreshLayout) findViewById(R.id.swipe_container);
        swipeLayout.initRefresh();
    }

    /**
     * 添加数据
     */
    private void setData() {
        list = new ArrayList<HashMap<String, String>>();
        listView = (ListView) findViewById(R.id.list);
        adapter = new MyAdapter(this, list);
        listView.setAdapter(adapter);
    }

    /**
     * 设置监听
     */
    private void setListener() {
        swipeLayout.setOnRefreshListener(this);
        swipeLayout.setOnLoadListener(this);
    }
    /**
     * 上拉刷新
     */
    @Override
    public void onRefresh() {
        swipeLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                // 更新数据
                // 更新完后调用该方法结束刷新
                list.clear();
                for (int i = 0; i < 15; i++) {
                    list.add(new HashMap<String, String>());
                }
                adapter.notifyDataSetChanged();
                swipeLayout.cancelRefresh();
            }
        }, 2000);

    }

    /**
     * 加载更多
     */
    @Override
    public void onLoad() {
        swipeLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                // 更新数据
                // 更新完后调用该方法结束刷新
                swipeLayout.cancelRefresh();
                for (int i = 0; i < 5; i++) {
                    list.add(new HashMap<String, String>());
                }
                adapter.notifyDataSetChanged();
            }
        }, 2000);
    }

}

mainLayout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.example.swiperefreshlayoutdemo.RefreshLayout
        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/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </ListView>
    </com.example.swiperefreshlayoutdemo.RefreshLayout>

</RelativeLayout>

adapter

package com.example.swiperefreshlayoutdemo;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public class MyAdapter extends BaseAdapter {
    public ArrayList<HashMap<String, String>> list;
    public Context context;
    public LayoutInflater layoutInflater;

    public MyAdapter(Context context, ArrayList<HashMap<String, String>> list) {
        this.context = context;
        this.list = list;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        ViewHolder holder = null;
        if (convertView == null) {
            view = layoutInflater.inflate(R.layout.item, null);
            holder = new ViewHolder();
            view.setTag(holder);
        } else {
            view = convertView;
            holder = (ViewHolder) view.getTag();
        }
        return view;
    }

    static class ViewHolder {
    }

}

adapter Layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="44dp" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="14dp"
            android:text="hello world!" />
    </RelativeLayout>

</RelativeLayout>

参考 http://blog.csdn.net/seven2729/article/details/48311451
基本上都是他写的 我自己改了一点 欢迎大家查阅他的博客

demo 下载地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值