Java tutorial
/* * Copyright (c) 2016 ?. All Rights Reserved. * Use of this source code is governed by a Shanghai Unovo Information Technology Co.,Ltd license * that can be found in the LICENSE file in the root of the web site. * * http://www.unovo.com.cn * * An additional intellectual property rights grant can be found * in the file PATENTS. All contributing project authors may * be found in the AUTHORS file in the root of the source tree. * */ package com.unovo.frame.uikit.refresh; 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.ListView; /** * STAY HUNGRY, STAY FOOLISH! * * @Prject: apartment_app-V2 * @Location: com.unovo.apartment.v2.ui.base * @Description: TODO * @author: loQua.Xee * @email: shyscool@163.com * @date: 16/7/28 16:08 * @version: V1.0 */ @SuppressWarnings("unused") public class SuperRefreshLayout extends SwipeRefreshLayout implements SwipeRefreshLayout.OnRefreshListener { private ListView mListView; private int mTouchSlop; private SuperRefreshLayoutListener mListener; private boolean mIsOnLoading = false; private boolean mCanLoadMore = false; private int mYDown; private int mLastY; private int mTextColor; private int mFooterBackground; private boolean mIsMoving = false; public SuperRefreshLayout(Context context) { this(context, null); } public SuperRefreshLayout(Context context, AttributeSet attrs) { super(context, attrs); mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop(); setOnRefreshListener(this); } @Override public void onRefresh() { if (mListener != null && !mIsOnLoading) { mListener.onRefreshing(); } } @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(); } } /** * ?ListViewFooter */ private void getListView() { int child = getChildCount(); if (child > 0) { View childView = getChildAt(0); if (childView instanceof ListView) { mListView = (ListView) childView; // ?ListView, ? ScrollDetecor scrollDetecor = new ScrollDetecor(); scrollDetecor.setListView(mListView); scrollDetecor.setScrollThreshold(0); mListView.setOnScrollListener(scrollDetecor); } } } public void setCanLoadMore(boolean canLoadMore) { this.mCanLoadMore = canLoadMore; } @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: // mIsMoving = true; mLastY = (int) event.getRawY(); break; case MotionEvent.ACTION_UP: mIsMoving = false; break; default: break; } return super.dispatchTouchEvent(event); } /** * ??, ?, listview?, ?. * * @return ?? */ private boolean canLoad() { return isInBottom() && !mIsOnLoading && isPullUp() && mCanLoadMore; } /** * ,?.onLoad */ private void loadData() { if (mListener != null) { setIsOnLoading(true); mListener.onLoadMore(); } } /** * ?? * * @return ?? */ private boolean isPullUp() { return mLastY != 0 && (mYDown - mLastY) >= mTouchSlop; } /** * * * @param loading loading */ public void setIsOnLoading(boolean loading) { mIsOnLoading = loading; if (!mIsOnLoading) { mYDown = 0; mLastY = 0; } } /** * ? */ private boolean isInBottom() { return (mListView != null && mListView.getAdapter() != null) && mListView.getLastVisiblePosition() == (mListView.getAdapter().getCount() - 1); } public interface SuperRefreshLayoutListener { void onRefreshing(); void onLoadMore(); } /** * ? */ public void onLoadComplete() { setIsOnLoading(false); setRefreshing(false); } /** * set * * @param loadListener loadListener */ public void setSuperRefreshLayoutListener(SuperRefreshLayoutListener loadListener) { mListener = loadListener; } public boolean isMoving() { return mIsMoving; } //top private int first; private class ScrollDetecor extends AbsListViewScrollDetector { @Override void onScrollUp() { if (scrollUpOrDownLisntener != null) { scrollUpOrDownLisntener.onScrollUpDown(true, getTotalScrollY()); } } @Override void onScrollDown() { if (scrollUpOrDownLisntener != null) { scrollUpOrDownLisntener.onScrollUpDown(false, getTotalScrollY()); } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { super.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount); if (canLoad()) { loadData(); } } } private int getTotalScrollY() { View c = mListView.getChildAt(0); if (c == null) { return 0; } int firstVisiblePosition = mListView.getFirstVisiblePosition(); int top = c.getTop(); return -top + firstVisiblePosition * c.getHeight(); } public interface ScrollUpOrDownLisntener { void onScrollUpDown(boolean up, int distance); } private ScrollUpOrDownLisntener scrollUpOrDownLisntener; public void setScrollUpOrDownLisntener(ScrollUpOrDownLisntener lisntener) { this.scrollUpOrDownLisntener = lisntener; } private float startY; private float startX; // viewPager? private boolean mIsVpDragger; @Override public boolean onInterceptTouchEvent(MotionEvent ev) { int action = ev.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: // ? startY = ev.getY(); startX = ev.getX(); // ? mIsVpDragger = false; break; case MotionEvent.ACTION_MOVE: // viewpager?return false if (mIsVpDragger) { return false; } // ??? float endY = ev.getY(); float endX = ev.getX(); float distanceX = Math.abs(endX - startX); float distanceY = Math.abs(endY - startY); // X?Y?viewPager? if (distanceX >= distanceY) { mIsVpDragger = true; return false; } break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: // ? mIsVpDragger = false; break; } return super.onInterceptTouchEvent(ev); } }