2015-09-22 58 views
0

我在我的應用中使用了SlidingTabLayout(來自Google來源)在不同片段之間導航。 我可以在內容之間滑動很好,但是我面臨的問題是行爲欄的標題奇怪。SlidingTabLayout和Actionbar標題

假設我有兩個標籤有兩個頭銜(「第一名稱」和「第二名稱」):

| Action bar  | 

| First Title | Second Title | 

當我第一次進入包含SlidingTabLayout的片段,其在動作條的標題是這樣的:

| Firs...  | 

| First Title | Second Title | 

當我刷卡(到例如第二個選項卡),所述動作條的標題變爲:

| Second Title  | 

| First Title | Second Title | 

並保持這樣的狀態。當我滑動至少一次時,似乎操作欄將加載最後一個片段的標題

我想是這樣的:

我想在動作條永遠不會改變,無論在SlidingTabLayout標題就是顯示一個「主標題」。

下面是我的代碼的一些部分:

**含有SlidingTabLayout片段:

private String mainTitle; 
.... 

@Override 
public void onCreate(Bundle savedInstance) { 
    super.onCreate(savedInstance); 

    // The mainTitle is given to this fragment by another fragment 
    Bundle args = getArguments(); 
    if (args != null) { 
     mainTitle = args.getString("TITLE"); 
    } 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.layout, container, false); 
    mViewPager = (ViewPager) rootView.findViewById(R.id.viewpager); 
    mSlidingLayout = (SlidingTabLayout) rootView.findViewById(R.id.sliding_layout); 
    List<Fragment> listFragments = new ArrayList<>(); 
    List<String> listTitles = new ArrayList<>(); 
    for (int i = 0; i < mSize; i++) { 
     Bundle bundle = new Bundle(); 
     .... 
     listFragments.add(Fragment.instanciate(getActivity(), CustomFragment.class.getName(), bundle)); 
     listTitles.add("..."); 
    } 
    mViewPager.setAdapter(new PagerAdapter(getChildFragmentManager(), listFragments, listTitles)); 

    mSlidingLayout.setDistributedEvenly(true); 
    mSlidingLayout.setViewPager(mViewPager); 

    return rootView; 
} 

@Override 
public void onResume() { 
    super.onResume(); 
    // I TRY TO SET THE TITLE HERE !! 
    getActivity().getSupportActionBar().setTitle(mainTitle); 
} 

**適配器:

class PagerAdapter extends FragmentPagerAdapter { 
    List<Fragment> fragments; 
    List<String> titles; 
    ... 
    @Override 
    public Fragment getItem(int position) { 
     Fragment fragment = fragments.get(position); 
     return fragment; 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     // maybe the problem is in this method. 
     // this method is supposed to provide the titles for the tab 
     // but maybe it's also changing the actionbar title 
     return titles.get(position); 
} 

我設置的標題每次我輸入一個片段時,在onResume方法中的ActionBar。

謝謝!

編輯1:

我嘗試使用新SlidingTabLayout我已經從谷歌I/O了,結果還是一樣!

看來,ActionBar中的標題起初是一個提示,然後當我滑動到另一個片段時,它會變爲最後一個加載的片段。

就像是加載片段,每次加載片段時,ActionBar中的標題都會被該片段的標題覆蓋。

編輯2:

我改變了我的代碼發佈的最新版本,它(我使用現在SlidingTabLayout),並顯示出我是如何得到我的MAINTITLE。

+0

會發生什麼如果您縮短了您的第一個標題? – Fadils

+0

同樣的事情,我不認爲這是標題長度的問題,因爲當我滑動到另一個片段時它顯示「Second Title」。 –

+0

你爲什麼不在活動的onCreate中設置標題? – NasaGeek

回答

0

找到了!

這是非常愚蠢的我(這解釋了爲什麼其他人沒有這個問題)

我還設置標題在每個片(甚至是那些包含在標籤),所以當我刷卡,這些碎片上的onResume被調用,它改變了ActionBar中的標題...

謝謝大家的幫助,我很感激它!

1

您可以使用下面的代碼它工作正常,我

public class MatchesActivity extends AppCompatActivity implements ActionBar.TabListener { 

SectionsPagerAdapter mSectionsPagerAdapter; 
ViewPager mViewPager; 
FloatingActionButton skipButton; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_matches); 
    final ActionBar actionBar = getSupportActionBar(); 
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 
    actionBar.setDisplayHomeAsUpEnabled(true); 
    actionBar.setDisplayShowHomeEnabled(true); 
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 
    // Set up the ViewPager with the sections adapter. 
    mViewPager = (ViewPager) findViewById(R.id.pager); 
    skipButton = (FloatingActionButton) findViewById(R.id.skip_next); 
    skipButton.setVisibility(View.GONE); 
    mViewPager.setAdapter(mSectionsPagerAdapter); 
    mViewPager.setOffscreenPageLimit(1); 
    mViewPager.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View view, MotionEvent motionEvent) { 
      return false; 
     } 
    }); 
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { 
     @Override 
     public void onPageSelected(int position) { 
      actionBar.setSelectedNavigationItem(position); 
     } 
    }); 

    actionBar.addTab(
      actionBar.newTab().setText("MATCHES") 
        .setTabListener(this)); 
    actionBar.addTab(
      actionBar.newTab().setText("PINS") 
        .setTabListener(this)); 
    actionBar.addTab(
      actionBar.newTab().setText("CHATS") 
        .setTabListener(this)); 
} 

@Override 
public void onBackPressed() { 
    startActivity(new Intent(MatchesActivity.this, MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK & Intent.FLAG_ACTIVITY_CLEAR_TOP)); 
    MatchesActivity.this.finish(); 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    int id = item.getItemId(); 
    if (id == android.R.id.home) { 
     onBackPressed(); 
    } 
    return super.onOptionsItemSelected(item); 
} 

@Override 
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 
    mViewPager.setCurrentItem(tab.getPosition()); 
} 

@Override 
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 
} 

@Override 
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 
} 

public class SectionsPagerAdapter extends FragmentStatePagerAdapter { 

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

    @Override 
    public Fragment getItem(int position) { 
     switch (position) { 
      case 0: 
       return new MatchesFragment(); 
      case 1: 
       return new PinsFragment(); 
      case 2: 
       return new ConversationFragment(); 
      default: 
       return new MatchesFragment(); 
     } 
    } 

    @Override 
    public int getCount() { 
     return 3; 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     Locale l = Locale.getDefault(); 
     switch (position) { 
      case 0: 
       return getString(R.string.title_section1).toUpperCase(l); 
      case 1: 
       return getString(R.string.title_section2).toUpperCase(l); 
      case 2: 
       return getString(R.string.title_section3).toUpperCase(l); 
     } 
     return null; 
    } 
} 

}

+0

我在片段(而不是活動)中使用'PagerAdapter'和'SlidingTabLayout'。這將是一個有點複雜,改變這一切... –

1

我張貼在這裏mine.It爲me.Make工作守則要求它的變化和嘗試它。我使用了PagerSlidingTabStrip庫。

public class DealFragment extends Fragment { 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View view= inflater.inflate(R.layout.fragment_deal, container, false); 
    ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager); 
    viewPager.setAdapter(new SampleFragmentPagerAdapter(getChildFragmentManager())); 
    PagerSlidingTabStrip tabsStrip = (PagerSlidingTabStrip)view.findViewById(R.id.tabs); 
    tabsStrip.setBackgroundColor(Color.parseColor("#333333")); 
    // Attach the view pager to the tab strip 
    tabsStrip.setViewPager(viewPager); 
    return view; 
} 
public class SampleFragmentPagerAdapter extends FragmentPagerAdapter { 
    final int PAGE_COUNT = 2; 
    private final String tabTitles[] = new String[] { "Today's Deals", "Deals Close By" }; 

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

    @Override 
    public int getCount() { 
     return PAGE_COUNT; 
    } 

    @Override 
    public android.support.v4.app.Fragment getItem(int position) { 
     if(position==0) 
     { 
      return new TodaysDeal(); 
     } 
     else 
     { 
      return new DealsCloseBy(); 
     } 

    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     // Generate title based on item position 
     return tabTitles[position]; 
    } 
} 

}

希望它可以幫助你。

+0

感謝Animesh,但我沒有看到你設置的ActionBar的標題。我只看到你在哪裏設置標籤的標題.. –

+0

這不會作爲一個ActionBar出現,它會出現在操作欄下面作爲tabView,ActionBar或ToolBar,並不重要。並且對於ActionBar名稱,您無需在此處指定它。它基於您在應用程序的manifest.xml中提及的Activity名稱。 –

+0

好吧,實際上問題是** ActionBar **中的標題。在我的應用程序中,ActionBar的標題設置在Fragments的'onResume'方法中。所以每次我們輸入一個新的片段都會改變標題,而不是在清單中設置。當我使用帶有標籤的片段時,這個標題發生了奇怪的變化...... –

1

actionBar標題不會更改,因爲onResume()未被稱爲您向後滑動的時間。

這是因爲ViewPager第二次不會調用片段的onResume。儘管實際上片段被恢復。

您可以做的是將您父級片段(包含pagertabstrip)中的操作欄標題設置改爲onCreateView

供您參考:

**包含PagerTabStrip片段:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.layout, container, false); 
    mViewPager = (ViewPager) rootView.findViewById(R.id.viewpager); 

    List<Fragment> listFragments = new ArrayList<>(); 
    List<String> listTitles = new ArrayList<>(); 

    ... 

    // HERE's what you want 
    final ActionBar actionBar = getActivity().getSupportActionBar(); 
    if (actionBar != null) { 
     actionBar.setTitle(R.string.main_title); 
    } 

    mViewPager.setAdapter(new PagerAdapter(getChildFragmentManager(), listFragments)); 

    return rootView; 
} 
+0

感謝Fadils,但它仍然無法工作...... **似乎操作欄的標題被覆蓋在最後一個Fragment加載在PagerAdapter上** –

+0

你如何獲得你的主題?你是否像上面那樣設置了字符串資源的標題? – Fadils

+0

標題作爲「字符串」對象從另一片段提供給片段。所以,我不使用資源ID,因爲主標題可以更改 –