天天看點

使用TabLayout快速實作一個導航欄1.添加依賴檔案2.在布局檔案中引入TabLayout3.建立Fragment4.自定義一個FragmentPagerAdapter

在沒有Material Design的年代,要實作一個類似微信首頁面的效果,我們有以下幾種解決方案:

1.Fragment + ViewPager  +  RadioGroup自定義固定導覽列

2.Fragment + ViewPager  帶滑動導覽列

3.Fragment + ViewPager +  HorizontalScrollView自定義滑動導覽列

當然,除了這些之外,還有許多已經被Google丢棄的方案,我們就不說了。當有了Material Design之後,一切都變得那麼漂亮,也變得那麼簡單,我們今天就來看看怎麼用TabLayout快速實作一個導航欄。先來看看效果圖:

使用TabLayout快速實作一個導航欄1.添加依賴檔案2.在布局檔案中引入TabLayout3.建立Fragment4.自定義一個FragmentPagerAdapter

這就是我們要實作的一個效果,好了,開始吧。

1.添加依賴檔案

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

在gradle檔案中添加上面兩個檔案的依賴。

2.在布局檔案中引入TabLayout

主布局檔案如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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_layout_navi"
        android:layout_width="match_parent"
        app:tabMode="scrollable"
        android:layout_height="wrap_content" />

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"></android.support.v4.view.ViewPager>
</LinearLayout>
           

通過上面的示例圖我們已經看到了,我們的App上面是一個TabLayout,下面是一個ViewPager,是以我們的布局檔案中添加這兩個東西就可以了,注意TabLayout的引用方式。

3.建立Fragment

實際開發中我們可能需要建立多個Fragment,在這裡做示例我就隻建立一個,然後多次使用,思路是這樣的,每次執行個體化一個Fragment的時候,傳入該Fragment要顯示的文本,代碼如下:

public class MyFragment extends Fragment {
    private static final String ARG_PARAM1 = "param1";
    private String mParam1;

    public static MyFragment newInstance(String param1) {
        MyFragment fragment = new MyFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my, null);
        TextView content = (TextView) view.findViewById(R.id.fg_tv);
        content.setText(mParam1);
        return view;
    }
}
           

Fragment的布局檔案是這樣的:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="lenve.tablayouttest.fragment.MyFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:id="@+id/fg_tv"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>
           

4.自定義一個FragmentPagerAdapter

這個FragmentPagerAdapter我們在之前使用ViewPager的時候都有自定義過,這裡就不多說了,直接上代碼:

public class MyFragmentAdapter extends FragmentPagerAdapter {
    private final int PAGE_COUNT = 3;
    private final String[] titles;
    private Context context;
    private List<Fragment> fragments;

    public MyFragmentAdapter(List<Fragment> fragments,String[] titles, FragmentManager fm, Context context) {
        super(fm);
        this.context = context;
        this.fragments = fragments;
        this.titles = titles;
    }

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

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

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

注意,這裡有個不同的地方就是我們重寫了方法getPageTitle,這個方法傳回ViewPager中每個Fragment對應的title。

最後,我們再來看看MainActivity,首先初始化資料,初始化Fragment,這些都是ViewPager基本用法,我們來看看最後一句,這一句就是将ViewPager和TabLayout綁定在一起,實作了ViewPager和TabLayout的同步。

public class MainActivity extends AppCompatActivity {

    private String[] titles = new String[]{"聊天", "好友", "發現", "我的","聊天", "好友", "發現", "我的","聊天", "好友", "發現", "我的"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
        List<Fragment> fragments = new ArrayList<Fragment>();
        for (int i = 0; i < titles.length; i++) {
            fragments.add(MyFragment.newInstance(titles[i]));
        }
        FragmentPagerAdapter adapter = new MyFragmentAdapter(fragments, titles, getSupportFragmentManager(), this);
        viewPager.setAdapter(adapter);
        TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout_navi);
        tabLayout.setupWithViewPager(viewPager);
    }
}
           

Demo下載下傳http://download.csdn.net/detail/u012702547/9349175