简体   繁体   English

显示/隐藏片段而不是替换

[英]Show/Hide Fragment instead of replace

I have 3 and more fragments and I want to access them with the drawer.我有 3 个或更多片段,我想用抽屉访问它们。 So that when I click on "Profile" the current fragment(ie Home) should hide and "Profile" fragment should show and vice versa.. Right now its working with "replace fragment" but, Instead of replace I want to show/hide them when I click on the drawer button.因此,当我单击“配置文件”时,当前片段(即主页)应该隐藏并且“配置文件”片段应该显示,反之亦然。现在它使用“替换片段”但是,而不是替换我想显示/隐藏当我点击抽屉按钮时。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //my coding
    

    replace(new HomeFragment());
    init();
}

private void init() {
    mDrawer = (FlowingDrawer) findViewById(R.id.drawerlayout);
    iv_Menu = findViewById(R.id.iv_Menu);
    ll_Home = findViewById(R.id.ll_Home);
    ll_Profile = findViewById(R.id.ll_Profile);
    ll_Setting = findViewById(R.id.ll_Setting);
    ll_Share = findViewById(R.id.ll_Share);
    ll_Logout = findViewById(R.id.ll_Logout);

    iv_Menu.setOnClickListener(this);
    ll_Home.setOnClickListener(this);
    ll_Profile.setOnClickListener(this);
    ll_Setting.setOnClickListener(this);
    ll_Share.setOnClickListener(this);
    ll_Logout.setOnClickListener(this);


}

@Override
public void onClick(View view) {
    switch (view.getId()){
        case R.id.iv_Menu:
            mDrawer.openMenu(true);
            break;

        case R.id.ll_Home:
            replace(new HomeFragment(),"Home");
            mDrawer.closeMenu(true);
            break;

        case R.id.ll_Profile:
            replace(new ProfileFragment(),"Profile");
            mDrawer.closeMenu(true);
            break;

        case R.id.ll_Setting:
            startActivity(new Intent(this, SimplPreach.class));
            mDrawer.closeMenu(true);
            break;

        case R.id.ll_Share:
            Toast.makeText(this, "Share.", Toast.LENGTH_SHORT).show();
            mDrawer.closeMenu(true);
            break;

        case R.id.ll_Logout:
            Toast.makeText(this, "Logout.", Toast.LENGTH_SHORT).show();
            mDrawer.closeMenu(true);
            break;
    }
}

private void replace(Fragment fragment, String s) {
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.fl_Main,fragment);
    transaction.addToBackStack(s);
    transaction.commit();
}

private void replace(Fragment fragment) {
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.fl_Main,fragment);
    transaction.commit();
}

I have been searching from hours but nothing is working, How can I use show hide instead of replace.. Please help me..我一直在搜索几个小时但没有任何效果,我如何使用显示隐藏而不是替换..请帮助我..

FragmentTransaction has the following methods you need to use. FragmentTransaction具有您需要使用的以下方法。

show(Fragment fragment)
hide(Fragment fragment)
add(int containerViewId, Fragment fragment, String tag)

And I think you don't need to call transaction.addToBackStack(s) in your case.而且我认为您不需要在您的情况下调用transaction.addToBackStack(s)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM