gpt4 book ai didi

android - 在 ViewPager 中设置操作栏标题

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:52:57 24 4
gpt4 key购买 nike

我的 ViewPager 由许多 fragment 组成。我在 actionBar 中显示 fragment 的标题,我正在使用下面的代码来设置标题。由于 ViewPager 还启动相邻的 fragment ,我在 actionbar 中看到下一个 fragment 的标题。应该是正确的如何继续在 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;
}
}
}

最佳答案

试试这个……

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

}
});

关于android - 在 ViewPager 中设置操作栏标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29847194/

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