Silicon Valley News 8 -- replace ViewPagerIndicator with TabLayout and tablayoutindicator

Source: Internet
Author: User

Silicon Valley News 8 -- replace ViewPagerIndicator with TabLayout and tablayoutindicator

1. associated database

Compile 'com. android. support: design: 23.3.0'

 

2. Write TabLayout to the layout.

 <android.support.design.widget.TabLayout    android:id="@+id/tabLayout"    android:layout_width="0dp"    android:layout_height="wrap_content"    android:layout_gravity="center_vertical"    android:layout_weight="1" />

 

3. TabLayout and ViewPager Association

TabLayout. setupWithViewPager (viewPager); // pay attention to subsequent page changes. TabPageIndicator listens to page changes. // tabPagerIndicator. setOnPageChangeListener (new MyOnPageChangeListener (); viewPager. addOnPageChangeListener (new MyOnPageChangeListener (); tabLayout. setTabMode (TabLayout. MODE_SCROLLABLE)

  

4. Solve the compatibility problem between TabLayout and ViewPagerIndicator. 

4.1 <activity android: name = ". activity. MainActivity" android: theme = "@ style/Theme. PageIndicatorDefaults"/>

4.2 modify the ViewPagerIndicator Library to the following:

<style name="Theme.PageIndicatorDefaults" parent="Theme.AppCompat.Light.NoActionBar">    <item name="vpiIconPageIndicatorStyle">@style/Widget.IconPageIndicator</item>    <item name="vpiTabPageIndicatorStyle">@style/Widget.TabPageIndicator</item></style>

4.3 add V7 package compile 'com. android. support: appcompat-v7: 23.3.0'

 

5. TabLayout set pointer color and Height & text color

5. 1. Add a style

<android.support.design.widget.TabLayout        android:id="@+id/tabLayout"        android:layout_width="wrap_content"       > <style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">        <item name="tabMaxWidth">72dp</item>        <item name="tabMinWidth">72dp</item>        <item name="tabIndicatorColor">#ff0000</item>        <item name="tabIndicatorHeight">2dp</item>        <item name="tabTextAppearance">@style/MyCustomTabTextAppearance</item>        <item name="tabSelectedTextColor">@android:color/holo_red_light</item>    </style>    <style name="MyCustomTabTextAppearance" parent="TextAppearance.Design.Tab">        <item name="android:textSize">16sp</item>        <item name="android:textColor">@android:color/black</item>        <item name="textAllCaps">false</item>    </style>

 

6. TabLayout custom Style

1. Set the style

Vp_news_menu_detailpager.setAdapter (adapter); tabLayout. setupWithViewPager (vp_news_menu_detailpager); // sets the rolling mode tabLayout. setTabMode (TabLayout. MODE_SCROLLABLE); for (int I = 0; I <tabLayout. getTabCount (); I ++) {TabLayout. tab tab = tabLayout. getTabAt (I); tab. setCustomView (getTabView (I);} // note that setupWithViewPager must be in ViewPager. call after setAdapter ()

2. Add the getTabView () method to the adapter.

public View getTabView(int position){View view = LayoutInflater.from(context).inflate(R.layout.tab_item, null);TextView tv= (TextView) view.findViewById(R.id.textView);tv.setText(childrenDatas.get(position).getTitle());ImageView img = (ImageView) view.findViewById(R.id.imageView);img.setImageResource(R.drawable.dot_focus);return view;}

Layout file tab_item.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center_horizontal"android:orientation="vertical"><TextViewandroid:text="text"android:id="@+id/textView"android:textColor="#000000"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="3dp" /><ImageViewandroid:src="@drawable/dot_focus"android:id="@+id/imageView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true" /></LinearLayout>

  

  

 

  

  

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.