@Override protected void onFinishInflate() { super.onFinishInflate(); if (getChildCount() != 2) { throw new IllegalArgumentException("SlideLayout only need contains two child (content and slide)."); } mContentView = getChildAt(0); mSlideView = getChildAt(1); }
private void smoothScrollTo(int destX, int destY) { int scrollX = getScrollX(); int deltaX = destX - scrollX; int scrollY = getScrollY(); int deltaY = destY - scrollY; mScroller.startScroll(scrollX, scrollY, deltaX, deltaY, (int) (Math.abs(Math.sqrt(deltaX*deltaX + deltaY*deltaY)) * 3)); postInvalidate(); }
@Override public void onClick(View v) { if (itemView.getSlideState() == MiniSlideRightLayout.STATE_OPEN) { itemView.smoothCloseSlide(); } else { Toast.makeText(parent.getContext().getApplicationContext(), mListData.get(position), Toast.LENGTH_SHORT).show(); } } });
public int getSlideState() { int retValue = STATE_CLOSE; if (mIsScrolling) { retValue = STATE_SLIDING; } else { int scrollOffset = (mSlideDirection == SLIDE_LEFT || mSlideDirection == SLIDE_RIGHT) ? getScrollX() : getScrollY(); retValue = (scrollOffset == 0) ? STATE_CLOSE : STATE_OPEN; } return retValue; }
int eventX = (int) event.getX(); int eventY = (int) event.getY(); int scrollX = getScrollX(); int scrollY = getScrollY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: getParent().requestDisallowInterceptTouchEvent(true); mIsScrolling = true; int newScrollX = 0; scrollTo(newScrollX, newScrollY); break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: mIsScrolling = false; getParent().requestDisallowInterceptTouchEvent(false); int finalScrollX = 0; int finalScrollY = 0; switch (mSlideDirection) { case SLIDE_RIGHT: if (scrollX > getSlideCriticalValue()) { finalScrollX = mSlideView.getMeasuredWidth(); if (scrollX < -getSlideCriticalValue()) { finalScrollX = -mSlideView.getMeasuredWidth(); if (scrollY < -getSlideCriticalValue()) { finalScrollY = -mSlideView.getMeasuredHeight();
@Override protected void onLayout(boolean changed, int l, int t, int r, int b) { mContentView.layout(0, 0, getMeasuredWidth(), getMeasuredHeight()); switch (mSlideDirection) { case SLIDE_LEFT: mSlideView.layout(-mSlideView.getMeasuredWidth(), 0, 0, getMeasuredHeight()); break; case SLIDE_RIGHT: mSlideView.layout(getMeasuredWidth(), 0, mSlideView.getMeasuredWidth() + getMeasuredWidth(), getMeasuredHeight()); break; case SLIDE_TOP: mSlideView.layout(0, -mSlideView.getMeasuredHeight(), getMeasuredWidth(), 0); break; case SLIDE_BOTTOM: mSlideView.layout(0, getMeasuredHeight(), getMeasuredWidth(), mSlideView.getMeasuredHeight() + getMeasuredHeight()); break; } }
@Override public View getView(final int position, View convertView, final ViewGroup parent) { //TODO final SlideLayout itemView = (SlideLayout) LayoutInflater.from(parent.getContext()).inflate(R.layout.item_demo_list, parent, false); TextView content = (TextView) itemView.findViewById(R.id.content_id); TextView slide = (TextView) itemView.findViewById(R.id.slide_id); content.setText(mListData.get(position)); content.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (itemView.getSlideState() == MiniSlideRightLayout.STATE_OPEN) { itemView.smoothCloseSlide(); } else { Toast.makeText(parent.getContext().getApplicationContext(), mListData.get(position), Toast.LENGTH_SHORT).show(); } } }); slide.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(parent.getContext().getApplicationContext(), "Slide Clicked!!!!", Toast.LENGTH_SHORT).show(); } }); return itemView; } }