【问题标题】:Hide Android Fragment隐藏 Android 片段
【发布时间】:2017-09-05 13:54:30
【问题描述】:

使用 Android Framgent,我有三个片段布局我想根据标志隐藏片段。当第一个片段可见时想要禁用第二个片段 是否能够成功隐藏选项卡布局的选项卡标题但无法删除 Page.How 来实现它。? 我已经搜索了很多,但没有找到我接受的。

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;
    }
}}

提前谢谢你。

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    由于您在意图中获得了标志,因此您不需要 tabLayout。只需初始化和片段即可。 一些代码如下:

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

    【讨论】:

    • 我想隐藏布局而不是添加
    • 在 onCreate 中,设置片段数组并将其大小用于 SectionsPagerAdapter 的 getCount()。然后不需要删除标签。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-27
    • 2015-04-02
    • 2012-12-30
    • 1970-01-01
    • 2019-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多