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
基本上都是他写的 我自己改了一点 欢迎大家查阅他的博客