2017-05-03 69 views
0

我改變我的標題有問題。我可以改變標題使用片段,但每次我點擊或改變到另一個片段標題沒有改變到正確的標題。這裏是我的代碼:android studio改變標題使用片段

這是我的主要tabactivity

public class TabActivity extends AppCompatActivity { 


private SectionsPagerAdapter mSectionsPagerAdapter; 


private ViewPager mViewPager; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_tab); 

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    // Create the adapter that will return a fragment for each of the three 
    // primary sections of the activity. 
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 

    // Set up the ViewPager with the sections adapter. 
    mViewPager = (ViewPager) findViewById(R.id.container); 
    mViewPager.setAdapter(mSectionsPagerAdapter); 

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); 
    tabLayout.setupWithViewPager(mViewPager); 

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
        .setAction("Action", null).show(); 
     } 
    }); 

} 





@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_tab, 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); 
} 



/** 
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to 
* one of the sections/tabs/pages. 
*/ 
public class SectionsPagerAdapter extends FragmentPagerAdapter { 

    public SectionsPagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public Fragment getItem(int position) { 
     switch (position){ 
      case 0: 
       profile_menu tab1 = new profile_menu(); 

       return tab1; 
      case 1: 
       group_menu tab2 = new group_menu(); 
       return tab2; 
      case 2: 
       world_menu tab3 = new world_menu(); 
       return tab3; 
      case 3: 
       prize_menu tab4 = new prize_menu(); 
       return tab4; 

      default: 
       return null; 
     } 
    } 

    @Override 
    public int getCount() { 
     // Show 3 total pages. 
     return 4; 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     switch (position) { 
      case 0: 
       return "Profile "; 
      case 1: 
       return "Group "; 
      case 2: 
       return "World "; 
      case 3: 
       return "Prize"; 
     } 
     return null; 

    } 




} 

public void setActionBarTitle(String title){ 
    getSupportActionBar().setTitle(title); 
} 
} 

,這是我的片段

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.prize_menu, container, false); 
    getActivity().setTitle("Prize"); 
    return rootView; 
} 
+0

你有沒有看這, [請點擊這裏](http://stackoverflow.com/questions/15560904/setti ng-custom-actionbar-title-from-fragment) –

回答

0

我認爲你想更改標籤時更改標籤?

mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { 
    @Override 
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 

    } 

    @Override 
    public void onPageSelected(int position) { 
     switch (position) { 
      case 0: 
       getSupportActionBar().setTitle("Profile "); 
       break; 
      case 1: 
       getSupportActionBar().setTitle("Group"); 
       break; 
      case 2: 
       getSupportActionBar().setTitle("World"); 
       break; 
      case 3: 
       getSupportActionBar().setTitle("Prize"); 
       break; 
      default: 
       break; 
     } 
    } 

    @Override 
    public void onPageScrollStateChanged(int state) { 

    } 
}); 
+0

沒有工作我的標題卡住了獎它根本不想改變。我把它放在錯誤的地方嗎?我把裏面@Override 保護無效onCreate(Bundle savedInstanceState){ –

+0

我發現它需要休息;對於每種情況,謝謝 –

+0

@AymeiAymei yup..sorry我的錯誤..tq – ZeroOne

0

呼叫getActivity().setTitle("Prize");它在每個片段的onResume

+0

我以前試過,但它仍然是隨機的 –

0
if (getActivity().getActionBar() != null) { 
     getActivity().getActionBar().setTitle("YourTitle"); 
    } 

使用此在每個片段的onViewCreated()行將解決您的問題

+0

我不是爲什麼,但我的標題沒有改變,當我嘗試它 –

0

在片段 坐落在簡單的方式您的標題。將這種方法添加到您的公共類中。 在操作欄中添加帶後退箭頭的標題。

public void setPageTitle(String title) { 
     ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayShowCustomEnabled(false); 
     ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(Html.fromHtml("<font color='#ffffff'>"+title+ "</font>")); 
    } 

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstantState){ 
     view = inflater.inflate(R.layout.fragment_name,container,false); 
     setPageTitle("title"); 
     return view; 
    } 

如果你想獨自標題,在你的片段onCreateView單獨使用此線()。但是這段代碼不處理後退箭頭。

((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Title"); 
+0

它沒有工作仍然是隨機的。當我點擊組片段的標題更改爲配置文件,有時更改爲世界片段 –

+0

Im 100%肯定,即時通訊在我的所有項目中使用此代碼。您是否放置了最後一行((AppCompatActivity)getActivity())。getSupportActionBar()。setTitle(「Title」);在你的片段onCreateView? –

+0

是的,我也許它只是不符合我的我不知道爲什麼 –

0

快速的方法來做到: 聲明工具欄靜態和片段訪問:

MainActivity:

public static Toolbar mToolbar; 
在您的片段

mToolbar.setTitle("your title here"); 
+0

我不知道爲什麼,但我的應用程序崩潰 –