android自定义控件之ListView上拉加载与下拉刷新

自定义控件LoadLayout(只有上拉加载)(下拉刷新用控件android.support.v4.widget.SwipeRefreshLayout)(上拉与下拉继承让LoadLayout继承SwipeRefreshLayout即可)

使用方法(上拉implements LoadLayout.OnLoadListener,在onLoad()方法中实现加载操作)

     (下拉implements SwipeRefreshLayout.OnRefreshListener,在onRefresh()方法中实现刷新操作)

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;

/**
 * 自定义控件ListView上拉加载
 * Created by Administrator on 2015/10/22 0022.
 */
public class LoadLayout extends FrameLayout implements AbsListView.OnScrollListener {

    private enum Operation {
        START, STOP
    }

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

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

    /**
     * 底部文字
     */
    private TextView mFootText;

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

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

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

    /**
     * @param context
     */
    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);

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

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

    /*
     * (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:
                // 移动
                break;

            case MotionEvent.ACTION_UP:
                mLastY = (int) event.getRawY();
                // 抬起
                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) {
            changeAnimation(Operation.START);
            mListView.addFooterView(mListViewFooter, null, false);
        } else {
            changeAnimation(Operation.STOP);
            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();
    }

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

view_lv_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:background="#ffffff"
    android:gravity="center"
    android:paddingBottom="8dip"
    android:paddingTop="5dip" >

    <TextView
        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" />

</RelativeLayout>

rotating_loading.xml

<?xml version="1.0" encoding="utf-8"?>
<animated-rotate
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:pivotX="50%"
  android:pivotY="50%"
  android:drawable="@mipmap/ic_ref"
  android:duration="10" />


在activity中使用

setContentView(R.layout.activity_main);
LoadLayout load = (LoadLayout) findViewById(R.id.load);
load.setOnLoadListener(this);

关闭loading

load.setLoading(false);

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<com.xh.boke.component.LoadLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/load"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</com.xh.boke.component.LoadLayout>

 

posted @ 2015-10-22 11:36  请输入...昵称  阅读(441)  评论(0编辑  收藏  举报