我已经搜索了三天的解决方案。我尝试了很多方法,但我无法做到。 (或者我可能在哪里做错了)。 所以,我想要做的是将活动标题更改为当前标签名称,每次更改标签时,活动标题也应该更改。
所以我有一个MainActivity,我有3个Tabs和3个不同的片段。这里是MainActivity:
public class MainActivity extends ActionBarActivity {
private Toolbar toolbar;
private ViewPager mPager;
private SlidingTabLayout mTabs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(new CustomPagerAdapter(getSupportFragmentManager()));
mTabs = (SlidingTabLayout) findViewById(R.id.tabs);
mTabs.setDistributeEvenly(true);
mTabs.setViewPager(mPager);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
//Custom ViewPager for the tabs
class CustomPagerAdapter extends FragmentPagerAdapter {
String[] tabsTitles;
String vpPager;
public CustomPagerAdapter(FragmentManager fm) {
super(fm);
tabsTitles = getResources().getStringArray(R.array.tabTitles);
}
@Override
public Fragment getItem(int position) {
//if (position == 0){
// news_fragment news_fragment = new news_fragment();
// return news_fragment;
//}
if (position == 1){
login_fragment login_fragment = new login_fragment();
return login_fragment;
}
else if (position == 2){
about_fragment about_fragment = new about_fragment();
return about_fragment;
}
else {
news_fragment news_fragment = new news_fragment();
return news_fragment;
}
}
@Override
public CharSequence getPageTitle(int position) {
return tabsTitles[position];
}
@Override
public int getCount() {
return 3;
}
}
}
我正在使用此SlidingTabLayout
答案 0 :(得分:6)
试试这个:
viewpager
.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
actionBar.setTitle(tabsTitles[position]);
}
@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 :(得分:0)
更改活动时。 Firstlt onCreate
方法运行。因此,在您onCreate
获取ActionBar
并设置其标题。
示例强>
ActionBar actionBar = getActionBar();
actionBar.setTitle("title");
注意:如果您使用的是getActionBar()
支持库,请将getSupportActionBar()
更改为ActionBar
。
答案 2 :(得分:0)
尝试这个:
class CustomPagerAdapter extends FragmentPagerAdapter {
....
Toolbar toolbar;
public CustomPagerAdapter(FragmentManager fm, Toolbar toolbar) {
...
this.toolbar = toolbar;
}
@Override
public Fragment getItem(int position) {
toolbar.setTitle(getPageTitle(position));
...
}
...
}
答案 3 :(得分:0)
通过对某些布局进行充气来设置活动中的自定义操作栏,例如(After Oncreate):
android.app.ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#ffffff")));
bar.setDisplayShowHomeEnabled(false);
bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
bar.setCustomView(R.layout.homescreen_actionbar);
在自定义布局中使用TextView,在片段的onresume方法中使用getactivity()获取其id,并设置标题,iit将正常工作。