用另一个片段替换片段显示两个片段

时间:2017-04-17 07:18:12

标签: android android-fragments

在片段类中,当用户点击按钮时,我想用另一个片段替换当前片段:

 @Override
    public void onClick(View view) {
        Fragment fragment = null;
        switch (view.getId()) {
            case R.id.buttoncambiarfoto:
                fragment = new AboutFragment();
                replaceFragment(fragment);
                break;

            case R.id.buttoncambiarusername:
                fragment = new AboutFragment();
                replaceFragment(fragment);
                break;
        }
    }
    public void replaceFragment(Fragment someFragment) {

        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.perfilId, someFragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }

它有效,但当前片段仍在屏幕上,新片段同时出现。

我做错了什么?

已编辑,当前片段布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/perfilId"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.juarezserver.mergeapp.fragment.PerfilFragment">

    <!-- TODO: Update blank fragment layout -->

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">


            <ImageView
                android:id="@+id/fotoPerfil"
                android:layout_width="match_parent"
                android:layout_height="180dp"
                android:scaleType="fitCenter"
                app:srcCompat="@drawable/ic_person_pin_black_24dp" />

            <Button
                android:id="@+id/buttoncambiarfoto"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@color/btn_logut_bg"
                android:text="Editar Foto del Perfil" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"></LinearLayout>

            <TextView
                android:id="@+id/textView3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="20dp"
                android:text="Datos Personales"
                android:textAlignment="center"
                android:textColor="@color/bg_register"
                android:textSize="18sp"
                android:textStyle="bold" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="horizontal"
                android:padding="10dp">

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:singleLine="false"
                    android:text="Nombre de usuario"
                    android:textColor="@color/colorPrimaryDark"
                    android:textSize="12sp"
                    android:textStyle="italic" />

                <TextView
                    android:id="@+id/txtUsername"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Modesto" />

                <Button
                    android:id="@+id/buttoncambiarusername"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="@color/btn_logut_bg"
                    android:text="Cambiar nombre de usuario" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="horizontal"
                android:padding="10dp">

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Nombre"
                    android:textColor="@color/colorPrimaryDark"
                    android:textSize="12sp"
                    android:textStyle="italic" />

                <TextView
                    android:id="@+id/txtNombre"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Modesto" />

                <Button
                    android:id="@+id/button7"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="@color/btn_logut_bg"
                    android:text="Cambiar nombre" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="horizontal"
                android:padding="10dp">

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Apellidos"
                    android:textColor="@color/colorPrimaryDark"
                    android:textSize="12sp"
                    android:textStyle="italic" />

                <TextView
                    android:id="@+id/txtApellidos"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Modesto" />

                <Button
                    android:id="@+id/button3"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="@color/btn_logut_bg"
                    android:text="Cambiar apellidos" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="horizontal"
                android:padding="10dp">

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Email"
                    android:textColor="@color/colorPrimaryDark"
                    android:textSize="12sp"
                    android:textStyle="italic" />

                <TextView
                    android:id="@+id/txtEmail"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Modesto" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="horizontal"
                android:padding="10dp">

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Celular"
                    android:textColor="@color/colorPrimaryDark"
                    android:textSize="12sp"
                    android:textStyle="italic" />

                <TextView
                    android:id="@+id/txtCel"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Modesto" />

                <Button
                    android:id="@+id/button6"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="@color/btn_logut_bg"
                    android:text="Cambiar celular" />


            </LinearLayout>

        </LinearLayout>
    </ScrollView>

</FrameLayout>

4 个答案:

答案 0 :(得分:0)

删除:transaction.addToBackStack(null); 不要将相同的片段添加到backstack

答案 1 :(得分:0)

此代码将删除当前片段保留在屏幕上的问题,并且新片段同时出现。也将帮助您管理backstack。我在BottomNavigationView上使用此功能,您也可以使用NavigationView。我不知道它可以减少与否,但对我来说效果很好

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()){
        case R.id.nav_home:
            mFragment = getSupportFragmentManager().findFragmentByTag("Fragment_Home");
            if(!(mFragment!=null && mFragment.isVisible())){
                mFragmentManager.popBackStackImmediate("Fragment_Home", FragmentManager.POP_BACK_STACK_INCLUSIVE);
                mFragmentTransaction = mFragmentManager.beginTransaction();
                mFragmentTransaction.replace(R.id.container, new HomeFragment(), "Fragment_Home").addToBackStack("Fragment_Home").commit();
            }else if(mFragment!=null && !mFragment.isVisible()){
                mFragmentManager.popBackStackImmediate("Fragment_Home", FragmentManager.POP_BACK_STACK_INCLUSIVE);
                mFragmentTransaction = mFragmentManager.beginTransaction();
                mFragmentTransaction.replace(R.id.container, mFragment, "Fragment_Home").addToBackStack("Fragment_Home").commit();
            }
            return true;
        case R.id.nav_account:
            mFragment = getSupportFragmentManager().findFragmentByTag("Fragment_Account");
            if(!(mFragment != null && mFragment.isVisible())){
                mFragmentManager.popBackStackImmediate("Fragment_Account", FragmentManager.POP_BACK_STACK_INCLUSIVE);
                mFragmentTransaction = mFragmentManager.beginTransaction();
                mFragmentTransaction.replace(R.id.container, new AccountFragment(), "Fragment_Account").addToBackStack("Fragment_Account").commit();
            }else if(mFragment!=null && !mFragment.isVisible()){
                mFragmentManager.popBackStackImmediate("Fragment_Account", FragmentManager.POP_BACK_STACK_INCLUSIVE);
                mFragmentTransaction = mFragmentManager.beginTransaction();
                mFragmentTransaction.replace(R.id.container, mFragment, "Fragment_Account").addToBackStack("Fragment_Account").commit();
            }
            return true;
        case R.id.nav_category:
            mFragment = getSupportFragmentManager().findFragmentByTag("Fragment_Category");
            if(!(mFragment != null && mFragment.isVisible())){
                mFragmentManager.popBackStackImmediate("Fragment_Category", FragmentManager.POP_BACK_STACK_INCLUSIVE);
                mFragmentTransaction = mFragmentManager.beginTransaction();
                mFragmentTransaction.replace(R.id.container, new CategoryFragment(), "Fragment_Category").addToBackStack("Fragment_Category").commit();
            }else if(mFragment!=null && !mFragment.isVisible()){
                mFragmentManager.popBackStackImmediate("Fragment_Category", FragmentManager.POP_BACK_STACK_INCLUSIVE);
                mFragmentTransaction = mFragmentManager.beginTransaction();
                mFragmentTransaction.replace(R.id.container, mFragment, "Fragment_Category").addToBackStack("Fragment_Category").commit();
            }
            return true;
        case R.id.nav_services:
            mFragment = getSupportFragmentManager().findFragmentByTag("Fragment_Services");
            if(!(mFragment != null && mFragment.isVisible())){
                mFragmentManager.popBackStackImmediate("Fragment_Services", FragmentManager.POP_BACK_STACK_INCLUSIVE);
                mFragmentTransaction = mFragmentManager.beginTransaction();
                mFragmentTransaction.replace(R.id.container, new ServicesFragment(), "Fragment_Services").addToBackStack("Fragment_Services").commit();
            }else if(mFragment!=null && !mFragment.isVisible()){
                mFragmentManager.popBackStackImmediate("Fragment_Services", FragmentManager.POP_BACK_STACK_INCLUSIVE);
                mFragmentTransaction = mFragmentManager.beginTransaction();
                mFragmentTransaction.replace(R.id.container, mFragment, "Fragment_Services").addToBackStack("Fragment_Services").commit();
            }
            return true;
    }
    return false;
}

答案 2 :(得分:0)

使用Fragment添加标签并尝试...

public void replaceFragment(Fragment someFragment, String fragmentTag) {

    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(R.id.perfilId, someFragment, fragmentTag);
    transaction.addToBackStack(null);
    transaction.commit();
}

并添加片段标签,如下所示: -

@Override
public void onClick(View view) {
    Fragment fragment = null;
    switch (view.getId()) {
        case R.id.buttoncambiarfoto:
            fragment = new AboutFragment();
            replaceFragment(fragment,"CAMBIAR_FOTO_FRAGMENT");
            break;

        case R.id.buttoncambiarusername:
            fragment = new AboutFragment();
            replaceFragment(fragment,"CAMBIAR_USERNAME_FRAGMENT");
            break;
    }
}

答案 3 :(得分:0)

@Override       
public void onClick(View view) {
Fragment fragment = null;
switch (view.getId()) {
case R.id.buttoncambiarfoto:                 
fragment = new "Change the fragment class constructor here with respect to the button click"
                    replaceFragment(fragment);
                    break;
                case R.id.buttoncambiarusername:
                    fragment = new "Change the fragment class constructor here with respect to the button click "

                    replaceFragment(fragment);

                    break;

            }

        }

        public void replaceFragment(Fragment someFragment) {


            FragmentTransaction transaction = getFragmentManager().beginTransaction();

            transaction.replace(R.id.perfilId, someFragment);

            transaction.addToBackStack(null);

            transaction.commit();

        }
相关问题