我的ViewPager包含许多片段。我在actionBar中显示片段的标题,我使用下面的代码来设置标题。因为ViewPager也启动了相邻的片段,我在操作栏中看到了下一个片段的标题。应该是正确的如何在viewpager的操作栏中设置标题?
private class MyPagerAdapter extends FragmentPagerAdapter {
private String[] titles = {
"Titleme", "Titletos", "Titleos", "Titles", "MeTitle5", "Title6",
"Title7", "Title8"
};
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
@Override
public android.support.v4.app.Fragment getItem(int pos) {
Bundle args = new Bundle();
switch (pos) {
case 0:
currentFragment = new HomeFragment();
getSupportActionBar().setTitle("HomeFragment");
break;
case 1:
currentFragment = new HomeFragment5();
getSupportActionBar().setTitle("HomeFragment5");
break;
case 2:
currentFragment = new HomeFragment4();
getSupportActionBar().setTitle("HomeFragment4");
break;
case 3:
currentFragment = new HomeFragment3();
getSupportActionBar().setTitle("HomeFragment3");
break;
case 4:
currentFragment = new HomeFragment4();
getSupportActionBar().setTitle("HomeFragment4");
break;
case 5:
currentFragment = new ChlHome();
getSupportActionBar().setTitle("ChlHome");
break;
case 6:
currentFragment = new GiFragment();
getSupportActionBar().setTitle("GiFragment");
break;
case 7:
currentFragment = new FlList();
getSupportActionBar().setTitle(" FlList");
break;
default:
}
return currentFragment;
}
@Override
public int getCount() {
return 8;
}
}
根据shkschneider回复
进行了更新 final ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
tabs.setViewPager(pager);
pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
pager.setCurrentItem(position);
switch(position) {
case 0:
getSupportActionBar().setTitle("H");
break;
case 1:
getSupportActionBar().setTitle("P");
break;
case 2:
getSupportActionBar().setTitle("A");
break;
case 3:
getSupportActionBar().setTitle("V");
break;
case 4:
getSupportActionBar().setTitle("M");
break;
case 5:
getSupportActionBar().setTitle("C");
break;
case 6:
getSupportActionBar().setTitle("G");
break;
case 7:
getSupportActionBar().setTitle("F");
break;
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
private class MyPagerAdapter extends FragmentPagerAdapter {
private String[] titles = {
"Titleme", "Titletos", "Titleos", "Titles", "MeTitle5", "Title6",
"Title7", "Title8"
};
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
@Override
public android.support.v4.app.Fragment getItem(int pos) {
Bundle args = new Bundle();
switch (pos) {
case 0:
currentFragment = new HomeFragment();
break;
case 1:
currentFragment = new HomeFragment5();
break;
case 2:
currentFragment = new HomeFragment4();
break;
case 3:
currentFragment = new HomeFragment3();
break;
case 4:
currentFragment = new HomeFragment4();
break;
case 5:
currentFragment = new ChlHome();
break;
case 6:
currentFragment = new GiFragment();
break;
case 7:
currentFragment = new FlList();
break;
default:
}
return currentFragment;
}
@Override
public int getCount() {
return 8;
}
}
}
答案 0 :(得分:6)
试试这个......
final ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
tabs.setViewPager(pager);
//Use tabs for setting listener
tabs
.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
tabs.setNextFocusRightId(position);
actionBar.setTitle(titles[pos]);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int pos) {
// TODO Auto-generated method stub
}
});
答案 1 :(得分:3)
试试这段代码:
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int pos) {
actionBar.setSelectedNavigationItem(pos);
actionBar.setTitle(titles[pos]);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
答案 2 :(得分:1)
您可以创建标题字符串数组,然后在视图寻呼机上设置页面侦听器,并在onPageScrolled内部执行以下操作:
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
getSupportActionBar().setTitle(titles[position]);
}
答案 3 :(得分:0)
getItem()
时,都会调用 Fragment
,所以当然标题会显示下一个。
相反,您应该使用ViewPager.OnPageChangeListener:
@Override
public void onPageSelected(final int position) {
switch (position) {
case 0:
getSupportActionBar().setTitle("HomeFragment");
break ;
// ...
}
}
答案 4 :(得分:0)
从所有地方删除行
getSupportActionBar().setTitle("XYZ");
来自你的来源。
仅从getPageTitle
方法返回标题。