Android layout when using Viewpager, how to resolve and sub-page long press sliding conflict issues

Source: Internet
Author: User

When using Viewpager, how to resolve and sub-page long press the sliding conflict problem.

My problem prototype:

This problem, I believe meet people will be less, I was in a viewpager, one of the fragment realized the long press the function of sliding pictures, and found them both

Ontouchevent event conflict.

The solution that has been tried:

1-----

Encounter this problem, the first is Baidu, Baidu to the method has, custom Viewpager, in the inside rewrite ontouchevent and onintercepttouchevent, the specific code is below, this method, I try

found that only in the first load Viewpager page to achieve the blocking effect, I here in detail, in the Mainactivity page, we initialized the Viewpager, and then we will be on the second page of the

Fragment in the block, you might try to fragment a Mainactivity object in the new, and then somehow get the viewpager inside it, or introduce it with Mainactiv ity

Bind the XML file, and then Findviewbyid () in fragment to get the secondary viewpager. then use the function inside the custom Viewpager.

Unfortunately, the way I talked about it, I tried, all failed.

1  Packagecom. Lgh.weixin;2 3 /**4 * Created by Administrator on 2015/5/30.5  */6 ImportAndroid.content.Context;7 ImportAndroid.support.v4.view.ViewPager;8 ImportAndroid.util.AttributeSet;9 ImportAndroid.util.Log;Ten Importandroid.view.MotionEvent; One  A  -  Public classMyviewpagerextendsViewpager { -  the     Private BooleanEnabled =true;//the default is to slide. -  -  -      PublicMyviewpager (Context context, AttributeSet attrs) { +         Super(context, attrs); -     } +  A      Public voidSetstopviewpagerslip (Booleanenabled) { at          This. Enabled =enabled; -LOG.I ("--------------------", "////" + This. Enabled); -     } -     //Touch's not responding. - @Override -      Public Booleanontouchevent (Motionevent event) { in         if( This. Enabled) { -             return Super. Ontouchevent (event); to         } +         return false; -     } the  *  $ @OverridePanax Notoginseng      Public Booleanonintercepttouchevent (Motionevent event) { -         if( This. Enabled) { the             return Super. Onintercepttouchevent (event); +         } A         return false; the     } +}

2 -----

The second way is that I finally really solved it. In the beginning, the use of errors, resulting in failure.

Method: View.requestdisallowintercepttouchevent (True); The incoming parameter is true

The reason I failed to use it for the first time was:

In my fragment, because Viewpager it is a view, so when I use this method, it is directly

Viewpager. Requestdisallowintercepttouchevent (True);  The result is a failure. the correct way to use the child view is below ↓

Workaround:

View.requestdisallowintercepttouchevent (True), the view requirement used in is the view returned by the XML you are currently fragment introduced, and instead:

View. getParent (). Requestdisallowintercepttouchevent (TRUE);

At this point can really solve! Some of the necessary code is introduced below.

1 View main = inflater.inflate (R.layout.apart_center,container,false);
1 main.getparent (). Requestdisallowintercepttouchevent (true);

Android layout when using Viewpager, how to resolve and sub-page long press sliding conflict issues

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.