Create a sliding control (revised version) and a listviewviewgroup Based on ListView sliding Deletion

Source: Internet
Author: User

Create a sliding control (revised version) and a listviewviewgroup Based on ListView sliding Deletion

This is a revised version. It corrected some judgment problems in ontouch, and adopted the Judgment Method in teacher Ren yugang's blog. It also caused me to publish a blog without fully understanding it!


HELLO everyone, I haven't written a blog for a long time .. Recently, I have been training cars at home, And it is cool and bitter.

I wish you a fortune in 2015!

For example, this is the sliding deletion Effect of ListView. Gitub actually has a very good open-source project, but it is the best to understand it, most of the current sliding deletion effects of listview are implemented by creating a sliding control first and then solving the sliding conflict of listview. Today we will first create a sliding control, first the previous


1. Custom ViewGroup

We can see that our sliding control is actually composed of two parts: the first part is the content, the second part is our deletion button, then we can create a ViewGroup for implementation.

Public class MySwipeItem extends ViewGroup {/*** delete button */private View mButtonView;/*** content */private View mContentView; /***** control scroll */private Scroller mScroller;/***** record coordinates */private int lastX = 0;/***** scroll maximum value */private int mMaxDistancex = 100; public MySwipeItem (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle); init (context ); // TODO Auto-generated constructor stub} public MySwipeItem (Context context, AttributeSet attrs) {super (context, attrs); init (context ); // TODO Auto-generated constructor stub} public MySwipeItem (Context context) {super (context); init (context);} private void init (Context context) {mScroller = new Scroller (context); mButtonView = LayoutInflater. from (context ). inflate (R. layout. button, null); mContentView = LayoutInflater. from (context ). inflate (R. layout. content, null); addView (mContentView); addView (mButtonView); mMaxDistancex = Math. round (TypedValue. applyDimension (TypedValue. COMPLEX_UNIT_DIP, mMaxDistancex, getResources (). getDisplayMetrics ();} @ Overrideprotected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {super. onMeasure (widthMeasureSpec, heightMeasureSpec); mButtonView. measure (widthMeasureSpec, heightMeasureSpec); mContentView. measure (widthMeasureSpec, heightMeasureSpec );}

We add the two content la S and button la s to measure their size. Because the content layout is fill and the button layout is fixed, we can use MeasureSpec directly, next we should consider sorting them. The button is invisible to the right of the content, that is, we need to manually arrange them in onlayout.

@Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b) {mContentView.layout(l, t, r, b);mButtonView.layout(r, t, r + mButtonView.getMeasuredWidth(), b);}

In this way, our buttons will always be on the right side of the content. Similarly, when we want two buttons, we should all know. After the layout is complete, we will start to let him
.

2. scroroller

We initially defined a scroroller and used it and scrollto to scroll down our controls.

@ Overridepublic boolean onTouchEvent (MotionEvent event) {int scrollX = getScrollX (); int x = (int) event. getX (); switch (event. getAction () {case MotionEvent. ACTION_DOWN: {if (! MScroller. isFinished () {mScroller. abortAnimation ();} break;} case MotionEvent. ACTION_MOVE: {int deltaX = x-lastX; Log. d ("TAG", "X:" + lastX); Log. d ("TAG", "deX:" + deltaX); Log. d ("TAG", "scrollerX:" + scrollX); // calculate whether the sliding endpoint is valid to prevent the sliding cross-border int newScrollX = scrollX-deltaX; if (deltaX! = 0) {if (newScrollX <0) {newScrollX = 0;} else if (newScrollX> mMaxDistancex) {newScrollX = mMaxDistancex;} this. scrollTo (newScrollX, 0);} break;} case MotionEvent. ACTION_UP: {int newScrollX = 0; // you have made a judgment here. When you release your hand, it will automatically slide to both sides, depending on the current location if (scrollX> mMaxDistancex/2) {newScrollX = mMaxDistancex;} // slide slowly toward the end this. smoothScrollTo (newScrollX, 0); break ;}} lastX = x; return true ;}

The judgment process is optimized here to make the code more concise. We only need to compare newScroll to determine whether the code is out of bounds.

Private void smoothScrollTo (int destX, int destY) {// scroll slowly to the specified position int scrollX = getScrollX (); int delta = destX-scrollX; mScroller. startScroll (scrollX, 0, delta, 0, Math. abs (delta) * 3); invalidate ();}


After startScroll is called, The postInvalidate method calls back the computeScroll method of the parent control.
@Overridepublic void computeScroll() {if (mScroller.computeScrollOffset()) {scrollTo(mScroller.getCurrX(), 0);postInvalidate();}}
So we get the desired rolling effect. This is the time to come. The next issue will solve the listview sliding conflict! Finally, I wish you a happy New Year!

Project source code


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.