作者:干豆豆腐 | 来源:互联网 | 2024-12-04 13:44
本文介绍如何通过自定义控件LoadLayout实现ListView的上拉加载更多和下拉刷新功能。LoadLayout支持上拉加载,而下拉刷新则利用了android.support.v4.widget.SwipeRefreshLayout组件。
本文将详细介绍如何在Android应用中通过自定义控件实现ListView的上拉加载更多和下拉刷新功能。我们将使用一个名为LoadLayout的自定义控件来处理上拉加载,并结合android.support.v4.widget.SwipeRefreshLayout来实现下拉刷新。
自定义控件LoadLayout
LoadLayout是一个自定义的FrameLayout,专门用于实现ListView的上拉加载功能。该控件通过监听ListView的滚动事件来判断用户是否已经滚动到列表底部,并在此基础上触发上拉加载的操作。
import android.content.Context;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.AbsListView;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.xh.boke.R;
public class LoadLayout extends FrameLayout implements AbsListView.OnScrollListener {
private enum Operation {START, STOP}
private int mTouchSlop;
private ListView mListView;
private TextView mFootText;
private OnLoadListener mOnLoadListener;
private View mListViewFooter;
private int mYDown;
private int mLastY;
private boolean isLoading = false;
public LoadLayout(Context context) {
this(context, null);
}
public LoadLayout(Context context, AttributeSet attrs) {
super(context, attrs);
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
mListViewFooter = LayoutInflater.from(context).inflate(R.layout.view_lv_footer, null, false);
mFootText = (TextView) mListViewFooter.findViewById(R.id.pull_to_refresh_loadmore_text);
}
@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();
}
}
private void getListView() {
int childs = getChildCount();
if (childs > 0) {
for (int i = 0; i View childView = getChildAt(i);
if (childView instanceof ListView) {
mListView = (ListView) childView;
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_UP:
mLastY = (int) event.getRawY();
if (canLoad()) {
loadData();
}
break;
default:
break;
}
return super.dispatchTouchEvent(event);
}
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;
}
private void loadData() {
if (mOnLoadListener != null) {
setLoading(true);
mOnLoadListener.onLoad();
}
}
public void setLoading(boolean loading) {
isLoading = loading;
if (isLoading) {
changeAnimation(Operation.START);
mListView.addFooterView(mListViewFooter, null, false);
} else {
changeAnimation(Operation.STOP);
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 {
void onLoad();
}
private void changeAnimation(Operation operation) {
Drawable[] drawables = mFootText.getCompoundDrawables();
for (Drawable drawable : drawables) {
if (drawable != null && drawable instanceof Animatable) {
Animatable animatable = ((Animatable) drawable);
switch (operation) {
case START:
animatable.start();
break;
case STOP:
animatable.stop();
break;
}
}
}
}
}
布局文件
为了使LoadLayout能够正常工作,我们需要定义两个XML布局文件:view_lv_footer.xml
和 rotating_loading.xml
。
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:gravity="center"
android:paddingBottom="8dip"
android:paddingTop="5dip">
android:id="@+id/pull_to_refresh_loadmore_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:paddingTop="5dip"
android:drawableLeft="@anim/rotating_loading"
android:drawablePadding="8dp"
android:text="加载中..."
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@android:color/black"
android:textSize="@dimen/activity_horizontal_margin"
android:textStyle="bold" />
xmlns:android="http://schemas.android.com/apk/res/android"
android:pivotX="50%"
android:pivotY="50%"
android:drawable="@mipmap/ic_ref"
android:duration="10" />
在Activity中使用
要在Activity中使用LoadLayout,首先需要在布局文件中引入LoadLayout,并设置其包含一个ListView。然后在Activity中找到LoadLayout实例,并为其设置上拉加载的监听器。
setContentView(R.layout.activity_main);
LoadLayout load = (LoadLayout) findViewById(R.id.load);
load.setOnLoadListener(this);
当数据加载完成后,可以通过调用setLoading(false)
方法来关闭加载指示器。
load.setLoading(false);
主布局文件
最后,我们来看一下主布局文件activity_main.xml
的定义:
android:id="@+id/load"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent" />