gpt4 book ai didi

android - TabPageIndicator 和 TitlePageIndicator 在实现上有什么区别?

转载 作者:搜寻专家 更新时间:2023-11-01 07:36:30 26 4
gpt4 key购买 nike

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

最佳答案

TabPageIndicator 和 TitlePageIndicator 有什么区别?

Try viewing that page and see if you find the answer.

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

标题指示符

Displays the title of the selected page in the center with the titles of the adjacent pages (if available) in a more subtle style.

标签指示器

Similar to the title indicator but displays as many titles as possible in scrolling and animated horizontal tabs.

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 时,您需要在您的 Adapter 中实现 TitleProvider

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 上找到

关于android - TabPageIndicator 和 TitlePageIndicator 在实现上有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10441631/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com