我正在使用http://viewpagerindicator.com/库。我想在Gmail应用上创建一个TitlePageIndicator
,如下图所示:
我确实想要实现同样的事情,第一个标题是'较新',最后标题是'较旧',中心标题是'[页面位置] [总页数]。是否可以使用我正在使用的库来实现这一点,还是应该对其进行修改?
答案 0 :(得分:1)
我有同样的问题。在Matthieu的帮助下解决了这样的问题。基于ViewPagerIndicator随附的样本。
基本上在页面更改时进行侦听,并根据当前位置更改适配器以提供不同的页面标题。下面给出的代码对我来说很有用。
如果你有这些样本,你只需要替换这两个文件,那么试试这个样本
更改了TestFragmentAdapter的代码,如下所示:
class TestFragmentAdapter extends FragmentPagerAdapter implements IconPagerAdapter {
protected static final String[] CONTENT = new String[] { "This", "Is", "stack", "overflow" };
protected static final int[] ICONS = new int[] {
R.drawable.perm_group_calendar,
R.drawable.perm_group_camera,
R.drawable.perm_group_device_alarms,
R.drawable.perm_group_location
};
private int mCount = CONTENT.length;
// CHANGE START HERE
private int current_position=0;
public void set_current_position(int i) {
current_position = i;
}
// CHANGE ENDS HERE
public TestFragmentAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return TestFragment.newInstance(CONTENT[position % CONTENT.length]);
}
@Override
public int getCount() {
return mCount;
}
@Override
public CharSequence getPageTitle(int position) {
// CHANGE START HERE
if (position == current_position-1) {
return "Previous";
} else if (position == current_position+1) {
return "Next";
}
// CHANGE ENDS HERE
return TestFragmentAdapter.CONTENT[position % CONTENT.length];
}
@Override
public int getIconResId(int index) {
return ICONS[index % ICONS.length];
}
public void setCount(int count) {
if (count > 0 && count <= 10) {
mCount = count;
notifyDataSetChanged();
}
}
}
这样的SampleTitlesDefault代码(添加了OnPageChangeListener)
public class SampleTitlesDefault extends BaseSampleActivity implements ViewPager.OnPageChangeListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_titles);
mAdapter = new TestFragmentAdapter(getSupportFragmentManager());
mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
mIndicator = (TitlePageIndicator)findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);
// CHANGE START HERE
mIndicator.setOnPageChangeListener(this);
// CHANGE ENDS HERE
}
// CHANGE START HERE
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
mPager = (ViewPager)findViewById(R.id.pager);
((TestFragmentAdapter)mPager.getAdapter()).set_current_position(i);
}
@Override
public void onPageScrollStateChanged(int i) {
}
// CHANGE ENDS HERE
}