UnderlinePageIndicator Based on ViewPagerIndicator, slide control on the bottom of the ViewPager tab, and viewpagerindicator

Source: Internet
Author: User

UnderlinePageIndicator Based on ViewPagerIndicator, slide control on the bottom of the ViewPager tab, and viewpagerindicator

UnderlinePageIndicator Based on ViewPagerIndicator and slide control on the bottom of the ViewPager Tab 

UnderlinePageIndicator (Appendix: Address), You can write a sliding control with a line at the bottom of the tab.
Control.


A special effect is that the slider at the bottom of the header gradually slide during ViewPager switching.
Code:

MainActivity. java

Package zhangphil. underline; import java. util. arrayList; import com. viewpagerindicator. underlinePageIndicator; import android. support. v4.app. fragment; import android. support. v4.app. fragmentManager; import android. support. v4.app. fragmentPagerAdapter; import android. support. v4.view. pagerAdapter; import android. support. v4.view. viewPager; import android. support. v7.app. actionBarActivity; import android. view. grav Ity; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; import android. widget. linearLayout; import android. widget. textView; import android. widget. linearLayout. layoutParams; import android. graphics. color; import android. OS. bundle;/*** Based on the UnderlinePageIndicator of the third-party open-source ViewPagerIndicator, you can write a sliding control with a line at the bottom of the tab. * Control. * A special effect is that when the tab of the header is switched over to ViewPager, the slider at the bottom gradually slides and transitions. * **/Public class MainActivity extends ActionBarActivity {private LinearLayout mLinearLayout; private ArrayList <Fragment> mArryList; private ViewPager mPager; // The font Color of the unselected tab is private int COLOR_NORMAL = Color. DKGRAY; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mArryList = new ArrayList <Fragment> (); // initialize five Fragmen T as a test. For (int I = 0; I <5; I ++) {TestFragment f = (TestFragment) TestFragment. newInstance (); f. id = I; mArryList. add (f);} PagerAdapter mAdapter = new MyFragmentAdapter (getSupportFragmentManager (); mPager = (ViewPager) findViewById (R. id. pager); mPager. setAdapter (mAdapter); UnderlinePageIndicator mIndicator = (UnderlinePageIndicator) findViewById (R. id. indicator); mIndicator. setViewPager (mPager); mIndicator. setF Ades (false); mIndicator. setSelectedColor (0xff33B5E5); mIndicator. setOnPageChangeListener (new ViewPager. onPageChangeListener () {@ Overridepublic void onPageSelected (int pos) {setIndicatorViewSelected (pos) ;}@ Overridepublic void onPageScrolled (int arg0, float arg1, int arg2) {}@ Overridepublic void onPageScrollStateChanged (int arg0) {}}); mLinearLayout = (LinearLayout) findViewById (R. id. tab_LinearLayout ); // Add the Tab addIndicators (); // initialization, 0th items are selected setIndicatorViewSelected (0);} // Add the Tab private void addIndicators () {for (int I = 0; I <getItemsCount (); I ++) {View v = getIndicatorAt (I); addIndicatorItem (v, I );}} // set the effect of tab changes when selected here. private void setIndicatorViewSelected (int pos) {for (int I = 0; I <mLinearLayout. getChildCount (); I ++) {if (I = pos) {View v = mLinearLayout. getChildAt (I); TextView TV = (TextV Iew) v; // Android Holo blue TV. setTextColor (0xff33B5E5);} else {View v = mLinearLayout. getChildAt (I); TextView TV = (TextView) v; TV. setTextColor (COLOR_NORMAL) ;}} protected int getItemsCount () {return mArryList. size () ;}// here, only one TextView is returned as the View of the tab. // More views can be returned here. Protected View getIndicatorAt (int pos) {TextView v = new TextView (this); v. setGravity (Gravity. CENTER); v. setText ("tab" + pos); v. setTextColor (COLOR_NORMAL); return v;} // Add a View in the linear layout to add events to the added View. Private void addIndicatorItem (View view, final int index) {LayoutParams params = new LayoutParams (LayoutParams. WRAP_CONTENT, LayoutParams. MATCH_PARENT, 1); mLinearLayout. addView (view, index, params); view. setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {// when the user clicks this View, the ViewPager setting is correct and the Pager Itemset (index );}});} private void set (int pos) {mPager. setCurrentItem (pos, true ); SetIndicatorViewSelected (pos);} // Fragment for testing only, identified by an id. Private static class TestFragment extends Fragment {public int id; public static Fragment newInstance () {return new TestFragment ();} public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {TextView v = new TextView (getActivity (); v. setGravity (Gravity. CENTER); v. setTextSize (50f); v. setText ("Fragment:" + id); return v ;}} private class MyFragmentAdapter extends FragmentPagerAdapter {public MyFragmentAdapter (FragmentManager fm) {super (fm );} @ Overridepublic Fragment getItem (int position) {return mArryList. get (position) ;}@ Overridepublic int getCount () {return mArryList. size ();}}}

Required layout file: activity_main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <LinearLayout        android:id="@+id/tab_LinearLayout"        android:layout_width="match_parent"        android:layout_height="30dip"        android:orientation="horizontal" />    <com.viewpagerindicator.UnderlinePageIndicator        android:id="@+id/indicator"        android:layout_width="match_parent"        android:layout_height="7px" />    <View        android:layout_width="match_parent"        android:layout_height="1px"        android:background="#33B5E5" />    <android.support.v4.view.ViewPager        android:id="@+id/pager"        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1" /></LinearLayout>


Zookeeper

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.