如何在选项卡更改时更改“活动”标题(SlidingTabLayout)



我已经为此搜索了大约三天的解决方案。我试了很多方法,但都做不好。(或者可能我在哪里做错了什么)。因此,我要做的是将"活动"标题更改为当前选项卡名称,每次更改选项卡时,活动标题也应该更改。

所以我有一个MainActivity,其中有3个选项卡和3个不同的选项卡片段。这里的主要活动:

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

试试这个:

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
                    }
                });

更改活动时。运行Firstlt onCreate方法。所以在你的onCreate中得到ActionBar并设置它的标题。

示例

 ActionBar actionBar = getActionBar();
 actionBar.setTitle("title");

注意:如果使用ActionBar的支持库,请将getActionBar()更改为getSupportActionBar()

也试试这个:

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));
        ...
    }
    ...
}

在您的活动中设置一个自定义操作栏,通过膨胀一些布局,如(创建后):

    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);

在您的自定义布局和片段的onresume方法中使用getactivity()获取其id,并设置您的标题和iit将正常工作。