today, here we solve the 2 problems of Viewpager
Issue1:viewpgaer problems with white screen when implementing the Carousel effect
Issue2:viewpager The problem of increasing the speed of the carousel with the number of operations when implementing the Carousel effect
Workaround: For the first problem, we locate the problem point is the problem of data, on the native component Viewpager, when the rotation of the view of less than 3, there will be such a situation, in order to this situation, many people also expressed very helpless, so, Our response is to add the data to double, which solves the problem of this kind of white screen, and the second problem, we are positioned in the execution of time-consuming operation (the default is to use handler here), but did not clear the sliding message before execution, resulting in animation confusion, and then the rotation of the phenomenon of rapid change. The code is as follows:
Private Homepageradapter adapter;
Private list<view> mlists = new arraylist<view> ();
Private list<string> imgurllist = new arraylist<> ();//carousel map Picture URL//...//Here we omit a lot, the following operation is called after the request interface succeeds Imgurllist.clear ();//Clear data if (null!= modelbean.getdata (). Getadvertbeans () && modelbean.getdata ().
Getadvertbeans (). Size () >0) {//rotation limit of number of carousel (loop Add to solve white screen problem) if (Modelbean.getdata (). Getadvertbeans (). Size () = = 1) { for (int i = 0; i < 4; i++) {Initset
Data (0,modelbean);
}}else if (Modelbean.getdata (). Getadvertbeans (). Size () = = 2) { for (int i = 0; i < 4; i++) {Initsetdata (i%2,
Modelbean); }}else if (Modelbean.getdata (). GetAdVertbeans (). Size () = = 3) {for (int i = 0; i < 6; i++) {
Initsetdata (I%3,modelbean); }}else {for (int i = 0; I < model Bean.getdata (). Getadvertbeans (). Size ();
i++) {initsetdata (I,modelbean); }}} Initflashview (Modelbean);//switching of the Carousel diagram//Several methods private void Initsetdata (i NT I, Homebean Modelbean) {for (i = 0; i < Modelbean.getdata (). Getadvertbeans (). Size (); i++) {if (null!= modelbean.getdata (). Getadvertbeans (). Get (i) && null!= modelbean.getdata (). Getadvertbeans (). get (i).
Getadpicurl ()) {Imgurllist.add (Modelbean.getdata (). Getadvertbeans (). get (i). Getadpicurl ());
}else { Imgurllist.add ("");
}}} private void Initflashview (Homebean modelbean) {mlists.clear (); if (Imgurllist.size () >0) {for (int i = 0; i < imgurllist.size (); i++) {ImageView IV = n
EW ImageView (Getactivity ()); Viewgroup.layoutparams LP = new Viewgroup.layoutparams (ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
IV.SETLAYOUTPARAMS (LP);
Get the actual picture Picasso.with (Getactivity ()) according to the subscript. Load (Imgurllist.get (i)). Error (R.mipmap.default_image). into (iv);
Iv.setscaletype (ImageView.ScaleType.FIT_XY);
Mlists.add (iv); }}//adapter if (NULL = = Adapter) {adapter = new Homepageradapter (mlists, Mitemclicklistener
);
Viewpagerhome.setadapter (adapter);
}else {adapter.notifydatasetchanged (); }}}//Carousel jump Homepageradapter.oniteMclicklistener Mitemclicklistener = new Homepageradapter.onitemclicklistener () {@Override public void OnI
Temclick (view view, int position) {Intent Intent = new Intent (getactivity (), adpichomejumpactivity.class); if (Imgjumpurllist.size () >0 && null!= imgjumpurllist.get (position)) {Intent.putextra (
"Jumpurl", imgjumpurllist.get (position));
} getactivity (). StartActivity (Intent);
}
};
Here we may find that carousel cannot jump, so the next thing we do is to execute the method Initview () after the interface call in the OnCreate () method, the code is as follows:
@ViewInject (r.id.viewpager_home) Viewpager viewpagerhome;
Private final int home_ad_result = 1;//Carousel @Override public void OnCreate (@Nullable Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Gethomedata ();//Interface Call method Initview ();
} private void Initview () {//Open thread Loop play Carousel try {Viewpagerhome.setcurrentitem (50000);
}catch (Exception e) {e.printstacktrace (); } mhandler.removemessages (Home_ad_result);//This method must be written, otherwise it will appear issue2 phenomenon, the more the operation, the faster the Carousel playback speed Mhandler.sendemptymessage
Delayed (Home_ad_result, 3000);
} private Handler Mhandler = new Handler () {@Override public void Handlemessage (Message msg) {
Super.handlemessage (msg); Switch (msg.what) {case Home_ad_result://Here is the next page to set the current page Viewpagerh
Ome.setcurrentitem (Viewpagerhome.getcurrentitem () + 1); MHandler.removemessages (Home_ad_result);
Mhandler.sendemptymessagedelayed (Home_ad_result, 3000);
Break
Default:break; }
}
};
here, our analysis is basically over, only records for reference.
If you have any questions, please leave a message.