我在我的navigationdrawer
应用程序中使用了滑动标签和android
。在滑动标签中,我为每个标签分配了4 Fragments
。在navigationdrawer
中,我也可以选择提到的4 Fragments
和其他几个Fragments
。现在我的问题是,当我使用我的滑动标签转到Fragment
时,我的布局不需要标题栏,因为用户可以知道他所在的Fragment
因为选项卡名称可用。但是,如果用户使用Fragment
转到navigationdrawer
,则必须使用名称为Fragment
的标题栏。但是,我在标签和Fragment
中加载了相同的navigationdrawer
。如何从navigationdrawer
打开标题栏并从标签打开时隐藏标题栏。
代码:
public Fragment getItem(int position) {
if(position == 0) // if the position is 0 we are returning the First tab
{
TopTenFragment tab1 = new TopTenFragment();
return tab1;
}
else if(position == 1) // As we are having 2 tabs if the position is now 0 it must be 1 so we are returning second tab
{
SongsFragment tab2 = new SongsFragment();
return tab2;
}
else if(position == 2)
{
VideoFragment tab3 = new VideoFragment();
return tab3;
}
else if(position == 3)
{
RadioFragment tab4 = new RadioFragment();
return tab4;
}
else{
return null;
}
导航图代码
@Override
public void onNavigationDrawerItemSelected(int position) {
if (position == 0) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, new Dashboard()).commit();
}
if (position == 1) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, new ListenLiveFragment()).commit();
}
if (position == 2) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, new SongsFragment()).commit();
}
if (position == 3) {
fragmentManager.beginTransaction()
.replace(R.id.container, new VideoFragment()).commit();
}
}
答案 0 :(得分:1)
您可以通过bundle将参数传递给片段。
Bundle bundle = new Bundle();
boolean showTitle = true;
bundle.putBoolean("showTitle", showTitle);
fragment.setArguments(bundle);
然后,您可以分段访问此捆绑包并显示/隐藏标题。
boolean showTitle = this.getArguments().getBoolean("showTitle",false);
if(showTitle)
//Show Title
else
//Hide Title
答案 1 :(得分:0)
也许这可以帮到你:
如果从导航栏中调用,请使用此
View view = inflater.inflate(R.layout.nameoflayout,container, false);
view.header.setVisibility(Visiblity.VISIBLE);
否则
View view = inflater.inflate(R.layout.nameoflayout,container, false);
view.header.setVisibility(Visiblity.GONE);
希望它有所帮助!!