Android uses the sliding control ListView to slide and delete androidlistview.

Source: Internet
Author: User

Android uses the sliding control ListView to slide and delete androidlistview.

In a twinkling of an eye, it will take 15 years. I hope you will enjoy a 15-year promotion and salary increase to reach the peak of your life.

This blog is based on the previous ListView sliding deletion Viewgroup to create a sliding control (Correction) blog. Let's start with the previous blog.


Actually, implementation is not complicated.

1. Solve sliding conflicts

Because our custom sliding control conflicts with the sliding event of the ListView itself, we can customize the ListView and override the onInterceptTouchEvent method.

Let's take a look at the distribution of android events. When users touch the screen, they will first call the dispatchTouchEvent method of ViewGroup.

In the dispathTouchEvent method, the onInterceptTouchEvent method is called. The main purpose of this method is to intercept user operations, for details about the specific distribution mechanism, refer to the complete analysis of the Android event distribution mechanism of Guo Shen's blog, which gives you a thorough understanding of the source code (below ).

Here we will first know that if this method returns false, it means that the touch event is completed by the sub-view to see the code.

/*** Determine whether to intercept the event */public boolean onInterceptTouchEvent (MotionEvent ev) {float lastX = ev. getX (); float lastY = ev. getY (); switch (ev. getAction () {case MotionEvent. ACTION_DOWN: mFirstX = lastX; mFirstY = lastY; int motionPosition = pointToPosition (int) mFirstX, (int) mFirstY); if (motionPosition> = 0) {currentItemView = getChildAt (motionPosition-getFirstVisiblePosition (); mySwipeItem = (MySwipeItem) currentItemView. findViewById (R. id. myitem);} break; case MotionEvent. ACTION_MOVE: float dx = lastX-mFirstX; float dy = lastY-mFirstY; Log. d ("TAG", "DX:" + dx); if (Math. abs (dx)> = 5 & Math. abs (dy)> = 5) {return false;} if (Math. abs (dx) <5 & Math. abs (dx)> 0) {mySwipeItem. toOff ();} break; default: break;} return super. onInterceptTouchEvent (ev );}

Here, I will make a judgment when moving. If the conditions are met, we will think that the user is performing a horizontal slide operation, and the sub-view of the listview will be obtained at the time of down, when the move condition is not met, the control is closed when vertical sliding is started.

2. Use the callback Interface

Our result is that if you slide the control and click the button next time to close the sliding control, we can use the interface callback to achieve this effect.

public interface OnScrollState {public void scrollView();public void scrollOn(View view);}public void setOnScrollState(OnScrollState onScrollState) {this.mOnScrollState = onScrollState;}
Customizes an interface and makes a judgment during touch.

@ 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 ();} if (mOnScrollState! = Null) {mOnScrollState. scrollView ();} break;} case MotionEvent. ACTION_MOVE: {int deltaX = x-lastX; // calculates whether the sliding end point is valid. 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); if (mOnScrollState! = Null & newScrollX = mMaxDistancex) {mOnScrollState. scrollOn (this) ;}break ;}lastx = x; return true ;}
Call the close method of the control in Main.

@Overridepublic void scrollView() {if (mLastItem != null) {mLastItem.toOff();}}@Overridepublic void scrollOn(View view) {mLastItem = (MySwipeItem) view;}

3. Disadvantages

It's easy to achieve the sliding effect, isn't it .... However, we will find that our own listview's onItemClick method does not work !!, Because we have rewritten the onInterceptTouchEvent method, but the textView and button in our sliding control can still use onclick, there is also a sliding deletion method of listview, which is highly similar to the sliding deletion effect of the dialog list.

4. Postscript

Don't ask me why I don't write a perfect one, so I won't say it's because I can't write it, and I'm lazy .... New users can learn from the distribution mechanism of android, but if you really want to use it in the project, I suggest you go to Github to get down a mature


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.