Silicon Valley News 8--tablayout replacement Viewpagerindicator

Source: Internet
Author: User

1. Associated Libraries

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

2. Layout Write on Tablayout

<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 Associations

Tablayout.setupwithviewpager (Viewpager);//Notice the change of the page after listening, Tabpageindicator monitor the change of the page// Tabpagerindicator.setonpagechangelistener (New Myonpagechangelistener ()); Viewpager.addonpagechangelistener (New Myonpagechangelistener ()); Tablayout.settabmode (tablayout.mode_scrollable)

  

4. Resolve compatibility issues with Tablayout and Viewpagerindicator 

4.1 <activity android:name= ". Activity. Mainactivity "Android:theme=" @style/theme.pageindicatordefaults "/>

4.2 Viewpagerindicator Library is modified as follows

<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=" @style/mycustomtablayout "        android:layout_height=" wrap_content "        android:layout_ weight= "1"/>

5.2. Values directory under Styles.xml inside

<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 Styles

1. style of the setting

Vp_news_menu_detailpager.setadapter (adapter); Tablayout.setupwithviewpager (Vp_news_menu_detailpager);// Set scrolling 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 the Setupwithviewpager must be called after Viewpager.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 ()); I Mageview img = (ImageView) View.findviewbyid (R.id.imageview); Img.setimageresource (r.drawable.dot_focus); return view ;}

Layout Files Tab_item.xml File

<?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>

  

  

  

  

Silicon Valley News 8--tablayout replacement Viewpagerindicator

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.