欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android中TabLayout結(jié)合ViewPager實現(xiàn)頁面切換

 更新時間:2022年04月24日 11:08:51   作者:熊,我-  
這篇文章主要為大家詳細介紹了Android中TabLayout結(jié)合ViewPager實現(xiàn)頁面切換效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android中TabLayout結(jié)合ViewPager實現(xiàn)頁面切換,供大家參考,具體內(nèi)容如下

一、實現(xiàn)思路

1、在build.gradle中添加依賴,例如:

compile 'com.android.support:support-v4:23.4.0'
compile 'com.android.support:design:23.4.0'

也可以將support-v4替換為appcompat-v7,例如:

compile 'com.android.support:appcompat-v7:23.4.0'

因為appcompat-v7是依賴于support-v4的。

更多說明可參考官方文檔support library部分。

2、在xml中添加TabLayout和ViewPager,例如:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:tool="http://schemas.android.com/tools"
       xmlns:app="http://schemas.android.com/apk/res-auto"
       tool:context=".TabViewActivity"
       android:orientation="vertical"
       android:layout_width="match_parent"
       android:layout_height="match_parent">

  <android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/tabLayoutBackground"
    app:tabMode="scrollable"
    app:tabTextColor="@color/color_white"
    app:tabSelectedTextColor="@color/tabSelectedText"
    app:tabIndicatorHeight="3dp"
    app:tabIndicatorColor="@color/color_white"/>

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

</LinearLayout>

TabLayout:

(1)tabMode有兩個屬性,一個是"scrollable",用于多標(biāo)簽;另一個是"fixed",用于少標(biāo)簽,它會讓全部標(biāo)簽平均分布在屏幕上,所以標(biāo)簽不能多,而且名稱也不能長,否則會顯示不完整。

(2)tabIndicator是指文本下的指示條。當(dāng)選中一個tab時,指示條才會出現(xiàn),出現(xiàn)在文本下面。

3、獲取View對象

TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);

4、創(chuàng)建FragmentStatePagerAdaper的子類,并實現(xiàn)構(gòu)造方法

  public class ViewPagerAdapter extends FragmentStatePagerAdapter {
    public ViewPagerAdapter(FragmentManager fm) {
      super(fm);
    }
  }

 創(chuàng)建該類的一個實例對象

ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager()); 

在這一步中,你可以選擇是實現(xiàn)FragmentPagerAdapter的子類,或者是FragmentStatePagerAdapter的子類。

FragmentPagerAdapter用于頁數(shù)較少的,也就Fragment的數(shù)量較少的,因為只要用戶還停留在當(dāng)前的Activity中,其中的Fragment都不會被銷毀,所以內(nèi)存消耗會比較大。

而FragmentStatePagerAdapter的工作原理類似于ListView,只要用戶不可見的Fragment,都會被銷毀,只保留它的狀態(tài)。

因為我用的是v4兼容包下的Fragment,所以需要用getSupportFragmentManager()去獲取FragmentManager。

5、設(shè)置ViewPager和TabLayout

 viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);

二、完善Adapter

1、重寫三個方法

  public class ViewPagerAdapter extends FragmentStatePagerAdapter {

    ......

    @Override
    public Fragment getItem(int position) {
      return null;
    }

    @Override
    public int getCount() {
      return 0;
    }

    @Override
    public CharSequence getPageTitle(int position) {
      return super.getPageTitle(position);
    }
  }

2、創(chuàng)建tab的標(biāo)題數(shù)據(jù):

 private String[] mTitles = new String[]{"語文", "英語", "數(shù)學(xué)", "物理", "生物", "化學(xué)", "地理", "政治", "歷史"};

創(chuàng)建Fragment的子類:

public class ViewPagerFragment extends Fragment {

  private static final String KEY = "extra";
  private String mMessage;

  public ViewPagerFragment() {
  }

  public static ViewPagerFragment newInstance(String extra) {
    Bundle args = new Bundle();
    args.putString(KEY, extra);
    ViewPagerFragment fragment = new ViewPagerFragment();
    fragment.setArguments(args);
    return fragment;
  }
}

創(chuàng)建Fragment的集合對象,并添加實例對象到集合里:

private ArrayList<ViewPagerFragment> mViewPagerFragments = new ArrayList<>();

    ......

    for (int i = 0; i < mTitles.length; i++) {
      mViewPagerFragments.add(ViewPagerFragment.newInstance(mTitles[i]));
    }

 3、修改Adapter中的方法

public class ViewPagerAdapter extends FragmentStatePagerAdapter {

    private String[] titles;
    private ArrayList<ViewPagerFragment> viewPagerFragments;

    public ViewPagerAdapter(FragmentManager fm) {
      super(fm);
    }

    public void setTitles(String[] titles) {
      this.titles = titles;
    }

    public void setFragments(ArrayList<ViewPagerFragment> viewPagerFragments) {
      this.viewPagerFragments = viewPagerFragments;
    }

    @Override
    public Fragment getItem(int position) {
      return viewPagerFragments.get(position);
    }

    @Override
    public int getCount() {
      return viewPagerFragments.size();
    }

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

 4、將數(shù)據(jù)傳給Adapter

 viewPagerAdapter.setTitles(mTitles);
viewPagerAdapter.setFragments(mViewPagerFragments); 

三、完善Fragment

1、fragment_view_pager_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:orientation="vertical"
       android:layout_width="match_parent"
       android:layout_height="match_parent">

  <TextView
    android:id="@+id/fragment_text"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"/>

</LinearLayout>

2、完善Fragment的方法

public class ViewPagerFragment extends Fragment {

  ......

  @Override
  public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle bundle = getArguments();
    if (bundle != null) {
      mMessage = bundle.getString(KEY);
    }
  }

  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_view_pager_item, container, false);
    TextView textView = (TextView) view.findViewById(R.id.fragment_text);
    textView.setText(mMessage);
    return view;
  }
}

在創(chuàng)建Fragment時,會調(diào)用onCreate方法,在其中執(zhí)行一些狀態(tài)信息的初始化,用于暫?;蛲V购蟮幕謴?fù)所用。

在Fragment首次加載視圖時,會調(diào)用onCreateView方法,在其中執(zhí)行視圖的加載和初始化,返回的應(yīng)該是該Fragment布局的根視圖。其中inflate方法的第三個參數(shù)代表的意思是,是否要將加載進來的布局(R.layout.fragment_view_pager_item)添加進container這個ViewGroup里。根據(jù)官方文檔的說明,上例那樣做的話,系統(tǒng)已經(jīng)將這個布局添加進container了,所以這里為false。

 靜態(tài)效果圖:

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

相關(guān)文章

最新評論