What are the implementation differences between TabPageIndicator and TitlePageIndicator?
-
05-06-2021 - |
Question
I've looked at the samples and the only difference seems to be the name, but when I just switch all the names in my app to TabPageIndicator from TitlePageIndicator, the tabs are in a messed up alignment and the footer underline is gone.
Am I missing something? Is there something extra I need to do? I have
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter( getSupportFragmentManager() );
ViewPager pager =
(ViewPager)findViewById( R.id.viewpager );
TabPageIndicator indicator =
(TabPageIndicator)findViewById( R.id.indicator );
pager.setAdapter( adapter );
indicator.setViewPager( pager );
and
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp" >
<com.viewpagerindicator.TabPageIndicator
android:id="@+id/indicator"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</FrameLayout>
Solution
What are the differences between TabPageIndicator and TitlePageIndicator?
Try viewing that page and see if you find the answer.
I did, that's why I posted to begin with.
Title Indicator
Displays the title of the selected page in the center with the titles of the adjacent pages (if available) in a more subtle style.
Tab Indicator
Similar to the title indicator but displays as many titles as possible in scrolling and animated horizontal tabs.
TitlePagerIndicator example:
Adapter
class TestFragmentAdapter extends FragmentPagerAdapter {
protected static final String[] CONTENT = new String[] { "This", "Is", "A", "Test", };
private int mCount = CONTENT.length;
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;
}
public void setCount(int count) {
if (count > 0 && count <= 10) {
mCount = count;
notifyDataSetChanged();
}
}
}
When you use TitlePagerIndicator
you need to implement TitleProvider
in your Adapter
.
class TestTitleFragmentAdapter extends TestFragmentAdapter implements TitleProvider {
public TestTitleFragmentAdapter(FragmentManager fm) {
super(fm);
}
@Override
public String getTitle(int position) {
return TestFragmentAdapter.CONTENT[position % CONTENT.length];
}
}
Putting it all together
TestFragmentAdapter mAdapter = new TestTitleFragmentAdapter(getSupportFragmentManager());
ViewPager mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
PageIndicator mIndicator = (TitlePageIndicator)findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);
TabPagerIndicator example:
public class SampleTabsDefault extends FragmentActivity {
private static final String[] CONTENT = new String[] { "Recent", "Artists", "Albums", "Songs", "Playlists", "Genres" };
TestFragmentAdapter mAdapter;
ViewPager mPager;
PageIndicator mIndicator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_tabs);
mAdapter = new GoogleMusicAdapter(getSupportFragmentManager());
mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
mIndicator = (TabPageIndicator)findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);
}
class GoogleMusicAdapter extends TestFragmentAdapter implements TitleProvider {
public GoogleMusicAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return TestFragment.newInstance(SampleTabsDefault.CONTENT[position % SampleTabsDefault.CONTENT.length]);
}
@Override
public int getCount() {
return SampleTabsDefault.CONTENT.length;
}
@Override
public String getTitle(int position) {
return SampleTabsDefault.CONTENT[position % SampleTabsDefault.CONTENT.length].toUpperCase();
}
}
}
All of these examples and more can be found on Jake Wharton's Github repo for ViewPagerIndicator