TabLayout两种带图标的Tab方法

第一种:TabLayout+ViewPager

主要通过tab.setCustomView()设置图片的

public class SimpleFragmentPagerAdapter extends FragmentViewPageAdapter{

    private List<String>mTitle;
    private List<Fragment> mList;
    private Context mContext;
    public SimpleFragmentPagerAdapter(FragmentManager fm, List<Fragment> mList, List<String> mTitle,Context mContext) {
        super(fm, mList, mTitle);
        this.mContext=mContext;
        this.mList=mList;
        this.mTitle=mTitle;
    }


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

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


    /**
     * 获取标题
     * @param position
     * @return
     */
    @Override
    public CharSequence getPageTitle(int position) {
        SpannableString sb = new SpannableString("   " +       mTitle.get(position));
        if (position==2){
            Drawable image = mContext.getResources().getDrawable(R.mipmap.select);
            image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
            ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM);
            sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            return sb;
        }
        else {
            return mTitle.get(position);
        }
        // Replace blank spaces with image icon

    }

    /**
     * 也可以写到Activity中  通过tab  setCustomView  改变选项卡
     * @param position
     * @return
     */
    public View getTabView(int position){
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_pager, null);
        TextView tv= (TextView) view.findViewById(R.id.textView);
        tv.setText(mTitle.get(position));
        ImageView img = (ImageView) view.findViewById(R.id.imageView);
        if (position==2){
            img.setImageResource(R.mipmap.select);
        }

        return view;
    }

在Activity中

 mListFragment=new ArrayList<>();
//        mTitles.add("热度");
        mTitles.add("销量");
        mTitles.add("价格");
        mTitles.add("优惠");
//        mListFragment.add(ValidFragment.getIntance("数据"));
        mListFragment.add(new InvalidFragment());
        mListFragment.add(ValidFragment.getIntance("数据"));
        mListFragment.add(new InvalidFragment());

        adapter=new   SimpleFragmentPagerAdapter(getSupportFragmentManager(),mListFragment,mTitles,this);
        wallet_viewpager.setAdapter(adapter);
        mTablayout.setupWithViewPager(wallet_viewpager);
        mTablayout.setTabGravity(TabLayout.GRAVITY_FILL);
         //设置
        for (int i = 0; i < mTablayout.getTabCount(); i++) {
            if (i==2){
                TabLayout.Tab tab = mTablayout.getTabAt(i);
                tab.setCustomView(adapter.getTabView(i));
            }

        }

谢谢泡在网上的日子

第二种方法Tablayout+LinearLayout

mTitles = new ArrayList<>();
        mListFragment = new ArrayList<>();
        mTitles.add("热度");
        mTitles.add("销量");
        mTitles.add("价格");
        mTitles.add("优惠");
        /**
         * 通过标题的数量动态添加Tab
         */
        for (int i = 0; i < mTitles.size(); i++) {
            TabLayout.Tab tab = mTablayout.newTab();
            View view = LayoutInflater.from(this).inflate(R.layout.item_pager, null);
            TextView tv = (TextView) view.findViewById(R.id.textView);
            tv.setText(mTitles.get(i));
            ImageView img = (ImageView) view.findViewById(R.id.imageView);
            /**
             * 自定义一个布局
             */
            tab.setCustomView(view);
            if (i == 2) {
                img.setVisibility(View.VISIBLE);
                img.setBackgroundResource(R.mipmap.select);
                tv.setFocusable(true);
            }
            mTablayout.addTab(tab);

        }

选中的代码

mTablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mFragmentManager = getSupportFragmentManager();
                mFragmentTransction = mFragmentManager.beginTransaction();
                switch (tab.getPosition()) {

                    case 0:
                        if (InvalidFragment == null) {
                            InvalidFragment = new InvalidFragment();
                        }
                        mFragmentTransction.replace(R.id.tb, InvalidFragment);
                        break;
                    case 1:
                        if (validFragment == null) {
                            validFragment = ValidFragment.getIntance("数据");
                        }
                        mFragmentTransction.replace(R.id.tb, validFragment);
                        break;
                    case 2:
                        if (validFragment == null) {
                            validFragment = ValidFragment.getIntance("数据");
                        }
                        mFragmentTransction.replace(R.id.tb, validFragment);
                        break;
                    case 3:
                        if (InvalidFragment == null) {
                            InvalidFragment = new InvalidFragment();
                        }
                        mFragmentTransction.replace(R.id.tb, InvalidFragment);
                        break;


                }
                mFragmentTransction.commit();
            }

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

            }

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

            }
        });

tab 布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_centerInParent="true"
    android:layout_centerHorizontal="true"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView"
        android:gravity="center"
        android:layout_centerInParent="true"
        android:textColor="@color/white"
        android:layout_marginLeft="3dp"
        android:text="fffffff"
        />
    <ImageView

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:visibility="gone"
        android:layout_toRightOf="@+id/textView"
        android:layout_centerVertical="true"

        />

</RelativeLayout>
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值