我有一个ViewPager,我在其中使用getPageTitle方法来获取当前页面的标题.
这是适配器代码:
@H_502_4@@Override public Fragment getItem(int i) { details = productData.get(i); Fragment fragment = new ProductViewFragment(); Bundle args = new Bundle(); args.putInt(ProductViewFragment.ARG_SECTION_NUMBER,i + 1); fragment.setArguments(args); return fragment; } @Override public int getCount() { return productData.size(); } public CharSequence getPageTitle(int position) { return (position+1)+" of "+myData.size(); }现在我想更新上一个和下一个片段的页面标题.我想将它们命名为“prevIoUs”和“next”.它也应该在后续页面上动态更新.
我能够获得当前页面标题号.例如,当我查看第5个片段时,当前片段将正确显示标题.在ViewPager的两个角上,它显示左侧的上一页标题编号和右侧的下一页标题编号.现在,我希望左侧的“上一个”和右侧的“下一个”页面标题类似于Gmail应用程序以及它如何显示ViewPager中的邮件计数.
解决方法
我的基础是ViewPagerIndicator附带的样本.
您基本上在页面更改时监听,并告诉适配器根据当前位置显示不同的页面标题.
如果你有这些样本工作,你只需要替换这两个文件,然后尝试样本标题 – >默认,它对我来说工作正常…
更改了TestFragmentAdapter的代码,如下所示:
@H_502_4@package com.viewpagerindicator.sample; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import com.viewpagerindicator.IconPagerAdapter; class TestFragmentAdapter extends FragmentPagerAdapter implements IconPagerAdapter { protected static final String[] CONTENT = new String[] { "This","Is","A","Test",}; 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 STARTS 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 STARTS 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)
@H_502_4@package com.viewpagerindicator.sample; import android.os.Bundle; import android.support.v4.view.ViewPager; import com.viewpagerindicator.TitlePageIndicator; // CHANGE ADDED implements.... HERE 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 STARTS HERE mIndicator.setOnPageChangeListener(this); // CHANGE ENDS HERE } // CHANGE STARTS 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 }