最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android中TabLayout+ViewPager實(shí)現(xiàn)tab和頁面聯(lián)動(dòng)效果

 更新時(shí)間:2017年06月09日 11:02:49   作者:天鬼  
本篇文章主要介紹了Android中TabLayout+ViewPager實(shí)現(xiàn)tab和頁面聯(lián)動(dòng)效果,具有一定的參考價(jià)值,有興趣的可以了解一下

TabLayout+ViewPager實(shí)現(xiàn)tab和頁面聯(lián)動(dòng)效果

xml中:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:id="@+id/activity_main"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <android.support.design.widget.TabLayout
    android:id="@+id/toolbar_tl_tab"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:layout_gravity="bottom"
    app:layout_scrollFlags="scroll"
    app:tabIndicatorColor="@android:color/holo_green_light"
    app:tabSelectedTextColor="@android:color/holo_green_light" />

  <View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#f0f0f0" />

  <android.support.v4.view.ViewPager
    android:id="@+id/vp_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</RelativeLayout>

代碼中使用:

public class MainActivity extends AppCompatActivity {

  private TabLayout toolbar_tl_tab;
  private ViewPager vp_container;
  private String[] titles = {"標(biāo)題1", "標(biāo)題2", "標(biāo)題3", "標(biāo)題4"};

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    init();
  }

  private void init() {
    toolbar_tl_tab = (TabLayout) findViewById(R.id.toolbar_tl_tab);
    vp_container = (ViewPager) findViewById(R.id.vp_container);
    toolbar_tl_tab.setupWithViewPager(vp_container);
    toolbar_tl_tab.setTabMode(TabLayout.MODE_SCROLLABLE);
    vp_container.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
      @Override
      public Fragment getItem(int position) {
        return new PageFragment();
      }

      @Override
      public CharSequence getPageTitle(int position) {
        return titles[position];
      }

      @Override
      public int getCount() {
        return titles.length;
      }
    });
  }

}

碎片:PageFragment

public class PageFragment extends Fragment {
  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_page, null);
    return view;
  }
}

碎片xml:fragment_page.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">

  <TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:text="頁面" />
</LinearLayout>

注意:

1、模式相關(guān)

使用滾動(dòng)模式,特點(diǎn)是超過屏幕可以滾動(dòng)顯示:

toolbar_tl_tab.setTabMode(TabLayout.MODE_SCROLLABLE);

使用屏幕等分模式,特點(diǎn)是顯示tab的寬度是屏幕等分后的寬度:

toolbar_tl_tab.setTabMode(TabLayout.MODE_FIXED);

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

崇明县| 广德县| 双桥区| 乐清市| 四子王旗| 新巴尔虎右旗| 东乡族自治县| 宜都市| 长垣县| 武邑县| 平顺县| 宁强县| 灵武市| 微博| 清徐县| 武鸣县| 洛宁县| 开阳县| 建昌县| 肇东市| 富源县| 玉环县| 正安县| 轮台县| 柳河县| 临江市| 柘荣县| 轮台县| 大名县| 明星| 开封县| 鹿邑县| 平原县| 高邑县| 衢州市| 嘉鱼县| 临潭县| 长治县| 宁波市| 吉安市| 兖州市|