Java tutorial
/* * 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.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.utils.ResFinder; import com.umeng.comm.core.utils.ResFinder.ResType; /** * SwipeRefreshLayout,. ? : * ??RefreshLayoutsetRefreshing(false)?? * ??setLoading(false)?? * * @author mrsimple */ public abstract class RefreshLayout<T extends AbsListView> extends SwipeRefreshLayout implements OnScrollListener { /** * ?? */ private int mTouchSlop; /** * */ protected T mAbsListView; /** * ListView?, */ private OnScrollListener mListViewOnScrollListener; /** * ?, ? */ 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; /** * @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"); } @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) { View childView = getChildAt(0); 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; } /* * (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()) { loadData(); } break; default: break; } return super.dispatchTouchEvent(event); } /** * ??, ?, listview?, ?. * * @return */ private boolean canLoad() { return isBottom() && !isLoading && isPullUp() && mOnLoadListener != null; } /** * ? */ private boolean isBottom() { // ????>0 if (mAbsListView != null && mAbsListView.getAdapter() != null) { return mAbsListView.getLastVisiblePosition() == (mAbsListView.getAdapter().getCount() - 1) && mAbsListView.getFirstVisiblePosition() >= 0; } return false; } /** * ?? * * @return */ private boolean isPullUp() { return (mYDown - mLastY) >= mTouchSlop; } /** * ,?.onLoad */ private 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 (canLoad()) { loadData(); } } /** * ? * * @author mrsimple */ public static interface OnLoadListener { public void onLoad(); } }