更换片段与另一片段显示两个片段
里面一个片段类我想,当用户点击一个按钮,另一个片段,以取代当前片段:更换片段与另一片段显示两个片段
@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>
此代码将删除当前片段的问题仍然在屏幕上,而新片段同时出现。也会帮助你管理后台。我在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;
}
添加标签和尝试...
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;
}
}
和我应该改变内部公共无效onClick(视图视图),调用replaceFragment两个参数? – mvasco
@mvasco尝试新的编辑答案.. –
@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();
}
谢谢,但结果相同。 – mvasco
您也可以检查相应片段的onCreateView,并检查是否已为其初始化相应的xml布局,而不是该类的相同xml文件。 –
@覆盖 公共查看onCreateView(LayoutInflater吹气,ViewGroup中容器, 捆绑savedInstanceState){// 充气布局对该片段 回报inflater.inflate(R.layout。class_layout_xml,container,false); } –
您添加了相同的片段?有什么问题 –
'fragment = new AboutFragment();'...你正在添加相同的片段双倍..check – rafsanahmad007
添加背景到片段的根布局 – Reena