com.umeng.comm.ui.imagepicker.widgets.RefreshLayout.java Source code

Java tutorial

Introduction

Here is the source code for com.umeng.comm.ui.imagepicker.widgets.RefreshLayout.java

Source

/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014-2015 Umeng, Inc
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.umeng.comm.ui.imagepicker.widgets;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;

import com.umeng.comm.core.listeners.Listeners.OnResultListener;
import com.umeng.comm.core.utils.ResFinder;
import com.umeng.comm.core.utils.ResFinder.ResType;

/**
 * SwipeRefreshLayout,. ? :
 * ??RefreshLayoutsetRefreshing(false)??
 * ??setLoading(false)??
 */
public abstract class RefreshLayout<T extends AbsListView> extends SwipeRefreshLayout implements OnScrollListener {

    /**
     * ??
     */
    private int mTouchSlop;

    /**
     * 
     */
    protected T mAbsListView;

    /**
     * ListView?,
     */
    private OnScrollListener mListViewOnScrollListener;

    private OnResultListener mScrollListener;

    /**
     * ?, ?
     */
    private OnLoadListener mOnLoadListener;
    /**
     * y??
     */
    protected int mYDown;
    /**
     * y??, mYDown
     */
    protected int mLastY;
    /**
     * ? (  )
     */
    protected boolean isLoading = false;

    private int mColor1;
    private int mColor2;
    private int mColor3;
    private int mColor4;

    /**
     * ListViewfooter
     */
    protected View mFooterView;

    private int mScrollDirection = 0; // 0:?1?

    /**
     * @param context
     */
    public RefreshLayout(Context context) {
        this(context, null);
    }

    public RefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);

        if (isInEditMode()) {
            return;
        }

        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();

        // ?color?id
        mColor1 = ResFinder.getResourceId(ResType.COLOR, "umeng_comm_lv_header_color1");
        mColor2 = ResFinder.getResourceId(ResType.COLOR, "umeng_comm_lv_header_color2");
        mColor3 = ResFinder.getResourceId(ResType.COLOR, "umeng_comm_lv_header_color3");
        mColor4 = ResFinder.getResourceId(ResType.COLOR, "umeng_comm_lv_header_color4");

    }

    @SuppressWarnings("deprecation")
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {

        if (isInEditMode()) {
            return;
        }
        super.onLayout(changed, left, top, right, bottom);

        // ?ListView
        if (mAbsListView == null) {
            getRefreshView();
        }

        // 
        this.setColorScheme(mColor1, mColor2, mColor3, mColor4);
    }

    /**
     * ?ListView
     */
    @SuppressWarnings("unchecked")
    protected void getRefreshView() {
        int childs = getChildCount();
        if (childs <= 0) {
            return;
        }
        View childView = null;
        for (int i = 0; i < childs; i++) {
            childView = getChildAt(i);
            if (childView instanceof AbsListView) {
                mAbsListView = (T) childView;
                // ?ListView, ?
                mAbsListView.setOnScrollListener(this);
            }
        }
    }

    @SuppressWarnings("unchecked")
    public T findRefreshViewById(int id) {
        mAbsListView = (T) this.findViewById(id);
        mAbsListView.setOnScrollListener(this);
        return mAbsListView;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        try {
            return dealTouchEvent(event);
        } catch (IllegalStateException exception) {
        }
        return true;
    }

    private boolean dealTouchEvent(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();
            }
            if (mScrollListener != null) {
                if (isPullUp()) {
                    mScrollDirection = 1;
                } else {
                    mScrollDirection = 0;
                }
            }
            break;
        default:
            break;
        }
        return super.dispatchTouchEvent(event);
    }

    /**
     * ??, ?, listview?, ?.
     * 
     * @return
     */
    private boolean canLoad() {
        return isBottom() && !isLoading && isPullUp() && mOnLoadListener != null;
    }

    /**
     * ?
     */
    private boolean isBottom() {
        if (mAbsListView == null) {
            getRefreshView();
        }
        // ????>0
        if (mAbsListView != null && mAbsListView.getAdapter() != null) {
            int childCount = mAbsListView.getAdapter().getCount();
            return childCount > 1 && mAbsListView.getLastVisiblePosition() == childCount - 1;
            // && mAbsListView.getFirstVisiblePosition() >= 0;
        }
        return false;
    }

    /**
     * ??
     * 
     * @return
     */
    private boolean isPullUp() {
        return mLastY > 0 && (mYDown - mLastY) >= mTouchSlop * 3;
    }

    /**
     * ,?.onLoad
     */
    protected void loadData() {
        setLoading(true);
        mOnLoadListener.onLoad();
    }

    /**
     * @param loading
     */
    public void setLoading(boolean loading) {
        isLoading = loading;
    }

    public AbsListView getListView() {
        if (mAbsListView == null) {
            getRefreshView();
        }
        return mAbsListView;
    }

    /**
     * ??listview
     * 
     * @param listener
     */
    public void addOnScrollListener(OnScrollListener listener) {
        mListViewOnScrollListener = listener;
    }

    /**
     * @param loadListener
     */
    public void setOnLoadListener(OnLoadListener loadListener) {
        mOnLoadListener = loadListener;
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        // ?
        if (mListViewOnScrollListener != null) {
            mListViewOnScrollListener.onScrollStateChanged(view, scrollState);
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

        // ?
        if (mListViewOnScrollListener != null) {
            mListViewOnScrollListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
        }

        if (mScrollListener != null) {
            mScrollListener.onResult(mScrollDirection);
        }

        // ?
        if (canLoad()) {
            loadData();
        }
    }

    /**
     * ?
     * 
     * @author mrsimple
     */
    public static interface OnLoadListener {
        public void onLoad();
    }

    public void setOnScrollDirectionListener(OnResultListener listener) {
        mScrollListener = listener;
    }
}