TabPageIndicator和TitlePageIndicator之间的实现差异是什么?

时间:2012-05-04 01:44:14

标签: android tabs viewpagerindicator

我查看了示例,唯一的区别似乎是名称,但是当我将我的应用程序中的所有名称从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>

1 个答案:

答案 0 :(得分: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's Github repo for ViewPagerIndicator

上找到