在viewPager内部的标签中添加图标

时间:2018-12-29 16:45:06

标签: java android android-fragments tabs android-viewpager

我正在尝试在ViewPager的3个选项卡中添加图标。 我找不到任何解决方案。 如果有其他解决方案可用,我准备完全更改我的代码 非常感谢你的回答。 大家好, 我正在尝试在ViewPager的3个选项卡中添加图标。 我找不到任何解决方案。 如果有其他解决方案可用,我准备完全更改我的代码 非常感谢您的回答。

    package com.example.septime.locumatix.Activities;


import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import com.example.septime.locumatix.Fragments.GrosBonnetAnalyseFragment;
import com.example.septime.locumatix.Fragments.GrosBonnetExercicesFragment;
import com.example.septime.locumatix.Fragments.GrosBonnetPlusLoinFragment;

import com.example.septime.locumatix.R;

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

public class GrosBonnetActivity extends AppCompatActivity {

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

        Toolbar toolbar = findViewById(R.id.mytoolbar);

        ViewPager viewPager = findViewById(R.id.viewpager);
        setViewPager(viewPager);

        TabLayout tabLayout = findViewById(R.id.mytabs);
        tabLayout.setupWithViewPager(viewPager);
    }
    private void setViewPager(ViewPager viewPager) {
        GrosBonnetActivity.ViewPagerAdapter adapter = new GrosBonnetActivity.ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new GrosBonnetAnalyseFragment(), "Analyse");
        adapter.addFragment(new GrosBonnetExercicesFragment(), "Exercices");
        adapter.addFragment(new GrosBonnetPlusLoinFragment(), "Plus loin");
        viewPager.setAdapter(adapter);
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

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

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

        void addFragment(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

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

1 个答案:

答案 0 :(得分:0)

已经是described here。您只需要method setCustomView