Ad Auto-carousel (perfect solution for artificial sliding after automatic rotation in sequence)

Source: Internet
Author: User

In the mobile phone client, the cycle of advertising is very common, in the online have seen some demo, a lot of writing is not too complete, the case of human non-intervention is indeed in accordance with the sequence of the rotation, but after the man to slide, the next automatic rotation is not to press the CurrentPosition play after the slide, Instead, it is played by jumping the position of the position before sliding manually. Spare time, the problem of a simple solution, for the latter to provide some ideas.

=======================================================================================================

All rights reserved, if need to reprint please indicate source: http://blog.csdn.net/you4580

=======================================================================================================

public class Mainactivity extends Activity implements onpagechangelistener{

Private Viewpager Mviewpager;
Private Foucsadapter Mfoucsadapter;
Private arraylist<view> mfocusviewlist;
private int mcurrentitem;
Private Boolean Isrun;
Private Boolean nowaction;
Carousel Timer
Private Scheduledexecutorservice Mscheduledexecutorservice;
Private int[] Image=new int[]{r.drawable.ic_launcher,r.drawable.ic_launcher,r.drawable.ic_launcher,r.drawable.ic_ Launcher,r.drawable.ic_launcher};
Private Handler Handler = new Handler () {
public void Handlemessage (Android.os.Message msg) {
Mviewpager.setcurrentitem (Mcurrentitem);
};
};



@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);


Mviewpager= (Viewpager) Findviewbyid (R.id.electricity_viewpager);
Mviewpager.setonpagechangelistener (this);


Mfoucsadapter = new Foucsadapter (Getapplicationcontext ());
Mviewpager.setadapter (Mfoucsadapter);


Mfocusviewlist = new arraylist<view> ();
for (int i = 0; i < 5; i++) {
View view = This.getlayoutinflater (). Inflate (R.layout.electricity_focus_item,null);
TextView Focusindex = (TextView) View.findviewbyid (R.id.electricity_focusindex);
ImageView focusimage = (ImageView) View.findviewbyid (r.id.electricity_focusimage);
Focusindex.settext (i+1+ "/" +5);
Focusimage.setbackgroundresource (Image[i]);
Mfocusviewlist.add (view);
}
Mfoucsadapter.setlist (mfocusviewlist);


Handler.postdelayed (New Runnable () {
@Override
public void Run () {
Mscrollview.scrollto (0,0);
if (!isrun) {
if (mscheduledexecutorservice! = null) {
Mscheduledexecutorservice.scheduleatfixedrate (New Scrolltask (), 0, 3, timeunit.seconds);
}
}
}
},500);
}


@Override
protected void OnStart () {
Super.onstart ();
Mscheduledexecutorservice = Executors.newsinglethreadscheduledexecutor ();
}


@Override
protected void OnStop () {
Super.onstop ();
if (mscheduledexecutorservice!=null) {
Mscheduledexecutorservice.shutdown ();
}
}


Private class Scrolltask implements Runnable {

public void Run () {
Synchronized (Mviewpager) {
if (!nowaction) {
Mcurrentitem = mcurrentitem+1;
Handler.obtainmessage (). Sendtotarget ();
}
}
}
}


@Override
public void onpagescrolled (int position, float positionoffset,
int positionoffsetpixels) {

}


@Override
public void onpageselected (int position) {
Mcurrentitem = position;
}


@Override
public void onpagescrollstatechanged (int.) {
if (state = = 0) {
Nowaction = false;
}
if (state = = 1) {
Nowaction = true;
}
}
}

===========================================================================================

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
tools:context= "Com.example.adscirledemo.MainActivity" >


<android.support.v4.view.viewpager
Android:id= "@+id/electricity_viewpager"
Android:layout_width= "Match_parent"
android:layout_height= "150DP"
android:background= "#ffffff"/>


</RelativeLayout>

============================================================================================


/**
*
* @DESCRIPTION:
* @AUTHOR: you4580
* @VERSION: v1.0
* @DATE: 2015-4-20
*/
public class Foucsadapter extends pageradapter{
Private Context Mcontext;
Private list<view> mlist;
Private Myitemclicklitener Myitemclicklitener;


Public Foucsadapter (Context context) {
This.mcontext = context;


}


public void setlist (list<view> List) {
This.mlist = list;
This.notifydatasetchanged ();
}
@Override
public int GetCount () {
if (mlist = = null) return 0;
return integer.max_value;
}


@Override
Public Object Instantiateitem (ViewGroup container, final int position) {
View view = null;
if (position% mlist.size () < 0) {
View = Mlist.get (mlist.size () + position);
} else {
view = mlist.get (position% mlist.size ());
}
Viewparent VP = view.getparent ();
if (VP! = null) {
ViewGroup parent = (viewgroup) VP;
Parent.removeview (view);
}
((Viewpager) container). AddView (view);
View.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
Myitemclicklitener.onitemclick (position);
}
});
return view;
}


@Override
public void Destroyitem (ViewGroup container, int position, object object) {


}


@Override
public Boolean isviewfromobject (view view, Object object) {
return view = = object;
}


public void Setmyitemclicklitener (Myitemclicklitener myitemclicklitener) {
This.myitemclicklitener = Myitemclicklitener;
}


public interface myitemclicklitener{
public void Onitemclick (int position);
}
}


===========================================================================================


<?xml version= "1.0" encoding= "Utf-8"?>
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent" >


<relativelayout
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent" >


<imageview
Android:id= "@+id/electricity_focusimage"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:background= "@drawable/bplaceholder_head_news"
Android:flipinterval= "30"
Android:persistentdrawingcache= "Animation"
Android:scaletype= "Centercrop"/>
</RelativeLayout>


<textview
Android:layout_width= "Fill_parent"
android:layout_height= "20dip"
Android:layout_alignparentbottom= "true"
Android:alpha= "0.5"
android:background= "@drawable/ele_focus_bottom"/>


<textview
Android:id= "@+id/electricity_focusindex"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_alignparentbottom= "true"
Android:layout_alignparentright= "true"
android:layout_marginright= "3DP"
Android:gravity= "Center_vertical|right"
android:paddingright= "5DP"
Android:textcolor= "#ffffff"
Android:textsize= "16sp"/>


</RelativeLayout>



===========================================================================================

Ad Auto-carousel (perfect solution for artificial sliding after automatic rotation in sequence)

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.