以编程方式更改布局 - android

时间:2015-06-29 11:57:31

标签: android android-layout android-fragments

我在我的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();

        }
    }

2 个答案:

答案 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)

也许这可以帮到你:

  1. 首先创建一个临时类并使用deafult声明一个整数变量位置为0
  2. 下一个导航抽屉代码中的每个if条件为临时类的位置变量赋予唯一的int值
  3. 现在在你的片段onCreateView方法中设置一个条件,首先检查是否有任何位置已经设​​置(如果没有设置,则意味着没有设置)(如果已设置)(从导航抽屉调用)
  4. 如果从导航栏中调用,请使用此

    View view = inflater.inflate(R.layout.nameoflayout,container, false);
    view.header.setVisibility(Visiblity.VISIBLE);
    
  5. 否则

    View view = inflater.inflate(R.layout.nameoflayout,container, false);
    view.header.setVisibility(Visiblity.GONE);
    

    希望它有所帮助!!

相关问题