TabLayout简单使用

今天写了一个关于TabLayout的简单案例,分享一下,里面可能有几个需要注意的点

案例图

TabLayout简单使用_第1张图片

注意这个需要关联design库

compile 'com.android.support:design:23.2.1'

1、主界面布局文件

<?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/id_tablayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingLeft="2dp" app:tabMode="fixed" app:tabSelectedTextColor="#00cc00"/>

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

这里需要注意TabLayout中的属性tabMode的属性分为scrollable和fixed,而如果使用scrollable的话,TabLayout的title内容就会,紧贴着左边显示,如果我们的title多的话,就无所谓,如果想博主这样,title比较少,只有3个或者2个,就要用fixed属性了,才能居中显示

使用scrollable的示意图:
TabLayout简单使用_第2张图片

2、主界面代码

package com.example.gimi.tablayoutdemo;

import android.app.Activity;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import java.util.ArrayList;

public class MainActivity extends FragmentActivity {

    private TabLayout tabLayout;
    private ViewPager viewPager;
    private ArrayList<Fragment> fragmentsList = new ArrayList<>();
    private ArrayList<String> mTitles = new ArrayList<>();
    private FragmentManager fragmentManager;
    private MyFragment fragment1;
    private MyFragment fragment2;
    private MyFragment fragment3;

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

    private void initView() {
        mTitles.add("我的");
        mTitles.add("产品");
        mTitles.add("观点");

        tabLayout = (TabLayout) findViewById(R.id.id_tablayout);
        viewPager = (ViewPager) findViewById(R.id.id_viewpager);

        fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.setCustomAnimations(android.R.anim.fade_in,android.R.anim.fade_out);
        fragment1 = MyFragment.newInstance("这是第一个fragment");
        fragment2 = MyFragment.newInstance("这是第二个fragment");
        fragment3 = MyFragment.newInstance("这是第三个fragment");
        fragmentsList.add(fragment1);
        fragmentsList.add(fragment2);
        fragmentsList.add(fragment3);

        MyPagerAdapter myPagerAdapter = new MyPagerAdapter(getSupportFragmentManager(),fragmentsList,mTitles);
        tabLayout.setSelected(true);
        tabLayout.setTabsFromPagerAdapter(myPagerAdapter);
        viewPager.setAdapter(myPagerAdapter);
        tabLayout.setupWithViewPager(viewPager);



        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }
}

3、Fragment代码

package com.example.gimi.tablayoutdemo;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/** * Created by Administrator on 2016/5/5. */
public class MyFragment extends Fragment {

    private TextView tv;

    static MyFragment newInstance(String s){
        MyFragment myFragment = new MyFragment();
        Bundle bundle = new Bundle();
        bundle.putString("DATA",s);
        myFragment.setArguments(bundle);
        return myFragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_fragment,container,false);
        Bundle bundle = getArguments();
        String data = bundle.getString("DATA");
        tv = (TextView) view.findViewById(R.id.id_fm_tv);
        if(data != null){
            tv.setText(data);
        }
        return view;
    }
}

4、MyPagerAdapter代码

package com.example.gimi.tablayoutdemo;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.ArrayList;
import java.util.List;

/** * Created by Administrator on 2016/5/5. */
public class MyPagerAdapter extends FragmentPagerAdapter {

    ArrayList<Fragment> fragmentsList;
    List<String> mTitles;

    public MyPagerAdapter(FragmentManager fm,ArrayList<Fragment> fragmentsList,ArrayList<String> titles) {
        super(fm);
        this.fragmentsList = fragmentsList;
        this.mTitles = titles;
    }

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


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

    //这里设置的是tablayout的title标题内容
    @Override
    public CharSequence getPageTitle(int position) {
        return mTitles.get(position);
    }

    @Override
    public int getItemPosition(Object object) {
        return super.getItemPosition(object);
    }
}

5、fragment布局文件

<?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">

    <TextView  android:id="@+id/id_fm_tv" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="fragment" android:textSize="25sp"/>

</LinearLayout>

你可能感兴趣的:(android)