Java tutorial
/** The MIT License (MIT) Copyright (c) 2014 singwhatiwanna https://github.com/singwhatiwanna http://blog.csdn.net/singwhatiwanna 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.xinkaishi.apple.xinweidian.CustomView; import android.content.Context; import android.graphics.Canvas; import android.graphics.Rect; import android.support.v4.view.MotionEventCompat; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.AbsListView.OnScrollListener; import android.widget.ExpandableListView; public class PinnedHeaderExpandableListView extends ExpandableListView implements OnScrollListener { private static final String TAG = "PinnedHeaderExpandableListView"; private static final boolean DEBUG = true; public interface OnHeaderUpdateListener { /** * view?? * ?view?LayoutParams */ public View getPinnedHeader(int firstVisibleGroupPos); public void updatePinnedHeader(View headerView, int firstVisibleGroupPos); } private View mHeaderView; private int mHeaderWidth; private int mHeaderHeight; private View mTouchTarget; private OnScrollListener mScrollListener; private OnHeaderUpdateListener mHeaderUpdateListener; private boolean mActionDownHappened = false; protected boolean mIsHeaderGroupClickable = true; // private static final int TOUCH_STATE_NONE = 0; private static final int TOUCH_STATE_X = 1; private static final int TOUCH_STATE_Y = 2; private int MAX_Y = 5; private int MAX_X = 3; private float mDownX; private float mDownY; private int mTouchState; private int mTouchPosition; private SwipeItemLayout mTouchView; // private OnSwipeListener mOnSwipeListener; public PinnedHeaderExpandableListView(Context context) { super(context); initView(); } public PinnedHeaderExpandableListView(Context context, AttributeSet attrs) { super(context, attrs); initView(); } public PinnedHeaderExpandableListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initView(); } private void initView() { setFadingEdgeLength(0); setOnScrollListener(this); } @Override public void setOnScrollListener(OnScrollListener l) { if (l != this) { mScrollListener = l; } else { mScrollListener = null; } super.setOnScrollListener(this); } /** * group? * @param onGroupClickListener ? * @param isHeaderGroupClickable header??<br/> * note : ?group??OnGroupClickListener#onGroupClicktrue * isHeaderGroupClickablefalse?? */ public void setOnGroupClickListener(OnGroupClickListener onGroupClickListener, boolean isHeaderGroupClickable) { mIsHeaderGroupClickable = isHeaderGroupClickable; super.setOnGroupClickListener(onGroupClickListener); } public void setOnHeaderUpdateListener(OnHeaderUpdateListener listener) { mHeaderUpdateListener = listener; if (listener == null) { mHeaderView = null; mHeaderWidth = mHeaderHeight = 0; return; } int firstVisiblePos = getFirstVisiblePosition(); int firstVisibleGroupPos = getPackedPositionGroup(getExpandableListPosition(firstVisiblePos)); mHeaderView = listener.getPinnedHeader(firstVisibleGroupPos); listener.updatePinnedHeader(mHeaderView, firstVisibleGroupPos); requestLayout(); postInvalidate(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (mHeaderView == null) { return; } measureChild(mHeaderView, widthMeasureSpec, heightMeasureSpec); mHeaderWidth = mHeaderView.getMeasuredWidth(); mHeaderHeight = mHeaderView.getMeasuredHeight(); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l, t, r, b); if (mHeaderView == null) { return; } int delta = mHeaderView.getTop(); mHeaderView.layout(0, delta, mHeaderWidth, mHeaderHeight + delta); } @Override protected void dispatchDraw(Canvas canvas) { super.dispatchDraw(canvas); if (mHeaderView != null) { drawChild(canvas, mHeaderView, getDrawingTime()); } } @Override public boolean dispatchTouchEvent(MotionEvent ev) { int x = (int) ev.getX(); int y = (int) ev.getY(); int pos = pointToPosition(x, y); if (mHeaderView != null && y >= mHeaderView.getTop() && y <= mHeaderView.getBottom()) { if (ev.getAction() == MotionEvent.ACTION_DOWN) { mTouchTarget = getTouchTarget(mHeaderView, x, y); mActionDownHappened = true; } else if (ev.getAction() == MotionEvent.ACTION_UP) { View touchTarget = getTouchTarget(mHeaderView, x, y); if (touchTarget == mTouchTarget && mTouchTarget.isClickable()) { mTouchTarget.performClick(); invalidate(new Rect(0, 0, mHeaderWidth, mHeaderHeight)); } else if (mIsHeaderGroupClickable) { int groupPosition = getPackedPositionGroup(getExpandableListPosition(pos)); if (groupPosition != INVALID_POSITION && mActionDownHappened) { if (isGroupExpanded(groupPosition)) { collapseGroup(groupPosition); } else { expandGroup(groupPosition); } } } mActionDownHappened = false; } return true; } return super.dispatchTouchEvent(ev); } private View getTouchTarget(View view, int x, int y) { if (!(view instanceof ViewGroup)) { return view; } ViewGroup parent = (ViewGroup) view; int childrenCount = parent.getChildCount(); final boolean customOrder = isChildrenDrawingOrderEnabled(); View target = null; for (int i = childrenCount - 1; i >= 0; i--) { final int childIndex = customOrder ? getChildDrawingOrder(childrenCount, i) : i; final View child = parent.getChildAt(childIndex); if (isTouchPointInView(child, x, y)) { target = child; break; } } if (target == null) { target = parent; } return target; } private boolean isTouchPointInView(View view, int x, int y) { if (view.isClickable() && y >= view.getTop() && y <= view.getBottom() && x >= view.getLeft() && x <= view.getRight()) { return true; } return false; } public void requestRefreshHeader() { refreshHeader(); invalidate(new Rect(0, 0, mHeaderWidth, mHeaderHeight)); } protected void refreshHeader() { if (mHeaderView == null) { return; } int firstVisiblePos = getFirstVisiblePosition(); int pos = firstVisiblePos + 1; int firstVisibleGroupPos = getPackedPositionGroup(getExpandableListPosition(firstVisiblePos)); int group = getPackedPositionGroup(getExpandableListPosition(pos)); if (DEBUG) { Log.e(TAG, "refreshHeader firstVisibleGroupPos=" + firstVisibleGroupPos); } if (group == firstVisibleGroupPos + 1) { View view = getChildAt(1); if (view == null) { Log.e(TAG, "Warning : refreshHeader getChildAt(1)=null"); return; } if (view.getTop() <= mHeaderHeight) { int delta = mHeaderHeight - view.getTop(); mHeaderView.layout(0, -delta, mHeaderWidth, mHeaderHeight - delta); } else { //TODO : note it, when cause bug, remove it mHeaderView.layout(0, 0, mHeaderWidth, mHeaderHeight); } } else { mHeaderView.layout(0, 0, mHeaderWidth, mHeaderHeight); } if (mHeaderUpdateListener != null) { mHeaderUpdateListener.updatePinnedHeader(mHeaderView, firstVisibleGroupPos); } } @Override public void onScrollStateChanged(AbsListView view, int scrollState) { if (mScrollListener != null) { mScrollListener.onScrollStateChanged(view, scrollState); } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { if (totalItemCount > 0) { refreshHeader(); } if (mScrollListener != null) { mScrollListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount); } } /** * item? * */ @Override public boolean onTouchEvent(MotionEvent ev) { if (ev.getAction() != MotionEvent.ACTION_DOWN && mTouchView == null) return super.onTouchEvent(ev); int action = MotionEventCompat.getActionMasked(ev); action = ev.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: int oldPos = mTouchPosition; mDownX = ev.getX(); mDownY = ev.getY(); mTouchState = TOUCH_STATE_NONE; mTouchPosition = pointToPosition((int) ev.getX(), (int) ev.getY()); if (mTouchPosition == oldPos && mTouchView != null && mTouchView.isOpen()) { mTouchState = TOUCH_STATE_X; mTouchView.onSwipe(ev); return true; } View view = getChildAt(mTouchPosition - getFirstVisiblePosition()); if (mTouchView != null && mTouchView.isOpen()) { mTouchView.smoothCloseMenu(); mTouchView = null; return super.onTouchEvent(ev); } if (view instanceof SwipeItemLayout) { mTouchView = (SwipeItemLayout) view; } if (mTouchView != null) { mTouchView.onSwipe(ev); } break; case MotionEvent.ACTION_MOVE: float dy = Math.abs((ev.getY() - mDownY)); float dx = Math.abs((ev.getX() - mDownX)); if (mTouchState == TOUCH_STATE_X) { if (mTouchView != null) { mTouchView.onSwipe(ev); } getSelector().setState(new int[] { 0 }); ev.setAction(MotionEvent.ACTION_CANCEL); super.onTouchEvent(ev); return true; } else if (mTouchState == TOUCH_STATE_NONE) { if (Math.abs(dy) > MAX_Y) { mTouchState = TOUCH_STATE_Y; } else if (dx > MAX_X) { mTouchState = TOUCH_STATE_X; // if (mOnSwipeListener != null) { // mOnSwipeListener.onSwipeStart(mTouchPosition); // } } } break; case MotionEvent.ACTION_UP: if (mTouchState == TOUCH_STATE_X) { if (mTouchView != null) { mTouchView.onSwipe(ev); if (!mTouchView.isOpen()) { mTouchPosition = -1; mTouchView = null; } } // if (mOnSwipeListener != null) { // mOnSwipeListener.onSwipeEnd(mTouchPosition); // } ev.setAction(MotionEvent.ACTION_CANCEL); super.onTouchEvent(ev); return true; } break; } return super.onTouchEvent(ev); } }