2015-09-22 54 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,因为主标题可以更改 –