我用导航抽屉创建了android应用程序,当我点击导航抽屉中的项目旧片段没有被替换时,我仍然可以在新片段下看到旧片段。如何解决这个问题?
private void displaySelectedScreen(int id)
{
Fragment fragment = null;
switch (id)
{
case R.id.home:
fragment = new Main();
break;
case R.id.settings:
Intent intent = new Intent(this, Settings.class);
startActivity(intent);
break;
case R.id.about:
Intent intent1 = new Intent(this, About.class);
startActivity(intent1);
break;
case R.id.share:
try
{
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_SUBJECT, "Audiophileradio");
String sAux = "\nLet me recommend you this application\n\n";
sAux = sAux + "https://play.google.com/store/apps/details?id=Orion.Soft \n\n";
i.putExtra(Intent.EXTRA_TEXT, sAux);
startActivity(Intent.createChooser(i, "choose one"));
}
catch(Exception e)
{
//e.toString();
}
break;
case R.id.send:
fragment = new Feedback();
break;
}
if (fragment != null)
{
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.contentFrame, fragment).commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item)
{
// Handle navigation view item clicks here.
int id = item.getItemId();
displaySelectedScreen(id);
return true;
}
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="audiophileradio.example.com.audiophileradio.MainActivity"
tools:showIn="@layout/app_bar_main"
android:background="@drawable/backg1">
<FrameLayout
android:id="@+id/contentFrame"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</FrameLayout>
<TextView
android:id="@+id/textView3"
android:layout_width="13dp"
android:layout_height="21dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="+"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.977"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.13"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"/>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="Title :"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.103"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.081"/>
<EditText
android:id="@+id/editText"
android:layout_width="218dp"
android:layout_height="35dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.593"
app:layout_constraintVertical_bias="0.084"
android:background="@drawable/border"/>
答案 0 :(得分:1)
您可以为片段或contentFrame添加背景:
<FrameLayout
android:id="@+id/contentFrame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000">
答案 1 :(得分:0)
您可以为片段添加标记
getSupportFragmentManager().beginTransaction()
.add(R.id.frame, new Main()(), "content_fragment")
.commit();
按标签找到它
getSupportFragmentManager().findFragmentByTag("content_fragment");
添加或删除
FragmentManager fragmentManager = getSupportFragmentManager();
Fragment fragment = fragmentManager.findFragmentByTag("content_fragment");
if(fragment == null)
{
// Create the detail fragment and add it to the activity using a fragment transaction.
mainFragment = new Main();
fragmentManager.beginTransaction()
.add(R.id.contentFrame, mainFragment,"content_fragment")
.commit();
}
else
{
// get old fragment
mainFragment = (Main)fragment;
}