Android tablayout (tab layout) Simple Usage Example Analysis _android

Source: Internet
Author: User

This example describes the Android tablayout (tab layout) simple usage. Share to everyone for your reference, specific as follows:

When we apply Viewpager, we often use tabpageindicator to work with them. To achieve a very beautiful effect. But Tabpageindicator is a third party, and more old, of course, now many great gods have begun to write their own tabpageindicator to meet their needs, at the 2015 Google Conference, Google released a new Android Support Design Library, which contains a few new controls, one of which has a tablayout, it can complete the tabpageindicator effect, but also official, the best is that it can be compatible with more than 2.2 version, including 2.2. Let me give you a simple example to use it.

The Android Studio is developed here, so quoting Tablayout is simple, just add compile ' com.android.support:design:22.2.0 ' to the Build.gradle.

The use of this is in my knowledge of the time. So the page is as good as the knowledge.

Fragment_find.xml

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
  android:layout_width= "Match_ Parent "
  android:layout_height=" match_parent "
  xmlns:app=" Http://schemas.android.com/apk/res-auto
  " android:orientation= "vertical" >
  <android.support.design.widget.tablayout
    android:id= "@+id/tab_ Findfragment_title "
    android:layout_width=" match_parent "
    android:layout_height=" Wrap_content
    " android:background= "@color/titleblue"
    app:tabindicatorcolor= "@color/white"
    app:tabselectedtextcolor= "@color/gray"
    app:tabtextcolor= "@color/white"
    />
  <android.support.v4.view.viewpager
    Android:id= "@+id/vp_findfragment_pager"
    android:layout_width= "fill_parent"
    android:layout_height= " 0DP "
    android:layout_weight=" 1 "
    />
</LinearLayout>

There is nothing special about this, just adding a tablayout and Viewpager as the top and bottom layout. which

App:tabindicatorcolor= "@color/white"//below the scrolling underline color
app:tabselectedtextcolor= "@color/gray"//tab is selected, the text color
app:tabtextcolor= "@color/white"//tab Default text color

Find_tab_adapter.java It is Viewpager's Adapter, because there are some lists in each of my columns, so using the list<view> way, switching layout inside is not very suitable, So I used a list<fragment> to load multiple Fragment directly.

 package com.example.cg.myzhihu.Adapters; import android.support.v4.app.Fragment;
Import Android.support.v4.app.FragmentManager;
Import Android.support.v4.app.FragmentPagerAdapter;
Import java.util.List;
 /** * Created by CG on 2015/9/26. 
  * * public class Find_tab_adapter extends Fragmentpageradapter {private list<fragment> list_fragment;//fragment List Private list<string> List_title; List of tab names public Find_tab_adapter (Fragmentmanager fm,list<fragment> list_fragment,list<string> list_
    Title) {super (FM);
    This.list_fragment = list_fragment;
  This.list_title = List_title;
  @Override public Fragment getitem (int position) {return list_fragment.get (position);
  @Override public int GetCount () {return list_title.size (); //This method is used to display the name on the tab @Override public charsequence getpagetitle (int position) {return List_title.get (position% l
  Ist_title.size ()); }
}

Findfragment.java This, it's all in the label.

Package Com.example.cg.myzhihu;
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.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Com.example.cg.myzhihu.Adapters.Find_tab_Adapter;
Import java.util.ArrayList;
Import java.util.List; /** * Discovery Page */public class Findfragment extends Fragment {private tablayout tab_findfragment_title;//define Tablayout P Rivate Viewpager Vp_findfragment_pager; Define Viewpager private Fragmentpageradapter fadapter; Define adapter private list<fragment> list_fragment; Define the list to be loaded fragment private list<string> list_title; tab Name list private find_hotrecommendfragment hotrecommendfragment; Hot recommended fragment private find_hotcollectionfragment hotcollectionfragment; Popular Favorites Fragment Private find_hotmonthfragment hotmonthfragment;
 Hot List of the month fragment Private Find_hottoday Hottoday; Hot List Today fragment @Override public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle
    Savedinstancestate) {View view = Inflater.inflate (R.layout.fragment_find, container, false);
    Initcontrols (view);
  return view; /** * Initialize the controls * @param view */private void Initcontrols (view view) {Tab_findfragment_title = (tablayou
    T) View.findviewbyid (r.id.tab_findfragment_title);
    Vp_findfragment_pager = (Viewpager) View.findviewbyid (R.id.vp_findfragment_pager);
    Initialization of each fragment hotrecommendfragment = new Find_hotrecommendfragment ();
    Hotcollectionfragment = new Find_hotcollectionfragment ();
    Hotmonthfragment = new Find_hotmonthfragment ();
    Hottoday = new Find_hottoday ();
    Load the fragment into the list list_fragment = new arraylist<> ();
    List_fragment.add (hotrecommendfragment);
    List_fragment.add (hotcollectionfragment);
    List_fragment.add (hotmonthfragment); List_fragment.aDD (hottoday);
    To load the tab name list with the name, normally we should define it in Values/arrays.xml and then call List_title = new arraylist<> ();
    List_title.add ("Hot Recommendation");
    List_title.add ("popular collection");
    List_title.add ("Hot List of the month");
    List_title.add ("Hot List Today");
    Set the Tablayout mode Tab_findfragment_title.settabmode (tablayout.mode_fixed);
    Add a tab name to the Tablayout Tab_findfragment_title.addtab (Tab_findfragment_title.newtab (). SetText (list_title.get (0)));
    Tab_findfragment_title.addtab (Tab_findfragment_title.newtab (). SetText (List_title.get (1)));
    Tab_findfragment_title.addtab (Tab_findfragment_title.newtab (). SetText (List_title.get (2)));
    Tab_findfragment_title.addtab (Tab_findfragment_title.newtab (). SetText (List_title.get (3)));
    Fadapter = new Find_tab_adapter (getactivity (). Getsupportfragmentmanager (), list_fragment,list_title);
    Viewpager Loading Adapter Vp_findfragment_pager.setadapter (Fadapter);
    Tab_findfragment_title.setviewpager (Vp_findfragment_pager); Tablayout Loading Viewpager Tab_findfragmeNt_title.setupwithviewpager (Vp_findfragment_pager);

 Tab_findfragment_title.set}}

The effect chart is not very dynamic:

I hope this article will help you with the Android program.

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.