我在我的应用程序TitlePageIndicator中使用ViewPager并且工作正常。我可以左右开关,一切正常。问题是,我在其他应用程序中看到,如果您触摸viewpager中的某个标题,您也可以转到此屏幕。但不知道我该怎么做。
提前致谢
MainActivity
private ViewPager pager = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
this.pager = (ViewPager) this.findViewById(R.id.pager);
MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(
getSupportFragmentManager());
adapter.addFragment(Europe.newInstance(getResources()
.getColor(R.color.colorAccent), "Europe"));
adapter.addFragment(America.newInstance(getResources()
.getColor(R.color.colorAccent), "America"));
adapter.addFragment(Asia.newInstance(getResources()
.getColor(R.color.colorAccent), "Asia"));
adapter.addFragment(Africa.newInstance(getResources()
.getColor(R.color.colorPrimaryDark), "Africa"));
adapter.addFragment(Oceania.newInstance(getResources()
.getColor(R.color.colorAccent), "Oceania"));
this.pager.setAdapter(adapter);
TitlePageIndicator indicator = (TitlePageIndicator) findViewById(R.id.indicator);
final float density = getResources().getDisplayMetrics().density;
indicator.setFooterColor(R.color.setTextColor);
indicator.setFooterLineHeight(1 * density); //1dp
indicator.setFooterIndicatorHeight(3 * density); //3dp
indicator.setFooterIndicatorStyle(TitlePageIndicator.IndicatorStyle.Underline);
indicator.setTextColor(0xAA000000);
indicator.setTextSize(60);
indicator.setSelectedColor(R.color.setTextColor);
indicator.setSelectedBold(true);
indicator.setViewPager(pager);
}
FragmentAdapter
public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
private final String[] titles = {"one", "two", "three", "four"};
private List<Fragment> fragments;
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
this.fragments = new ArrayList<Fragment>();
}
public void addFragment(Fragment fragment) {
this.fragments.add(fragment);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return one.newInstance(0, "Page # 1");
case 1:
return two.newInstance(1, "Page # 2");
case 2:
return three.newInstance(2, "Page # 3");
case 3:
return four.newInstance(3, "Page # 4");
default:
return null;
}
}
@Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
@Override
public int getCount() {
return this.fragments.size();
}
}