Android ViewPager每个标签一种颜色

时间:2016-08-04 10:12:07

标签: android android-fragments

我使用ViewPager在Activity上创建2个片段,如下所示:

public class ViewPagerAdapter extends FragmentStatePagerAdapter {
    int mNumOfTabs;

    public ViewPagerAdapter(FragmentManager fm, int NumOfTabs) {
        super(fm);
        this.mNumOfTabs = NumOfTabs;
    }

    @Override
    public Fragment getItem(int position) {

        switch (position) {
            case 0:
                Fragment1 tab1 = new Fragment1();
                return tab1;
            case 1:
                Fragment2 tab2 = new Fragment2();
                return tab2;
            default:
                return null;
        }
    }

    @Override
    public int getCount() {
        return mNumOfTabs;
    }
}

我在像这样的活动中使用它:

<android.support.design.widget.TabLayout
    android:layout_width="match_parent"
    android:layout_height="@dimen/tab_height"
    android:id="@+id/tab_layout"
    app:tabMode="fixed"
    app:tabGravity="fill"
    />

<android.support.v4.view.ViewPager
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/viewpager"
    android:layout_below="@id/tab_layout" />

这是包含片段的Activity中的代码:

ViewPager view_pager;
ViewPagerAdapter adapter;

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

    TabLayout tab_layout = (TabLayout) findViewById(R.id.tab_layout);
    tab_layout.addTab(tab_layout.newTab().setText("Tab1"));
    tab_layout.addTab(tab_layout.newTab().setText("Tab2"));

    view_pager = (ViewPager) findViewById(R.id.viewpager);

    adapter = new ViewPagerAdapter
            (getSupportFragmentManager(), tab_layout.getTabCount());

    final ViewPager view_pager = (ViewPager) findViewById(R.id.viewpager);

    view_pager.setAdapter(adapter);

    view_pager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tab_layout));

    tab_layout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {

        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            view_pager.setCurrentItem(tab.getPosition());
        }

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

        }

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

        }
    });

}

一切都很好,但我希望每个标签的颜色如下: enter image description here

我的意思是,当你打开每个标签时,它会改变颜色。

我该怎么做?

更新,关注LvN:
enter image description here

2 个答案:

答案 0 :(得分:0)

试试这个:

    yourViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
             // set your colors here 
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

答案 1 :(得分:0)

<强>已更新

您需要创建自定义标签视图才能实现此目的。

custom_tab.xml

<?xml version="1.0" encoding="utf-8"?>
  <TextView xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/tab"
  android:textColor="@color/colorAccent"
  android:textSize="@dimen/tab_label"
  android:fontFamily="@string/font_fontFamily_medium"/>

当您向标签添加项目时,请更改文本视图的背景颜色。

    TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
    tabOne.setText("TAB 1");
    tabOne.setBackgroundColor(Color.GREEN);        
    tabLayout.getTabAt(0).setCustomView(tabOne);

    TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
    tabTwo.setText("TAB 2");
    tabTwo.setBackgroundColor(Color.YELLOW);        
    tabLayout.getTabAt(1).setCustomView(tabTwo);