我正在开发一个有3个标签的Android应用程序。我正在使用TabLayout
和ViewPager
。我计划给AppBar& 3提供3种不同的背景颜色。 TabLayout在3个不同的选项卡上。我需要在通过滚动或单击选项卡更改选项卡的同时在颜色之间进行平滑过渡。你是如何进行这种转变的?有人可以帮助我吗?
答案 0 :(得分:3)
我做样品。希望这有帮助。
在您的活动中:
mFab.setTranslationX(0)
MyAdapter是这样的:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tablayout);
int[] colors = {Color.RED, Color.GREEN, Color.BLUE};
viewPager.setAdapter(new MyAdapter());
viewPager.addOnPageChangeListener(new HeaderColorChanger(tabLayout, colors));
tabLayout.setupWithViewPager(viewPager);
}
HeaderColorChanger是这样的:
class MyAdapter extends PagerAdapter {
@Override
public Object instantiateItem(ViewGroup container, int position) {
TextView view = (TextView) LayoutInflater
.from(container.getContext())
.inflate(R.layout.row_item, container, false);
container.addView(view);
view.setText("Page:" + position);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public int getCount() {
return 3;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public CharSequence getPageTitle(int position) {
return "Page:" + position;
}
}