Android viewpager nested Viewpager Sliding Click event Conflict Solution

Source: Internet
Author: User
Tags gety

In order to solve this problem. You can customize the Viewpager, then monitor the jewelry inside, custom click events

Package Com.hpuvoice.view;import Android.content.context;import Android.graphics.pointf;import Android.support.v4.view.viewpager;import Android.util.attributeset;import Android.view.gesturedetector;import Android.view.gesturedetector.ongesturelistener;import Android.view.motionevent;public class MyViewPager extends  Viewpager implements Ongesturelistener {/** gesture sliding processing class **/private Gesturedetector mdetector;/** touch pressed point **/pointf DownP = new PointF ();/** touch the current point **/pointf curp = new PointF (); Onsingletouchlistener onsingletouchlistener;public Myviewpager ( Context context, AttributeSet Attrs) {Super (context, attrs); Gesturedetector detector = new Gesturedetector (context, this); mdetector = detector;} Public Gesturedetector Getgesturedetector () {return mdetector;} @Overridepublic boolean ontouchevent (Motionevent arg0) {curp.x = Arg0.getx (); curp.y = Arg0.gety (); GetParent (). Requestdisallowintercepttouchevent (True); if (arg0.getaction () = = Motionevent.action_down) {downp.x = Arg0.getX (); DOWNP.Y = ARG0.GEty ();} if (arg0.getaction () = = Motionevent.action_move) {//This code is to inform his parent that Viewpager is now doing the operation of this control, do not interfere with my operation GetParent (). Requestdisallowintercepttouchevent (TRUE);} if (arg0.getaction () = = motionevent.action_up) {//At up time to determine whether to press and let go of the coordinates for a point//if it is a point, will perform a click event, this is my own write click event, not Onclickif (( downp.x = = curp.x) && (downp.y = = curp.y)) {Onsingletouch (); return true;}} Return super.ontouchevent (arg0);} @Overridepublic void Setonpagechangelistener (Onpagechangelistener listener) {Super.setonpagechangelistener ( listener);} /** * Click */public void Onsingletouch () {if (Onsingletouchlistener! = null) {Onsingletouchlistener.onsingletouch ();}} /** * * Create click event interface * */public interface Onsingletouchlistener {public void Onsingletouch ();} public void Setonsingletouchlistener (Onsingletouchlistener onsingletouchlistener) {This.onsingletouchlistener = Onsingletouchlistener;} @Overridepublic Boolean Ondown (Motionevent e) {return false;} @Overridepublic void Onshowpress (Motionevent e) {} @Overridepublic Boolean OnsiNgletapup (Motionevent e) {return false;} @Overridepublic boolean onscroll (motionevent E1, motionevent E2, float distancex,float Distancey) {return false;} @Overridepublic void Onlongpress (Motionevent e) {} @Overridepublic boolean onfling (motionevent E1, motionevent E2, float v Elocityx,float Velocityy) {return false;}}

Then in the use of the Viewpager class, a series of monitoring, use, colleagues of this case is to show the first page of the effect of the carousel, one of the other point is the home page carousel dynamic add small dots. and realize the carousel effect.

Package Com.hpuvoice.fragment;import Java.util.arraylist;import Java.util.hashmap;import java.util.Timer;import Java.util.timertask;import Org.json.jsonarray;import Org.json.jsonexception;import Org.json.JSONObject;import Android.annotation.suppresslint;import Android.content.intent;import Android.os.bundle;import Android.os.Handler; Import Android.os.message;import Android.support.v4.view.pageradapter;import Android.support.v4.view.ViewPager; Import Android.support.v4.view.viewpager.onpagechangelistener;import Android.util.typedvalue;import Android.view.gravity;import Android.view.layoutinflater;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.viewgroup;import Android.view.animation.animation;import Android.view.animation.animation.animationlistener;import Android.view.animation.animationutils;import Android.widget.button;import Android.widget.gridview;import Android.widget.imageview;import Android.widget.imageview.scaletype;import Android.widget.LinEarlayout;import Android.widget.textview;import Com.edaixi.activity.citylistactivity;import Com.edaixi.activity.loginactivity;import Com.edaixi.activity.mainactivity;import Com.edaixi.activity.placeorderactivity;import Com.edaixi.activity.r;import com.edaixi.activity.WebActivity; Import Com.edaixi.appdata.kclassdata;import Com.edaixi.appdata.keepingdata;import Com.edaixi.bean.Bannerlistbean; Import Com.edaixi.bean.inappurlbean;import Com.edaixi.bean.postcommonbean;import Com.edaixi.listadpter.myfuncadpter;import Com.edaixi.utils.common;import Com.edaixi.utils.densityutil;import Com.edaixi.utils.netutil;import Com.edaixi.utils.saveutils;import Com.edaixi.view.myspringring;import Com.edaixi.view.myviewpager.onsingletouchlistener;import Com.google.gson.gson;import Com.lidroid.xutils.bitmaputils;public class Homefragment extends Basefragment implements Onclicklistener {private static final int getbannerfaild = 2;private static final int getbannersucced = 3;private static final int getfunctionbtnsucced = 4;private static final int getfunctionbtnfaild = 5;boolean Requstflag = false;private View view;private Butto n main_order_btn;private hashmap<string, string> parms;private saveutils saveutils;private String address = ""; Private GridView main_grid;private myfuncadpter adapter;private com.edaixi.view.MyViewPager myview_pager;private ImageView anim_trousers;private ImageView anim_womencloth;private ImageView anim_clothsmall;private ImageView Imageview_title;private arraylist<bannerlistbean> funditonlist;private jsonobject str;private TitleAdapter Title;private Animation tosmallanimation;private ImageView anim_sleeveshort;private arraylist<bannerlistbean> Banerlistbak;private linearlayout pager_bottom;private int animorder;private myspringring cricleView;private ImageView point;private static TextView loction_text;private int currentposition = 0;private Boolean is_loop_flag = true;; @SuppressLint ("Handlerleak") private Handler Handler = new Handler () {Private GSon Gson = new Gson (); @Overridepublic void Handlemessage (Message msg) {Myview_pager.setcurrentitem (currentposition); Currentposition++;if ((banerlistbak.size () > 0) && (currentposition > Banerlistbak.size ()-1)) { currentposition = (currentposition% banerlistbak.size ());} if (Is_loop_flag) {handler.sendemptymessagedelayed (0, 3000);} try {switch (msg.what) {case Getbannersucced:postcommonbean Jsoncommonbean = Gson.fromjson (String) Msg.obj, Postcommonbean.class); if (Jsoncommonbean.isret ()) {String data = Jsoncommonbean.getdata (); banerlistbak = null; Banerlistbak = new arraylist<bannerlistbean> ();p aserbaner (data, Banerlistbak); Myview_pager.setcurrentitem (0) ; currentposition = integer.max_value/2-(integer.max_value/2% banerlistbak.size ()); Addbannerpoint (); Titleadapter Titlebak = new Titleadapter (Banerlistbak); Myview_pager.setadapter (Titlebak); Titlebak.notifydatasetchanged ();} Break;case getbannerfaild:break;case Getfunctionbtnsucced:requstflag = true; Postcommonbean ComMonbean = Gson.fromjson ((String) msg.obj,postcommonbean.class), if (Commonbean.isret ()) {String data = Commonbean.getdata ();p aserbaner (data, funditonlist); adapter.notifydatasetchanged ();} Break;case Getfunctionbtnfaild:break;} Super.handlemessage (msg);} catch (Exception e) {e.printstacktrace ();}}}; @SuppressLint ("Handlerleak") @Overridepublic View Oncreateview (layoutinflater inflater, ViewGroup container,bundle Savedinstancestate) {view = (LinearLayout) inflater.inflate (R.layout.fragment_home,container, false); main_order_btn = (Button) View.findviewbyid (r.id.main_order_btn); Main_grid = (GridView) View.findviewbyid (R.id.main_grid); myview_ Pager = (Com.edaixi.view.MyViewPager) View.findviewbyid (r.id.myview_pager); Cricleview = (myspringring) View.findviewbyid (r.id.myring_circle), Cricleview.startanim ();p Ager_bottom = (linearlayout) view.findViewById ( R.id.pager_bottom); anim_trousers = (ImageView) View.findviewbyid (r.id.anim_trousers); Anim_womencloth = (ImageView) View.findviewbyid (r.id.anim_womencloth); Anim_clothsmall = (ImageView) View.findviewbyid (r.id.anim_clothsmall); anim_sleeveshort = (ImageView) View.findviewbyid (r.id.anim_sleeveshort); loction_text = (TextView) View.findviewbyid (r.id.loction_text); loction_ Text.setonclicklistener (this); Myview_pager.setcurrentitem (currentposition); myview_ Pager.setonpagechangelistener (New Onpagechangelistener () {@Overridepublic void onpageselected (int position) { CurrentPosition = Position;if (Pager_bottom.getchildcount () > 0) {for (int i = 0; i < banerlistbak.size (); i++) {Pag Er_bottom.getchildat (i). Setbackgroundresource (Getimageresids () [0]);} Pager_bottom.getchildat (position). Setbackgroundresource (Getimageresids () [1];}} @Overridepublic void onpagescrollstatechanged (int arg0) {} @Overridepublic void onpagescrolled (int arg0, float arg1, int a RG2) {}}); Myview_pager.setonsingletouchlistener (new Onsingletouchlistener () {@Overridepublic void Onsingletouch () { Intent Intent = new Intent (), if (Banerlistbak.get (Myview_pager.getcurrentitem ()). GetUrl_type (). Equals ("Web")) {Intent.putextra ("Bannerlistbean", Banerlistbak.get (Myview_pager.getcurrentitem ())); Intent.setclass (Getactivity (), webactivity.class); startactivity (intent);} else if (Banerlistbak.get (Myview_pager.getcurrentitem ()). Geturl_type (). Equals ("In_app")) {if (!SAVEUTILS.GETBOOLSP (keepingdata.logined)) {Intent.setclass (Getactivity (), loginactivity.class);} else {Gson Gson = new Gson (); I Nappurlbean Inappurlbean = Gson.fromjson (Banerlistbak.get (Myview_pager.getcurrentitem ()). GETURL (), Inappurlbean.class); Intent.setclass (Getactivity (), Kclassdata.getrechageclsss (Inappurlbean.getklass ()));} StartActivity (intent);}}); Initview (); handler.sendemptymessagedelayed (0, +); return view;} private void Startanim () {tosmallanimation = (Animation) animationutils.loadanimation (Getactivity (), R.anim.main_btn_ SCAL_BIG); main_order_btn.startanimation (tosmallanimation);} public static TextView Getloctionview () {return loction_text;} private void Get_func_button_list () {parms.clear ();p arms.put ("Width ", string.valueof (DENSITYUTIL.GETWIDTHINPX (Getactivity ())));p arms.put (" Height ", string.valueof ( DENSITYUTIL.GETHEIGHTINPX (Getactivity ())); if (SAVEUTILS.GETSTRSP (keepingdata.user_city_id)! = null) {Parms.put (" City_id ", SAVEUTILS.GETSTRSP (keepingdata.user_city_id));} else {parms.put ("city_id", "1");} ((mainactivity) getactivity ()). GETDATE (Parms,common.get_func_button_list, Handler, getfunctionbtnsucced, Getfunctionbtnfaild, False, True, false);} private void Get_banner_list () {parms.clear ();p arms.put ("width", string.valueof (DENSITYUTIL.GETWIDTHINPX ( Getactivity ()));p arms.put ("Height", string.valueof (DENSITYUTIL.GETHEIGHTINPX (Getactivity ()))); SAVEUTILS.GETSTRSP (keepingdata.user_city_id)! = null) {parms.put ("city_id", SAVEUTILS.GETSTRSP (KeepingData.User_ city_id));} else {parms.put ("city_id", "1");} ((mainactivity) getactivity ()). GETDATE (Parms, Common.get_banner_list,handler, getbannersucced, Getbannerfaild, True , true, false);} private void Initview () {Banerlistbak = new Arraylist<banneRlistbean> (); funditonlist = new arraylist<bannerlistbean> (); saveutils = new Saveutils (this.getactivity ()); parms = new hashmap<string, string> (); get_banner_list (); title = new Titleadapter (banerlistbak); myview_ Pager.setadapter (title); adapter = new Myfuncadpter (getactivity (), funditonlist); Main_grid.setadapter (adapter); get_ Func_button_list (); Animation Toleftanim = (Animation) animationutils.loadanimation (Getactivity (), R.anim.toleft_translaneannim); final Animation Torightanim = (Animation) animationutils.loadanimation (Getactivity (), R.anim.toright_translateanim); final Animation torightanim1 = (Animation) animationutils.loadanimation (Getactivity (), r.anim.toleft_translation); final Animation toleftanim1 = (Animation) animationutils.loadanimation (Getactivity (), R.anim.long_short_anim); Animationlistener Animationlistener = new Animationlistener () {@Overridepublic void Onanimationstart (Animation Animation) {} @Overridepublic void Onanimationrepeat (animation animation) {} @Overridepublic void Onanimationend (Animation Animation) {Animorder++;switch (animorder) {case 1:anim_ Womencloth.startanimation (Torightanim); Break;case 2:anim_clothsmall.startanimation (TORIGHTANIM1); Break;case 3: Anim_sleeveshort.startanimation (TOLEFTANIM1); break;default:break;}}; Anim_trousers.setvisibility (view.visible); anim_womencloth.setvisibility (view.visible); anim_ Clothsmall.setvisibility (view.visible); anim_sleeveshort.setvisibility (view.visible); anim_ Trousers.startanimation (Toleftanim); Toleftanim.setanimationlistener (Animationlistener); Torightanim.setanimationlistener (Animationlistener); Torightanim1.setanimationlistener (AnimationListener); Toleftanim1.setanimationlistener (Animationlistener); Main_order_btn.setonclicklistener (new OnClickListener () {@ overridepublic void OnClick (View v) {Startanim (); Tosmallanimation.setanimationlistener (new Animationlistener () {@ overridepublic void Onanimationstart (Animation Animation) {} @Overridepublic void Onanimationrepeat (Animation Animation) {} @OverridepuBlic void Onanimationend (Animation Animation) {if (!SAVEUTILS.GETBOOLSP (keepingdata.logined)) {startactivity (new Intent (Getactivity (), Loginactivity.class)); return;};i Ntent Intent = new Intent (getactivity (), Placeorderactivity.class), Intent.putextra ("from", "Frommain"); Intent.putextra (Keepingdata.address_data, ADDRESS); startactivity (intent);});}); Loction_text.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {Intent Intent = new Intent ( Getactivity (), citylistactivity.class); Startactivityforresult (Intent, getactivity (). Result_first_user);}});} @Overridepublic void Setuservisiblehint (Boolean isvisibletouser) {super.setuservisiblehint (isvisibletouser); Isvisibletouser) {Is_loop_flag = true;handler.sendemptymessagedelayed (0, +); if (Netutil.getnetworkstate ( Getactivity ()) {if (funditonlist! = null) &&! Requstflag) {Timer timer = new timer (); Timer.schedule (new TimerTask () {@Overridepublic void run () {getactivity (). Runonuithread (New Runnable () {@Overridepublic void Run () {get_banner_list (); Get_func_button_list ();}});}}, 500);}}} else {Is_loop_flag = false;}} Private int[] Getimageresids () {return new int[] {r.drawable.splash_dot_default,r.drawable.splash_dot_current};} Private class Titleadapter extends Pageradapter {private arraylist<bannerlistbean> Mbannerlist;public Titleadapter (arraylist<bannerlistbean> bannerlist) {super (); mbannerlist = Bannerlist;}  @Overridepublic int GetCount () {int size = mbannerlist.size () = = 0? 2:mbannerlist.size ();//if (Mbannerlist.size () > 0) {//return size;//} else {//return 2;//}return 3;} @Overridepublic boolean isviewfromobject (View arg0, Object arg1) {return arg0 = = arg1;} @Overridepublic void Destroyitem (ViewGroup container, int position, object object) {(Viewpager) container). Removeview ( (View) object);} @Overridepublic Object Instantiateitem (ViewGroup container, final int position) {View view = View.inflate (Getactivity (), R.layout.bander_title, null); Imageview_title = (ImageviEW) View.findviewbyid (r.id.iv_bander_title); Imageview_title.setscaletype (Scaletype.center_crop); imageView_ Title.setimageresource (R.drawable.banner_default); if (mbannerlist.size () = = 0) {container.addview (view, 0); return View;} else {bitmaputils utils = new Bitmaputils (getactivity ()); Utils.display (Imageview_title, mbannerlist.get (position). Getimage_url ()); Container.addview (view, position); return view;}}} /** Dynamic Add carousel plot small dot **/protected void Addbannerpoint () {for (int i = 0; i < banerlistbak.size (); i++) {point = new ImageView (Getactivity ());p Oint.setbackgroundresource (r.drawable.splash_dot_current); Linearlayout.layoutparams params = new Linearlayout.layoutparams (LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);p arams.gravity = Gravity.center;point.setlayoutparams (params); Point.setvisibility (view.visible); if (i = = 0) {point.setenabled (true);} else {point.setenabled (false);} Pager_bottom.addview (point);}} /** Parse Banner result method **/protected void Paserbaner (String json, Arraylist<bannerlistbean> bannerlist) {Jsonarray jsonarray;bannerlist.clear (); try {JSONArray = new JSONArray ( JSON); for (int i = 0; i < jsonarray.length (); i++) {str = (jsonobject) jsonarray.opt (i); Bannerlistbean Bannerlistbean = new Bannerlistbean () Bannerlistbean.setimage_url (str.getstring ("Image_url")); Bannerlistbean.seturl_type (str.getstring ("Url_type")); Bannerlistbean.seturl (str.getstring ("url")); Bannerlistbean.settitle (str.getstring ("title")), if (str.getstring ("title"). Equals ("Billed by Bag")) {SAVEUTILS.SAVESTRSP ( Keepingdata.an_dai_ji_fei_url,str.getstring ("Url"));} else if (str.getstring ("title"). Equals ("Per-piece billing")) {SAVEUTILS.SAVESTRSP (keepingdata.an_jian_ji_fei_url,str.getstring ("url")); Bannerlist.add (Bannerlistbean);}} catch (Jsonexception e) {e.printstacktrace ();}} /** Callback user selected city result method **/@Overridepublic void Onactivityresult (int requestcode, int resultcode, Intent data) { Super.onactivityresult (Requestcode, ResultCode, data); if (SAVEUTILS.GETSTRSP (keepingdata.user_city)! = "") {LocTion_text.settext (SAVEUTILS.GETSTRSP (keepingdata.user_city)); Get_banner_list (); Get_func_button_list ();}} /** DP px Conversion tool method **/private int dp2px (int dp) {return (int) typedvalue.applydimension (Typedvalue.complex_unit_dip, Dp,get Resources (). Getdisplaymetrics ());}}

The layout is omitted.

Android viewpager nested Viewpager Sliding Click event Conflict Solution

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.