新特性:TabLayout动态添加条目

  • 布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include
        android:id="@+id/tool_bar"
        layout="@layout/layout_base_toolbar" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout_menu"
        android:layout_width="match_parent"
        android:layout_height="65dp"
        android:paddingTop="@dimen/padding_5"
        app:tabIndicatorColor="@color/light_yellow"
        app:tabSelectedTextColor="@color/light_yellow"
        app:tabTextColor="@color/gray_black" />


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

LinearLayout>
  • 在页面上进行添加

package cn.hnshangyu.zuyu.fragment.myself;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

import butterknife.Bind;
import cn.hnshangyu.zuyu.BaseActivity;
import cn.hnshangyu.zuyu.R;
import cn.hnshangyu.zuyu.utils.LogUtils;
import cn.hnshangyu.zuyu.utils.UIUtils;

/**
 * 我的模块中我的收藏
 */
public class MyCollectActivity extends BaseActivity {

    @Bind(R.id.tab_layout_menu)
    TabLayout mTl;
    @Bind(R.id.vp)
    ViewPager mVp;

    /**
     * 直播
     */
    private CollectLiveFragment mLive;
    /**
     * 点播
     */
    private CollectOnDemandFragment mOnDemand;
    /**
     * 商品
     */
    private CollectCommodityFragment mCommodity;
    /**
     * 人才
     */
    private CollectTalentsFragment mTalents;
    /**
     * 项目
     */
    private MyCollectFragment mProject;
    /**
     * 供需
     */
    private CollectSupplyDemandFragment mSupplyDemand;

    private ArrayList mFragmentList;
    /**
     * 适配器
     */
    private DemandAdapter mAdapter;
    private String[] stringName;
    /**
     * 图标
     */
    private int[] tabIcons = {
            R.drawable.selector_tab,
            R.drawable.selector_tab1,
            R.drawable.selector_tab2,
            R.drawable.selector_tab3,
            R.drawable.selector_tab4,
            R.drawable.selector_tab5
    };

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_collect;
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);

        stringName = getResources().getStringArray(R.array.my_collect_tab);

        //初始化各fragment
        mLive = CollectLiveFragment.newInstance();
        mOnDemand = CollectOnDemandFragment.newInstance();
        mCommodity = CollectCommodityFragment.newInstance();
        mTalents = CollectTalentsFragment.newInstance();
        mProject = MyCollectFragment.newInstance();
        mSupplyDemand = CollectSupplyDemandFragment.newInstance();

        //将fragment装进列表中
        mFragmentList = new ArrayList<>();
        mFragmentList.add(mLive);
        mFragmentList.add(mOnDemand);
        mFragmentList.add(mCommodity);
        mFragmentList.add(mTalents);
        mFragmentList.add(mProject);
        mFragmentList.add(mSupplyDemand);

        //设置TabLayout的模式  可滚动
//        mTl.setTabMode(TabLayout.MODE_FIXED);
        mTl.setTabMode(TabLayout.MODE_SCROLLABLE);

        mAdapter = new DemandAdapter(getSupportFragmentManager());
        //viewpager加载adapter
        mVp.setAdapter(mAdapter);
        //TabLayout加载viewpager
        mTl.setupWithViewPager(mVp);


        /**
         * 一定要在设置适配器之后设置Icon
         */
        mTl.getTabAt(0).setCustomView(getTabView(0));
        mTl.getTabAt(1).setCustomView(getTabView(1));
        mTl.getTabAt(2).setCustomView(getTabView(2));
        mTl.getTabAt(3).setCustomView(getTabView(3));
        mTl.getTabAt(4).setCustomView(getTabView(4));
        mTl.getTabAt(5).setCustomView(getTabView(5));
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void initView() {
        setTitleText(getString(R.string.my_collect));
        onShowTopBack(true);

    }




    public View getTabView(int position) {
        View view = LayoutInflater.from(this).inflate(R.layout.item_tab, null);
        ImageView iv_head = (ImageView) view.findViewById(R.id.iv_head);
        TextView tv_item = (TextView) view.findViewById(R.id.tv_item);
        TextView tv_number = (TextView) view.findViewById(R.id.tv_number);
        tv_item.setText(stringName[position]);
        if (position == 0) {
            tv_number.setVisibility(View.VISIBLE);
        } else {
            tv_number.setVisibility(View.GONE);
        }
        iv_head.setImageResource(tabIcons[position]);
        return view;
    }


    /**
     * 适配器
     */
    public class DemandAdapter extends FragmentStatePagerAdapter {


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

        @Override
        public Fragment getItem(int position) {

            return mFragmentList.get(position);
        }

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

//        //此方法用来显示tab上的名字
//        @Override
//        public CharSequence getPageTitle(int position) {
//            return getResources().getStringArray(R.array.my_collect_tab)[(position % mFragmentList.size())];
//        }

    }
}

fragment自己创建几个就好了……………

附加:tablayout选中指定位置

 mTl.getTabAt(tabPosition).select();

你可能感兴趣的:(Android控件使用)