SwipeRefreshLayout实现上拉加载下拉刷新

时间:2021-12-11 08:00:51

package com.example.swiperefreshlayoutdemo;

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

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;

public class MainActivity extends Activity implements OnRefreshListener,
OnLoadListener {

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

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
setData();
setListener();
}

private void initView() {
header = getLayoutInflater().inflate(R.layout.header, null);
swipeLayout = (RefreshLayout) findViewById(R.id.swipe_container);
swipeLayout.setOnRefreshListener(this);
swipeLayout.setColorScheme(android.R.color.holo_blue_bright,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light);

}

private void setData() {
list = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < 5; i++) {
HashMap<String, String> map=new HashMap<String, String>();
map.put("i", i+"-");
list.add(map);
}

listView = (ListView) findViewById(R.id.list);
listView.addHeaderView(header);
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() {
// 更新数据
// 更新完后调用该方法结束刷新
HashMap<String, String> map=new HashMap<String, String>();
map.put("i", "rrr-");
list.add(map);
adapter.notifyDataSetChanged();
swipeLayout.setRefreshing(false);
}
}, 1000);

}

@Override
public void onLoad() {
swipeLayout.postDelayed(new Runnable() {

@Override
public void run() {
// 更新数据
// 更新完后调用该方法结束刷新
HashMap<String, String> map=new HashMap<String, String>();
map.put("i", "lll---");
list.add(map);
adapter.notifyDataSetChanged();
swipeLayout.setLoading(false);
}
}, 1000);
}

}

-----------------------

package com.example.swiperefreshlayoutdemo;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;

/**
* 继承自SwipeRefreshLayout,从而实现滑动到底部时上拉加载更多的功能.
*
* @author mrsimple
*/
public class RefreshLayout extends SwipeRefreshLayout implements
OnScrollListener {

/**
* 滑动到最下面时的上拉操作
*/

private int mTouchSlop;
/**
* listview实例
*/
private ListView mListView;

/**
* 上拉监听器, 到了最底部的上拉加载操作
*/
private OnLoadListener mOnLoadListener;

/**
* ListView的加载中footer
*/
private View mListViewFooter;

/**
* 按下时的y坐标
*/
private int mYDown;
/**
* 抬起时的y坐标, 与mYDown一起用于滑动到底部时判断是上拉还是下拉
*/
private int mLastY;
/**
* 是否在加载中 ( 上拉加载更多 )
*/
private boolean isLoading = false;

/**
* @param context
*/
public RefreshLayout(Context context) {
this(context, null);
}

public RefreshLayout(Context context, AttributeSet attrs) {
super(context, attrs);

mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();

mListViewFooter = LayoutInflater.from(context).inflate(
R.layout.listview_footer, null, false);
}

@Override
protected void onLayout(boolean changed, int left, int top, int right,
int bottom) {
super.onLayout(changed, left, top, right, bottom);

// 初始化ListView对象
if (mListView == null) {
getListView();
}
}

/**
* 获取ListView对象
*/
private void getListView() {
int childs = getChildCount();
if (childs > 0) {
View childView = getChildAt(0);
if (childView instanceof ListView) {
mListView = (ListView) childView;
// 设置滚动监听器给ListView, 使得滚动的情况下也可以自动加载
mListView.setOnScrollListener(this);
Log.d(VIEW_LOG_TAG, "### 找到listview");
}
}
}

/*
* (non-Javadoc)
*
* @see android.view.ViewGroup#dispatchTouchEvent(android.view.MotionEvent)
*/
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
final int action = event.getAction();

switch (action) {
case MotionEvent.ACTION_DOWN:
// 按下
mYDown = (int) event.getRawY();
break;

case MotionEvent.ACTION_MOVE:
// 移动
mLastY = (int) event.getRawY();
break;

case MotionEvent.ACTION_UP:
// 抬起
if (canLoad()) {
loadData();
}
break;
default:
break;
}

return super.dispatchTouchEvent(event);
}

/**
* 是否可以加载更多, 条件是到了最底部, listview不在加载中, 且为上拉操作.
*
* @return
*/
private boolean canLoad() {
return isBottom() && !isLoading && isPullUp();
}

/**
* 判断是否到了最底部
*/
private boolean isBottom() {

if (mListView != null && mListView.getAdapter() != null) {
return mListView.getLastVisiblePosition() == (mListView
.getAdapter().getCount() - 1);
}
return false;
}

/**
* 是否是上拉操作
*
* @return
*/
private boolean isPullUp() {
return (mYDown - mLastY) >= mTouchSlop;
}

/**
* 如果到了最底部,而且是上拉操作.那么执行onLoad方法
*/
private void loadData() {
if (mOnLoadListener != null) {
// 设置状态
setLoading(true);
//
mOnLoadListener.onLoad();
}
}

/**
* @param loading
*/
public void setLoading(boolean loading) {
isLoading = loading;
if (isLoading) {
mListView.addFooterView(mListViewFooter);
} else {
mListView.removeFooterView(mListViewFooter);
mYDown = 0;
mLastY = 0;
}
}

/**
* @param loadListener
*/
public void setOnLoadListener(OnLoadListener loadListener) {
mOnLoadListener = loadListener;
}

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {

}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// 滚动时到了最底部也可以加载更多
if (canLoad()) {
loadData();
}
}

/**
* 加载更多的监听器
*
* @author mrsimple
*/
public static interface OnLoadListener {
public void onLoad();
}
}

--------------------

<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>

head.xml可以是自定义的布局文件;adapter也比较简单。

主要是给listview加个头文件head,这样不会出现转换header时候出错;