Carousel of the banner strips

Source: Internet
Author: User
Tags object object visibility

Customizing a Bannerview

Import java.util.ArrayList;
Import java.util.List;
Import Com.nostra13.universalimageloader.core.ImageLoader;


Import Android.content.Context;
Import android.content.Intent;
Import Android.os.Handler;
Import Android.support.v4.view.PagerAdapter;
Import Android.support.v4.view.ViewPager;
Import Android.util.AttributeSet;
Import Android.util.Log;
Import android.view.MotionEvent;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.view.View.OnClickListener;
Import Android.widget.ImageView;
Import Android.widget.LinearLayout;
Import Android.widget.RelativeLayout;

public class Bannerview extends Relativelayout {
Private Viewpager Mviewpager;
Private LinearLayout mlinearlayout;
Private Context Mcontext;
Private imageview[] Mindicator;
Private Handler Mhandler = new Handler ();
list<string> mlist = new arraylist<> ();
List<bannermodel> bannermodell=new arraylist<> ();
Private Runnable mrunnable = new Runnable () {
@Override
public void Run () {
Mviewpager.setcurrentitem (Mviewpager.getcurrentitem () + 1);
Mhandler.postdelayed (mrunnable, 3000);
}
};
private int mitemcount;

Public interface Onbanneritemclicklistener {
void OnClick (int position);
}

Public Bannerview (Context context) {
This (context, NULL);
}

Public Bannerview (context context, AttributeSet Attrs) {
Super (context, attrs);
This.mcontext = context;
Init ();
}

private void init () {
View.inflate (Mcontext, R.layout.view_bannerview, this);
Controls that are taken to the layout
Mviewpager = (Viewpager) Findviewbyid (R.id.viewpager);
Mlinearlayout = (linearlayout) Findviewbyid (r.id.ll_points);
}

/**
* Set data for Viewpager in banner
*
* @param list
*/
public void setlist (list<string> list,list<bannermodel> Bannerl) {//List is banner URL Bannerl is banner for the entire object with In the back of the banner click Implementation
if (mlist.size () = = 0) {
if (List!=null&&!list.isempty ()) {
Mlist.addall (list);
if (bannerl!=null) {
Bannermodell=bannerl;
}
Mitemcount = Mlist.size ();
}
Initview ();
}

}
private void Initview () {
Set adapter for Viewpager
Bannerpageradapter bannerpageradapter = new Bannerpageradapter (mlist, Mcontext,bannermodell);
Mviewpager.setadapter (Bannerpageradapter);
Initialize Bottom Point indicator
Initindicator (Mlist, Mcontext);
Mviewpager.setcurrentitem (* mitemcount);

Set up slide monitoring for Viewpager
Mviewpager.setonpagechangelistener (New Viewpager.onpagechangelistener () {
@Override
public void onpagescrolled (int position, float positionoffset, int positionoffsetpixels) {
}

@Override
public void onpageselected (int position) {
Switchindicator (position% mitemcount);
}

@Override
public void onpagescrollstatechanged (int.) {
}
});
}

@Override
public boolean dispatchtouchevent (Motionevent ev) {
Switch (ev.getaction ()) {
Case Motionevent.action_down:
Cancelrecycle ();
Break
Case Motionevent.action_cancel:
Case MOTIONEVENT.ACTION_UP:
Startrecycle ();
Break
}
return super.dispatchtouchevent (EV);
}

private void Initindicator (list<string> List, context context) {
Mindicator = new Imageview[mitemcount];
for (int i = 0; i < mindicator.length; i++) {
Linearlayout.layoutparams params = new Linearlayout.layoutparams (20, 20);
Params.setmargins (6, 0, 6, 0);
ImageView ImageView = new ImageView (context);
Mindicator[i] = ImageView;
if (i = = 0) {
Mindicator[i].setbackgroundresource (R.drawable.dot_normal);
} else {
Mindicator[i].setbackgroundresource (r.drawable.dot_focused);
}
Mlinearlayout.addview (ImageView, params);
}
if (Mitemcount = = 1) {
Mlinearlayout.setvisibility (View.gone);
} else {
Mlinearlayout.setvisibility (view.visible);
}
}

private void Switchindicator (int selectitems) {
for (int i = 0; i < mindicator.length; i++) {
if (i = = Selectitems) {
Mindicator[i].setbackgroundresource (R.drawable.dot_normal);
} else {
Mindicator[i].setbackgroundresource (r.drawable.dot_focused);
}
}
}

private void Startrecycle () {
Mhandler.postdelayed (mrunnable, 3000);
}

private void Cancelrecycle () {
Mhandler.removecallbacks (mrunnable);
}

@Override
protected void onwindowvisibilitychanged (int visibility) {
Super.onwindowvisibilitychanged (visibility);
if (visibility = = VISIBLE) {
Startrecycle ();
} else {
Cancelrecycle ();
}
}


Private class Bannerpageradapter extends Pageradapter {
Private list<string> Imagesurl;
Private context context;
List<bannermodel> bannermodellist;
Public Bannerpageradapter (list<string> imagesurl, Context context,list<bannermodel> bannerModelList) {
This.imagesurl = Imagesurl;
This.context = context;
This.bannermodellist=bannermodellist;
}

@Override
public int GetCount () {
return Mitemcount = = 1? 1:integer.max_value;
}

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

@Override
Public Object Instantiateitem (ViewGroup container, final int position) {
View ret = null;
ImageView ImageView = new ImageView (context);
Imageview.setscaletype (ImageView.ScaleType.FIT_XY);
ret = ImageView;
Container.addview (ret);
Imageloader.getinstance (). DisplayImage (Imagesurl.get (position% mitemcount), imageView);//Picture loading
Ret.setonclicklistener (New Onclicklistener () {}//banner Click event
return ret;
}

@Override
public void Destroyitem (ViewGroup container, int position, object object) {
Container.removeview ((View) object);
}
}

}

View_bannerview.xml file

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent" >

<android.support.v4.view.viewpager
Android:id= "@+id/viewpager"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent" >
</android.support.v4.view.ViewPager>

<linearlayout
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:layout_alignbottom= "@id/viewpager"
android:orientation= "Vertical"
android:padding= "5dip" >

<linearlayout
Android:id= "@+id/ll_points"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_margintop= "5dip"
Android:layout_gravity= "Center_horizontal"
android:orientation= "Horizontal" >
</LinearLayout>
</LinearLayout>

</RelativeLayout>


Direct use of layouts
Invocation part:

The network requests down the data into the Bannerview inside//list is the banner URL Bannerl is banner the entire object for the subsequent banner click Implementation

if (Bannerl! = Null&&!bannerl.isempty ()) {
if (Bannerl.size () >= 1) {//A sheet of time set does not scroll, no small dots
Banlist = new arraylist<> ();
for (int i = 0; i < bannerl.size (); i++) {
Banlist.add (Bannerl.get (i). GetPath ());
}
}
Holder.banner.setList (Banlist, Bannerl);//banner Layout Object
}


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.