前言
- SwipeRefreshLayout是Google推出的刷新的控件。这个组件只支持下拉刷新,不支持上拉加载更多的操作,现在我们就来实现它的上拉加载操作。
大家都知道上拉加载,这里就不多说了,直接上代码:
重写SwipeRefreshLayout类:
/**
* 继承自SwipeRefreshLayout,从而实现滑动到底部时上拉加载更多的功能.
* Created by Administrator on 2016/4/20.
*/
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;
/**
* @param context
*/
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);
// 初始化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();
}
}
listview_footer.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingBottom="8dip"
android:paddingTop="5dip" >
<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:layout_centerHorizontal="true"
android:paddingRight="100dp"
android:indeterminate="true" />
<TextView
android:id="@+id/pull_to_refresh_loadmore_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:paddingTop="5dip"
android:text="上拉加载更多"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@android:color/darker_gray"
android:textSize="14sp"
android:textStyle="bold" />
</RelativeLayout>
activity类
public class MainActivity extends AppCompatActivity {
private ListView listView;
private ArrayAdapter adapter;
private MySwipeRefreshLayout mySwipeRefreshLayout;
/**
* 模拟一些数据
* @return
*/
private List<String> getData() {
List<String> datas = new ArrayList<>();
for (int i = 0; i < 20; i++) {
datas.add("list item - " + i);
}
return datas;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listview);
adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1,
getData());
listView.setAdapter(adapter);
mySwipeRefreshLayout = (MySwipeRefreshLayout)
findViewById(R.id.swipe_layout);
// 设置下拉刷新时的颜色值,可以根据你自己的喜好来设置
mySwipeRefreshLayout
.setColorScheme(R.color.colorAccent,
R.color.primary, R.color.accent,
R.color.colorPrimaryDark);
// 设置下拉刷新监听器
mySwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
mySwipeRefreshLayout.postDelayed(new Runnable() {
@Override
public void run() {
// 更新数据
getData().add(new Date().toGMTString());
adapter.notifyDataSetChanged();
// 更新完后调用该方法结束刷新
mySwipeRefreshLayout.setRefreshing(false);
}
}, 2000);
}
});
// 加载监听器
mySwipeRefreshLayout.setOnLoadListener(new MySwipeRefreshLayout.OnLoadListener() {
@Override
public void onLoad() {
mySwipeRefreshLayout.postDelayed(new Runnable() {
@Override
public void run() {
getData().add(new Date().toGMTString());
adapter.notifyDataSetChanged();
// 加载完后调用该方法
mySwipeRefreshLayout.setLoading(false);
}
}, 2000);
}
});
}
}