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

Java tutorial

Introduction

Here is the source code for com.umeng.comm.ui.imagepicker.widgets.RecyclerRefreshLayout.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.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.OnScrollListener;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;

import com.umeng.comm.core.utils.ResFinder;
import com.umeng.comm.core.utils.ResFinder.ResType;

/**
 * SwipeRefreshLayout,. ? :
 * ??RefreshLayoutsetRefreshing(false)??
 * ??setLoading(false)??
 * 
 */
public class RecyclerRefreshLayout extends SwipeRefreshLayout {

    /**
     * ??
     */
    private int mTouchSlop;
    /**
     * 
     */
    protected RecyclerView mRecyclerView;
    /**
     * ListView?,
     */
    private OnScrollListener mListViewOnScrollListener;
    /**
     * ?, ?
     */
    private RefreshLayout.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;

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

    public RecyclerRefreshLayout(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 (mRecyclerView == null) {
            getRefreshView();
        }

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

    /**
     * ?ListView
     */
    protected void getRefreshView() {
        int childs = getChildCount();
        if (childs > 0) {
            View childView = getChildAt(0);
            if (childView instanceof RecyclerView) {
                mRecyclerView = (RecyclerView) childView;
                // ?ListView, ?
                mRecyclerView.setOnScrollListener(mScrollListener);
                mRecyclerView.setOnScrollListener(new OnScrollListener() {
                });
            }
        }
    }

    public RecyclerView findRefreshViewById(int id) {
        mRecyclerView = (RecyclerView) this.findViewById(id);
        mRecyclerView.setOnScrollListener(mScrollListener);
        return mRecyclerView;
    }

    /*
     * (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(mYDown - mLastY)) {
                loadData();
            }
            break;
        default:
            break;
        }

        return super.dispatchTouchEvent(event);
    }

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

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

        LinearLayoutManager layoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager();
        int lastVisibleItem = layoutManager.findLastVisibleItemPosition();
        int totalItemCount = layoutManager.getItemCount();
        // dy>0 ?
        if (lastVisibleItem >= totalItemCount - 4 && dy > 0) {
            return true;
        }
        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 RecyclerView getRecyclerView() {
        if (mRecyclerView == null) {
            getRefreshView();
        }
        return mRecyclerView;
    }

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

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

    OnScrollListener mScrollListener = new OnScrollListener() {

        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            // ?
            if (mListViewOnScrollListener != null) {
                mListViewOnScrollListener.onScrolled(recyclerView, dx, dy);
            }
            // ?
            if (canLoad(dy)) {
                loadData();
            }
        };

        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            // ?
            if (mListViewOnScrollListener != null) {
                mListViewOnScrollListener.onScrollStateChanged(recyclerView, newState);
            }
        };
    };
}