2

我查看了示例,唯一的区别似乎是名称,但是当我将应用程序中的所有名称从 TitlePageIndicator 切换到 TabPageIndicator 时,选项卡的对齐方式混乱,页脚下划线消失了。

我错过了什么吗?我需要做些额外的事情吗?我有

    @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 );

    <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>
4

1 回答 1

5

TabPageIndicator 和 TitlePageIndicator 有什么区别?

尝试查看该页面,看看您是否找到了答案。

我做到了,这就是我开始发布的原因。

标题指示符

将所选页面的标题与相邻页面的标题(如果可用)以更微妙的风格显示在中心。

标签指示器

类似于标题指示器,但在滚动和动画水平选项卡中显示尽可能多的标题。

TitlePagerIndicator 示例:

适配器

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();
        }
    }
}

当你使用时,TitlePagerIndicator你需要TitleProvider在你的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];
    }
}

把它们放在一起

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 示例:

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();
        }
    }
}

所有这些示例以及更多示例都可以在Jake Wharton 的 ViewPagerIndicator 的 Github 存储库中找到

于 2012-05-04T06:17:59.737 回答