当我使用Tabs导航时,为什么图标不显示?

时间:2017-08-27 09:37:34

标签: android android-tablayout android-tabs android-tabstrip

当我从第一个标签移动到第二个标签时,图标改变了,但选择器仍然只在第二个选项卡上。  我想将图标移动到它的选项卡。首先是选择器,还有其他选择器 enter image description here

enter image description here

enter image description here

我使用此代码

public class ElectronicServiceActivity extends AppCompatActivity {
    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    ImageView backimage;
    Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_electronic_service);
        backimage = (ImageView) findViewById(R.id.back_id);
        backimage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

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

        mContext=ElectronicServiceActivity.this;
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        tabLayout = (TabLayout) findViewById(R.id.tabs);

        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setBackgroundResource(R.drawable.food_tab_selector);

        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int postion = tab.getPosition();

                if (postion == 0) {

                   tabLayout.setBackgroundResource(R.drawable.equipment_tab_selector);


                } else if (postion == 1) {
                    tabLayout.setBackgroundResource(R.drawable.drugs_tab_selector);


                } else if (postion == 2) {
                 tabLayout.setBackgroundResource(R.drawable.search_food_selector);


                }
            }

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

            }

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

            }
        });

    }

    private void setupViewPager(ViewPager viewPager) {
        ElectronicServiceDrugs electronicServiceDrugs = new ElectronicServiceDrugs();
        ElectronicServiceEquipment electronicServiceEquipment = new ElectronicServiceEquipment();
        ElectronicServiceFood electronicServiceFood = new ElectronicServiceFood();
        ElectronicServiceActivity.ViewPagerAdapter adapter = new ElectronicServiceActivity.ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(electronicServiceEquipment, "خدمات الاجهزه الطبيه");
        adapter.addFragment(electronicServiceDrugs, "خدمات دوائيه");
        adapter.addFragment(electronicServiceFood, "خدمات غذائيه");
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(2);

    }

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

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

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

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

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

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

}

我想将每个图标移动到它的标签上 当标题集中于。该图标也显示在

检查照片

1 个答案:

答案 0 :(得分:1)

您可能希望在标签布局项目上设置图标,而不是在标签布局本身上设置背景:

if (position == 0)
  mTabLayout.getTabAt(0).setIcon(R.drawable.your_icon);