Android Custom Viewpager Indicator _android

Source: Internet
Author: User

This example for you to share the Android Viewpager indicator of the production method for your reference, the specific content as follows

1. Overview

Viewpageindicator This open source framework, everyone has been contacted, personal feeling is good is to use more trouble, need to configure the configuration there is not convenient customization. The first time I used the time has not been effective, and then found a long time to find that the activity of the theme is not configured well. Today we have to build a wheel to achieve one, which uses the adapter mode, if not clear this model, please see my Android source design mode analysis: http://www.jb51.net/article/96291.htm, the following look at the effect:

  

2. Realize

1). To achieve the basic effect of custom Horizontalscrollview extends Horizontalscrollview, set the Viewpager based on the number of continuous traversal add child view.

 public class Viewpagerindicator extends horizontalscrollview{the public viewpagerindicator (context context) {This (conte
 XT, NULL);
 Public Viewpagerindicator (context, AttributeSet attrs) {This (context, attrs, 0); Viewpagerindicator (context, AttributeSet attrs, int defstyleattr) {Super (context, Attrs, Defstyle
  ATTR);
 This.mcontext = context; @Override public void Setviewpager (Viewpager viewpager) {if (Viewpager = = null) {throw new Nullpointerexceptio
  N ("Viewpager is null ...");

  } This.mviewpager = Viewpager;
  int count = Mviewpager.getadapter (). GetCount ();

  Mcontaniergroup.removeallviews ();
   Loop add Tabview (TextView) for (int i = 0; i < count; i++) {final TextView ITEMTV = new TextView (mcontext);
   Itemtv.settext (This.mViewPager.getAdapter (). Getpagetitle (i));
   Setitemclickevent (ITEMTV, i);
   Mcontaniergroup.addtabview (ITEMTV);
   Setitemclickevent (ITEMTV, i);
  Mcontaniergroup.addtabview (ITEMTV); } SetItemParams ();

  Madapter.highlighttabview (Mcontaniergroup.gettabview (Mcureentitem));
 This.mViewPager.setOnPageChangeListener (this); private void Setitemparams () {if (mvisibletabcount!= 0) {final int childcount = Mviewpager.getadapter (). Getcou
   NT ();
     mrunnable = new Runnable () {@Override public void run () {int tabwidth = getwidth ()/mvisibletabcount; Loop traversal set tab width for (int index = 0; index < childcount; index++) {Linearlayout.layoutparams params =
      Arlayout.layoutparams) Mcontaniergroup. Gettabview (Index). Getlayoutparams ();
     Params.width = Tabwidth;
     //Set width, height, and angle indicator mcontaniergroup.settabwidth (tabwidth);
     Mcontaniergroup.getcontainer (). Getlayoutparams (). Height = getheight ();

     Mcontaniergroup.setindicatorview (Madapter.getindexview ());
    mrunnable = null;

   }
   };
  Post (mrunnable);

 }
 }
}

2). Set the Viewpager rolling Monitor to make Viewpager and tabview work together

 @Override public void onpagescrollstatechanged (int position) {if (position = 0) {//set to not click Misclick = False
  ;
 } if (Mpagechangelistener!= null) mpagechangelistener.onpagescrollstatechanged (position);
  @Override public void onpagescrolled (int position, float offset, int arg2) {Scroll (position, offset);
 if (Mpagechangelistener!= null) mpagechangelistener.onpagescrolled (position, offset, arg2);

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


 if (Mpagechangelistener!= null) mpagechangelistener.onpageselected (position); /** * indicator follows the finger for scrolling/public void scroll (int position, float offset) {int tabwidth = Mcontaniergroup.gettabvie

  W (0). GetWidth ();
  Let the selected tab remain in the most intermediate int total = (int) ((position + offset) * tabwidth);

  int green = (getwidth ()-tabwidth)/2;

  The scrolling distance int scroll = Total-green;
   if (!misclick) {this.scrollto (scroll, 0); Mcontaniergroup.scrollindicator (position, ofFset);

 }
 }

3. Finally, we use the adapter adapter mode to complete the highlight and subscript indicator.

Public abstract class Indicatoradapter<t extends View> {
 /**
  * Get tab View/public
 abstract T Gett Abview (int position);

 /**
  * Get the corner Mark
  View
 /Public View Getindexview () {return
  null;
 }

 /**
  * Highlight current tab
 /public void Highlighttabview (T tabview) {

 }

 /**
  * Restore current tab
  / Public
 void Restoretabview (T tabview) {

 }
}

4. Finally we look at how to use in the activity, there are four methods in the Indicatoradapter, we only need to implement a method, the remaining three methods if not used can no longer write. Exposed in the form of adapter, users can do it themselves without being limited.

 Mindicator.setadapter (Mviewpager, New indicatoradapter<textview> () {@Override public TextView gettabview (i
    NT position) {TextView Tabview = new TextView (Getbasecontext ());
    Tabview.settextcolor (Color.White);
    Tabview.settext (Mtitles.get (position));
    Tabview.setpadding (20, 20, 20, 20);
    Tabview.setgravity (Gravity.center);
   return tabview;
    @Override public View Getindexview () {ImageView view = new ImageView (Getbasecontext ());
    View.setimageresource (r.drawable.corners_login_nomral);
    View.setpadding (25, 0, 25, 0);
    Relativelayout.layoutparams params = new Relativelayout.layoutparams (layoutparams.wrap_content, 6);
    View.setlayoutparams (params);
   return view;
    @Override public void Highlighttabview (TextView tabview) {final TextView Itemview = (TextView) Tabview;
   Itemview.settextcolor (color.red); @Override public void Restoretabview (TextView tabview) {final TextView Itemview = (TextView) Tabview;
   Itemview.settextcolor (Color.White);

 }
  });

No need to configure the viewpagerindicator source download: Http://xiazai.jb51.net/201611/yuanma/AndroidViewPager (jb51.net). rar

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.