二级列表做完例子
1、XML布局
<com.android.mgwaiter.view.ListSwipeRefreshLayout
android:id="@+id/srl_swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/list_tasking_color">
android:id="@+id/srl_swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/list_tasking_color">
<ExpandableListView
android:id="@+id/task_expandablelistview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FFFFFF"
android:divider="@null" />
android:id="@+id/task_expandablelistview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FFFFFF"
android:divider="@null" />
</com.android.mgwaiter.view.ListSwipeRefreshLayout>
2、刷新方法
private void swipeRefresh() {
// 设置顶部下拉刷新
srl_swipe_refresh_layout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
reLoadList();
getTaskInfoStatic(); //网络请求
}
});
// 设置底部上拉加载更多
srl_swipe_refresh_layout.setOnLoadListener(new ListSwipeRefreshLayout.OnLoadListener() {
@Override
public void onLoad() {
page++;
getTaskInfoStatic();
}
});
}
// 设置顶部下拉刷新
srl_swipe_refresh_layout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
reLoadList();
getTaskInfoStatic(); //网络请求
}
});
// 设置底部上拉加载更多
srl_swipe_refresh_layout.setOnLoadListener(new ListSwipeRefreshLayout.OnLoadListener() {
@Override
public void onLoad() {
page++;
getTaskInfoStatic();
}
});
}
/**
*list清除数据
*/
private void reLoadList() {
if (dataList != null) {
dataList.clear();
page = 1;
mealExpandableListAdapter.refresh();
}
*list清除数据
*/
private void reLoadList() {
if (dataList != null) {
dataList.clear();
page = 1;
mealExpandableListAdapter.refresh();
}
}
3、上拉加载更多自定义ListSwipeRefreshLayout
package com.android.mgwaiter.view;
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.ExpandableListView;
import android.widget.ListView;
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.ExpandableListView;
import android.widget.ListView;
import com.android.mgwaiter.R;
public class ListSwipeRefreshLayout extends SwipeRefreshLayout implements AbsListView.OnScrollListener {
private int mTouchSlop;
private ListView mListView;
private OnLoadListener mOnLoadListener;
private View mListViewFooter;
private int mYDown;
private int mLastY;
private boolean isLoading = false;
private ListView mListView;
private OnLoadListener mOnLoadListener;
private View mListViewFooter;
private int mYDown;
private int mLastY;
private boolean isLoading = false;
public ListSwipeRefreshLayout(Context context) {
this(context, null);
}
this(context, null);
}
public ListSwipeRefreshLayout(Context context, AttributeSet attrs) {
super(context, attrs);
mTouchSlop = 150;//ViewConfiguration.get(context).getScaledTouchSlop();
mListViewFooter = LayoutInflater.from(context).inflate(R.layout.view_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();
}
}
private void getListView() {
int childs = getChildCount();
if (childs > 0) {
View childView = getChildAt(0);
if (childView instanceof ListView) {
mListView = (ExpandableListView) childView;
// 设置滚动监听器给ListView, 使得滚动的情况下也可以自动加载
mListView.setOnScrollListener(this);
}
}
}
public void setListView(ListView list) {
this.mListView = list;
setLoading(true);
this.mListView.setOnScrollListener(this);
}
@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:
mLastY = (int) event.getRawY();
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() {
super(context, attrs);
mTouchSlop = 150;//ViewConfiguration.get(context).getScaledTouchSlop();
mListViewFooter = LayoutInflater.from(context).inflate(R.layout.view_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();
}
}
private void getListView() {
int childs = getChildCount();
if (childs > 0) {
View childView = getChildAt(0);
if (childView instanceof ListView) {
mListView = (ExpandableListView) childView;
// 设置滚动监听器给ListView, 使得滚动的情况下也可以自动加载
mListView.setOnScrollListener(this);
}
}
}
public void setListView(ListView list) {
this.mListView = list;
setLoading(true);
this.mListView.setOnScrollListener(this);
}
@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:
mLastY = (int) event.getRawY();
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() {
Log.e("ListSwipeRefreshLayout","mTouchSlop :" + mTouchSlop + "(mYDown - mLastY) :" + (mYDown - mLastY));
return (mYDown - mLastY) >= mTouchSlop;
}
/**
* @方法说明: 如果到了最底部,而且是上拉操作.那么执行onLoad方法
*/
private void loadData() {
if (mOnLoadListener != null) {
mOnLoadListener.onLoad();
}
// 设置状态
setLoading(true);
}
/**
* @方法说明:设置刷新
*/
public void setLoading(boolean loading) {
isLoading = loading;
if (mListView != null && mListView.getFooterViewsCount() > 0)
mListView.removeFooterView(mListViewFooter);
if (isLoading) {
if (mListView != null && mListView.getFooterViewsCount() <= 0)
mListView.addFooterView(mListViewFooter);
} else {
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 (isFastDoubleClick(100))
return;
return (mYDown - mLastY) >= mTouchSlop;
}
/**
* @方法说明: 如果到了最底部,而且是上拉操作.那么执行onLoad方法
*/
private void loadData() {
if (mOnLoadListener != null) {
mOnLoadListener.onLoad();
}
// 设置状态
setLoading(true);
}
/**
* @方法说明:设置刷新
*/
public void setLoading(boolean loading) {
isLoading = loading;
if (mListView != null && mListView.getFooterViewsCount() > 0)
mListView.removeFooterView(mListViewFooter);
if (isLoading) {
if (mListView != null && mListView.getFooterViewsCount() <= 0)
mListView.addFooterView(mListViewFooter);
} else {
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 (isFastDoubleClick(100))
return;
// 滚动时到了最底部也可以加载更多
isLoading = false;
if (canLoad()) {
loadData();
}
}
public View getmListViewFooter() {
return mListViewFooter;
}
/**
* @类描述:加载更多的监听器
*/
public static interface OnLoadListener {
public void onLoad();
}
private static long lastClickTime;
public static boolean isFastDoubleClick(long times) {
long time = System.currentTimeMillis();
long timeD = time - lastClickTime;
if (0 < timeD && timeD < times) {
return true;
}
lastClickTime = time;
return false;
}
}
isLoading = false;
if (canLoad()) {
loadData();
}
}
public View getmListViewFooter() {
return mListViewFooter;
}
/**
* @类描述:加载更多的监听器
*/
public static interface OnLoadListener {
public void onLoad();
}
private static long lastClickTime;
public static boolean isFastDoubleClick(long times) {
long time = System.currentTimeMillis();
long timeD = time - lastClickTime;
if (0 < timeD && timeD < times) {
return true;
}
lastClickTime = time;
return false;
}
}