隐藏Android片段

时间:2017-09-04 08:20:10

标签: android android-layout

使用Android Framgent,我有三个片段布局我想在flag的基础上隐藏片段。当第一个片段可见时想要禁用第二个片段 我成功地隐藏了标签布局的标题标题,但无法删除Page。如何实现它。 我已经搜索了很多,但是我没有接受。

public class Sample extends AppCompatActivity implements Commands.FragmentDataListener_Commands_epg{
Bundle dataBundle;
TabLayout tabLayout;
String flag;
private Sample.SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_details_customer);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    Intent intent=getIntent();
    flag = intent.getStringExtra("flag");

    mSectionsPagerAdapter = new Sample.SectionsPagerAdapter(getSupportFragmentManager());
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);
    if(flag.equals("1")) {
        tabLayout.removeTabAt(0);

    }
    else if(flag.equals("2"))
    {
        tabLayout.removeTabAt(1);
    }

    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();

}


@Override
public void onFragmentDataUpdated(Bundle dataBundle)
{
    this.dataBundle=dataBundle;
}

@Override
public void onStart() {
    super.onStart();

    client.connect();
    AppIndex.AppIndexApi.start(client, getIndexApiAction());
}

@Override
public void onStop() {
    super.onStop();

    AppIndex.AppIndexApi.end(client, getIndexApiAction());
    client.disconnect();
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                    //first page
            case 1:
                    //second page
        }
        return null;
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "First";
            case 1:
                return "second";
          }
        return null;
    }
}}

提前谢谢。

1 个答案:

答案 0 :(得分:0)

由于您在意图中获得了旗帜,因此您不需要tabLayout。只需初始化和片段到您的视图。 一些代码如下:

Fragment fragmentInput = new  FragmentAddVehicle();
FragmentManager  getSupportFragmentManager.beginTransaction().add(R.id.ll_root_container, fragmentInput).commit();