众所周知,谷歌有一个自己的下拉刷新控件,没错!我说的就是v4包下的SwipeRefreshLayout,美中不足的是没有上拉加载更多的功能,这一点比较遗憾,有点蛋蛋的忧伤~~but!!!,网上已经有很多大神把他的功能重写了,实现了上拉加载更多的功能,利用了下午的时间学习了一下
先看一下大神是如何把他重写的,从重写的方法来看,里面嵌套的只能是ListView,俗话说的好:不要重复造轮子,咱也站在巨人的肩膀上学习一下:
package com.example.lenovo.pulltorefresh;
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.ListView;
public class MySwipeRefreshLayout extends SwipeRefreshLayout implements AbsListView.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;
public MySwipeRefreshLayout(Context context) {
this(context, null);
}
public MySwipeRefreshLayout(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);
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");
}
}
}
/** * 事件分发的处理 */
@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不在加载中, 且为上拉操作. */
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;
}
/** * 是否是上拉操作 */
private boolean isPullUp() {
return (mYDown - mLastY) >= mTouchSlop;
}
/** * 如果到了最底部,而且是上拉操作.那么执行onLoad方法 */
private void loadData() {
if (mOnLoadListener != null) {
setLoading(true);
mOnLoadListener.onLoad();
}
}
/** * 加载回调 */
public void setLoading(boolean loading) {
isLoading = loading;
if (isLoading) {
mListView.addFooterView(mListViewFooter);
} else {
mListView.removeFooterView(mListViewFooter);
mYDown = 0;
mLastY = 0;
}
}
/** * 加载监听 */
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();
}
}
/** * 加载更多的监听 */
public static interface OnLoadListener {
public void onLoad();
}
}
注释很详细,就不说明了~~
MainActivity代码:
package com.example.lenovo.pulltorefresh;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends Activity {
private ListView listView;
private MySwipeRefreshLayout mySwipeRefreshLayout;
private ArrayList<String> listData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
listView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, listData));
//下拉刷新监听
mySwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//刷新结束后调用一下该方法
mySwipeRefreshLayout.setRefreshing(false);
}
}, 3000);
}
});
//上拉加载更多监听
mySwipeRefreshLayout.setOnLoadListener(new MySwipeRefreshLayout.OnLoadListener() {
@Override
public void onLoad() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// 加载完后调用该方法
mySwipeRefreshLayout.setLoading(false);
}
}, 3000);
}
});
}
private void initData() {
listData = new ArrayList<>();
for(int i=0;i<30;i++){
listData.add("数据"+i);
}
}
private void initView() {
mySwipeRefreshLayout = (MySwipeRefreshLayout)findViewById(R.id.swipe_layout);
//设置旋转箭头的颜色
mySwipeRefreshLayout.setColorSchemeColors(Color.BLUE);
listView = (ListView) findViewById(R.id.listview);
}
}
Over!!就是这么生猛!!效果图就不上了~