Android achieves the non-scroll Effect of ListView. androidlistview

Source: Internet
Author: User
Tags gety

Android achieves the non-scroll Effect of ListView. androidlistview

The expected result is that the ListView cannot be rolled, but the biggest problem is that there must be a click event with the ListView Item. If you do not need to click the event, it is easy to set the ListView directly. setEnable (false );

If you still need to click events, the scrolling and clicking operations are all managed in the ListView Touch processing mechanism.

The processing logic of the ViewGroup is reused in the ListView click event. When a user clicks a view and the distance between the pressed and raised fingers is small, the click event will be triggered if the time limit of the clicked event is met.

The ListView rolling event is self-handled and has two judgment conditions. When the user triggers the move event and the sliding distance exceeds the touch slop distance or the sliding speed exceeds the threshold, the event is regarded as a rolling event.

1 import android. content. context; 2 import android. util. attributeSet; 3 import android. view. motionEvent; 4 import android. widget. listView; 5 6 public class ScrollDisabledListView extends ListView {7 8 private int mPosition; 9 10 public ScrollDisabledListView (Context context) {11 super (context ); 12} 13 14 public ScrollDisabledListView (Context context, AttributeSet attrs) {15 super (context, attrs); 16} 17 18 public ScrollDisabledListView (Context context, AttributeSet attrs, int defStyle) {19 super (context, attrs, defStyle); 20} 21 22 @ Override23 public boolean dispatchTouchEvent (MotionEvent ev) {24 final int actionMasked = ev. getActionMasked () & MotionEvent. ACTION_MASK; 25 26 if (actionMasked = MotionEvent. ACTION_DOWN) {27 // record position 28 mPosition = pointToPosition (int) ev. getX (), (int) ev. getY (); 29 return super. dispatchTouchEvent (ev); 30} 31 32 if (actionMasked = MotionEvent. ACTION_MOVE) {33 // the most critical part. Ignore the MOVE event 34 // The ListView onTouch will not be able to get the MOVE event, so the rolling process will not occur 35 return true; 36} 37 38 // 39 if (actionMasked = MotionEvent. ACTION_UP40 | actionMasked = MotionEvent. ACTION_CANCEL) {41 // press your finger and lift it in the same view and hand it over to the parent Control for processing. This is a click event 42 if (pointToPosition (int) ev. getX (), (int) ev. getY () = mPosition) {43 super. dispatchTouchEvent (ev); 44} else {45 // If the finger has been removed from the Item when it is pressed, it indicates a rolling action, clearing the Item pressed status 46 setPressed (false ); 47 invalidate (); 48 return true; 49} 50} 51 52 return super. dispatchTouchEvent (ev); 53} 54}

(

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.