[英]android getPageTitle is not worked
我正在使用FragmentPagerAdapter,並且getPageTitle()方法有問題。 一切都在工作,但是它。 請看一下我的代碼,並幫助我。
public class MyFragmentPageAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 3;
public final static String PAGE_CURRENT = "current_age";
public final static String PAGE_ID = "id";
int id = 0;
public MyFragmentPageAdapter(FragmentManager fm, int id) {
super(fm);
this.id = id;
}
@Override
public Fragment getItem(int position) {
MyFragment myFragment = new MyFragment();
Bundle bundle = new Bundle();
bundle.putInt(PAGE_CURRENT, position + 1);
bundle.putInt(PAGE_ID, id);
myFragment.setArguments(bundle);
return myFragment;
}
@Override
public CharSequence getPageTitle(int position) {
CharSequence title = null;
if (position == 0) {
title = "a";
} else if (position == 1) {
title = "b";
} else if (position == 2) {
title = "c";
}
return title;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return PAGE_COUNT;
}
}
在我的主要活動中,我宣布:
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setOnPageChangeListener(this);
FragmentManager fm = getSupportFragmentManager();
MyFragmentPageAdapter fragmentAdapter = new MyFragmentPageAdapter(fm,
id);
pager.setAdapter(fragmentAdapter);
//嘗試以下代碼
public class MyFragmentPageAdapter extends FragmentPagerAdapter {
protected static final String[] CONTENT = new String[] { "a", "b", "c"};
final int PAGE_COUNT = 3;
public final static String PAGE_CURRENT = "current_age";
public final static String PAGE_ID = "id";
int id = 0;
private int mCount = CONTENT.length;
// CHANGE STARTS HERE
private int current_position=0;
public void set_current_position(int i) {
current_position = i;
}
public MyFragmentPageAdapter(FragmentManager fm, int id) {
super(fm);
this.id = id;
}
@Override
public Fragment getItem(int position) {
MyFragment myFragment = new MyFragm`enter code here`ent();
Bundle bundle = new Bundle();
bundle.putInt(PAGE_CURRENT, position + 1);
bundle.putInt(PAGE_ID, id);
myFragment.setArguments(bundle);
return myFragment;
}
@Override
public CharSequence getPageTitle(int position) {
if (position == current_position-1) {
return "Previous";
} else if (position == current_position+1) {
return "Next";
}
// CHANGE ENDS HERE
return MyFragmentPageAdapter.CONTENT[position % CONTENT.length];
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return PAGE_COUNT;
}
}
我遇到了同樣的問題,並認為這是一個Android錯誤。 使用以下代碼在TabLayout.createTabAtPosition()中設置頁面標題: return newTab().setText("the page title");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.