TabLayout, a new android M control, allows you to conveniently and quickly implement the tab function.

Source: Internet
Author: User

TabLayout, a new android M control, allows you to conveniently and quickly implement the tab function.
1. Overview

TabLayout is one of the new components of the new Android Support Design Library released by google at the google conference in 2015 to fully Support Material Design.

To use these new MD components, you must first install the Android Support Library in the EXTRA Support Library of the Android Studio SDK, as shown below:

This component also needs to be added to the AS dependency library in the design library.

FILE -- project struct

Right-click the app and select Library dependancy.

 

Select design Library

Then, AS automatically adds dependencies to the build. gradle file of the APP, AS shown in

TabLayout works with V ierPager, so you also need to import the dependency library support-v4 package of viewerpager (that is, the positive number of the second Library)

Now, the preparations have been completed!

In this example, you can use tablayout to implement the Tab switching function. Here, a framework is implemented, which is relatively simple and can be used directly for some extension.

2. Use Process

First, you must introduce Tablayout and viewpager in the layout file, and then associate vierpager through the adapter in the Activity. Each internal page is usually a fragment

2.1 Layout File

Introduce two components,

Common attributes are as follows (xmlns needs to be introduced: app = http://schemas.android.com/apk/res-auto)
App: tabSelectedTextColor: Color of the Selected Tab font
App: tabTextColor: Color of the unselected font of the Tab
App: tabIndicatorColor: The color of the Tab indicator.

 

                 
 

 

2.2. Each vierpager corresponds to its own fragment

Here are two tabs, so the two fragment are implemented. The entire demo code is omitted below.

2.3 interaction between viewPager + fragment and Tablayout

Generally, TabLayout is used together with ViewPager to take advantage of it.

 

Package com. elvis. tablayout_demo; import android. OS. bundle; import android. support. design. widget. tabLayout; import android. support. v4.app. fragment; import android. support. v4.app. fragmentPagerAdapter; import android. support. v4.view. viewPager; import android. support. v7.app. appCompatActivity; import android. view. menu; import android. view. menuItem; import com. elvis. tablayout_demo.fragment.FragmenthotRec; import com. elvis. tablayout_demo.fragment.Fragmenthotpot; import java. util. arrayList; import java. util. list; public class MainActivity extends AppCompatActivity {private TabLayout mTabLayout; private ViewPager mViewPager; private List
 
  
List_fragment; private Fragmenthotpot mHotpot; private FragmenthotRec mHotRec; // the title of tablayout private String [] mTitles = new String [] {today's hot spot, hot recommendation }; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); initViews ();} private void initViews () {mTabLayout = (TabLayout) findViewById (R. id. id_tablayout); mViewPager = (ViewPager) findViewById (R. id. id_viewpager); mHotpot = new Fragmenthotpot (); mHotRec = new FragmenthotRec (); list_fragment = new ArrayList <> (); Combine (mHotpot); merge (mHotRec ); /* viewPager is associated with fragment through the adapter */mViewPager. setAdapter (new FragmentPagerAdapter (getsuppfrfragmentmanager () {@ Override public Fragment getItem (int position) {return list_fragment.get (position) ;}@ Override public int getCount () {return mTitles. length ;}@ Override public CharSequence getPageTitle (int position) {return mTitles [position] ;}}); // mTabLayout associated with ViewPager. setupWithViewPager (mViewPager) ;}@ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. menu_main, menu); return true;} @ Override public boolean onOptionsItemSelected (MenuItem item) {// Handle action bar item clicks here. the action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest. xml. int id = item. getItemId (); // noinspection SimplifiableIfStatement if (id = R. id. action_settings) {return true;} return super. onOptionsItemSelected (item );}}
 

Here, many Tablayout attributes are set in the xml file. You can also set them in the code using the following methods:
-AddTab (TabLayout. Tab tab, int position, boolean setSelected) is added to layout.
-AddTab (TabLayout. Tab tab, boolean setSelected) is the same
-AddTab (TabLayout. Tab tab) is the same
-GetTabAt (int index) to obtain the tab
-GetTabCount (): returns the total number of tabs.
-GetTabGravity (): returns the Gravity of the tab.
-GetTabMode (): returns the tab mode.
-GetTabTextColors (): Obtain the text color in the tab.
-NewTab (): Create a new tab
-RemoveAllTabs () removes all tabs.
-RemoveTab (TabLayout. Tab): removes the specified tab.
-RemoveTabAt (int position): removes the tab at the specified position.
-SetOnTabSelectedListener (TabLayout. OnTabSelectedListener onTabSelectedListener) adds a selection listener for each tab.
-SetScrollPosition (int position, float positionOffset, boolean updateSelectedText)
-SetTabGravity (int gravity): Set Gravity.
-SetTabMode (int mode): sets the Mode. There are two values: TabLayout. MODE_SCROLLABLE and TabLayout. MODE_FIXED indicates whether horizontal sliding is supported when the tab content exceeds the screen width. The first option supports sliding, and the second option does not. horizontal sliding is not supported by default.
-SetTabTextColors (ColorStateList textColor): Set the text color in the tab.
-SetTabTextColors (int normalColor, int selectedColor)
-SetTabsFromPagerAdapter (PagerAdapter adapter): Set PagerAdapter
-SetupWithViewPager (ViewPager viewPager) is linked with ViewPager

 

 

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.