@BindView(R.id.viewPager_item2)
ViewPager viewPager;
@BindView(R.id.button1_item2)
Button button1;
@BindView(R.id.button2_item2)
Button button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initView();
}
private void initView() {
initToolBar(findViewById(R.id.toolbar), true, getString(R.string.item2));
//button list
List<Button> buttons = new ArrayList<>();
buttons.add(button1);
buttons.add(button2);
//list fragment
List<Fragment> fragments = new ArrayList<>();
fragments.add(new VideoFragment());
fragments.add(new PictureFragment());
//创建adapter
PagerAdapter pagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
};
viewPager.setAdapter(pagerAdapter);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
for (Button button : buttons) {
button.setBackgroundColor(Color.WHITE);
button.setTextColor(Color.GRAY);
}
switch (position) {
case 0:
button1.setTextColor(Color.WHITE);
button1.setBackgroundColor(Color.GRAY);
break;
case 1:
button2.setTextColor(Color.WHITE);
button2.setBackgroundColor(Color.GRAY);
break;
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
button1.setBackgroundColor(Color.GRAY);
button1.setTextColor(Color.WHITE);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewPager.setCurrentItem(0);
for (Button button : buttons) {
button.setBackgroundColor(Color.WHITE);
button.setTextColor(Color.GRAY);
}
button1.setBackgroundColor(Color.GRAY);
button1.setTextColor(Color.WHITE);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewPager.setCurrentItem(1);
for (Button button : buttons) {
button.setBackgroundColor(Color.WHITE);
button.setTextColor(Color.GRAY);
}
button2.setBackgroundColor(Color.GRAY);
button2.setTextColor(Color.WHITE);
}
});
}
@Override
protected int getLayout() {
return R.layout.activity_item2;
}
@Override
public void onPointerCaptureChanged(boolean hasCapture) {
}
<include
layout="@layout/toolbar_main"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/button1_item2"
android:text="违章视频"
android:textColor="#000"
android:background="#fff"
android:layout_width="0px"
android:layout_weight="1"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button2_item2"
android:text="违章图片"
android:textColor="#000"
android:background="#fff"
android:layout_width="0px"
android:layout_weight="1"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:background="#07090A"
android:layout_width="match_parent"
android:layout_height="1px" />
<android.support.v4.view.ViewPager
android:id="@+id/viewPager_item2"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
网友评论