[Android] solves the problem of attaching events to the & quot; Control & quot; in the gallery, and also triggers events when the finger is lifted after sliding (events should not be triggered when sliding ,!, Android gallery

Source: Internet
Author: User

[Android] attaches events to the "controls" in gallery. After sliding, an event is triggered when the finger is lifted (which should not be triggered when sliding ,!, Android gallery

Ideas:

1. The method for attaching events (for example, onClickListener) to controls in gallery is similar to listview. You can directly mount the controls in baseAdapter. getView (the specific method is Baidu ).

2. It seems that there is no problem, but the event will still be triggered when the finger is lifted after sliding (the finger is mounted to the Event Control). This is not true.

In solution, if it is sliding (x has an offset), the event is intercepted in gallery. onInterceptTouchEvent, and the Child control naturally cannot accept the event.

Note: 1> you cannot simply determine whether x has an offset to intercept. Some devices may be deprecated even if they are lifted up in the same place. At this time, the onClick trigger is also blocked. Therefore, to be compatible with most machines, it is necessary to intercept them when horizontal movement exceeds a certain threshold. Therefore, the gallery does not change within the threshold.

3. The problem arises again. When sliding slowly for the first item, there will be a significant mutation (x-coordinate mutation) in the subitem ). The basic reason is that gallery starts to move normally after it seems to be sliding beyond the threshold. In fact, when the hand starts to move (when the gallery is not moved yet), gallery also made some initialization, there is an error when gallery actually moves.

During the solution, this initialization is continuously performed when sliding fingers within the threshold value, so that there is no error in initialization when gallery actually starts motion.


Public class MyGallery extends Gallery {float mLastMotionX = 0; public MyGallery (Context context) {super (context); // TODO Auto-generated constructor stub} public MyGallery (Context context, attributeSet attrs, int defStyle) {super (context, attrs, defStyle); // TODO Auto-generated constructor stub} public MyGallery (Context context, AttributeSet attrs) {super (context, context, attrs); // TODO Auto-generated constructor stub} @ Overridepublic boolean onInterceptTouchEvent (MotionEvent ev) {final int action = ev. getAction (); final float x = ev. getX (); switch (action) {case MotionEvent. ACTION_MOVE: final int xDiff = (int) Math. abs (x-mLastMotionX); if (xDiff> 50) {return true;} break; case MotionEvent. ACTION_DOWN: mLastMotionX = x; // The onTouchEvent (ev); break;} return false when the "2nd items" slide is prevented ;}}



Help: How can I prevent the gallery control from sliding around hands in Android development?

This. addContentView (view, params)
Hope to adopt it.


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.