Первый фрагмент виден даже после уничтожения второго фрагмента - PullRequest
0 голосов
/ 17 октября 2018

Изначально у меня есть две кнопки в MainActivity.Кнопка 1 соответствует отображению первого фрагмента, а кнопка 2 соответствует отображению второго фрагмента.

Проблема заключается в том, что когда пользователь нажимает кнопку 2, второй фрагмент загружается, но первый фрагмент все еще виден.Как я могу разобраться в этом?

Вот код для button1 и button2.

if(number == 1) {
    //button 1 pressed
    BlankFragment fragment1 = new BlankFragment();
    Fragment fragment = new FragmentTwo();
    FragmentManager fragmentManager = getSupportFragmentManager();

    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.placeFragment, fragment1);
            fragmentTransaction.commit();
} 

//button2 is pressed
if(number==2) {
    FragmentTwo fragment2 = new FragmentTwo();
    FragmentManager fragmentManager = getSupportFragmentManager();

    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.placeFragment, fragment2);
    fragmentTransaction.commit();
} 

Фрагмент одного XML-файла

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".BlankFragment">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="8">
    <ImageView
        android:id="@+id/imagess"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:layout_weight="5"
        android:scaleType="fitXY" />

    <Button
        android:id="@+id/button"
        android:layout_weight="0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="123dp"
        android:layout_marginStart="128dp"
        android:text="Button" />
</LinearLayout>

</FrameLayout> 

Фрагмент двух XMLfile

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    tools:context=".FragmentTwo">

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


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="4"
            >
                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/itemText"
                    android:backgroundTint="@color/colorPrimary"
                    android:paddingTop="6dp"
                    android:paddingBottom="12dp"
                    android:layout_weight="2.5"
                    android:hint="@string/enter_a_item"/>
                <Button
                    android:layout_width="wrap_content"
                    android:layout_weight="1.5"
                    android:id="@+id/enterButton"
                    android:gravity="center"
                    android:text="Enter"
                    android:onClick="onEnter"
                    android:background="@drawable/round"
                    android:layout_height="wrap_content" />
        </LinearLayout>
        <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/listview"
            />
</LinearLayout>


</FrameLayout> 

Фрагмент моей активности.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:orientation="vertical"
    tools:context=".Main2Activity">

    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/placeFragment"
        android:name="com.example.akhilbatchu.makearecipe.BlankFragment"
        />
</RelativeLayout> 

Пожалуйста, помогите мне.

Ответы [ 2 ]

0 голосов
/ 17 октября 2018

Вам нужно раскрасить второй фрагмент фона следующим образом.Также добавьте кликабельное свойство

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical"
        android:background="#FFFFFF"
        android:clickable="true"
        android:focusable="true"
        tools:context=".FragmentTwo">
0 голосов
/ 17 октября 2018

Может быть, вы используете add вместо replace где-то в коде

или

Вы должны заменить fragment в вашем фрагменте Activity xml каким-то пустым контейнером (например,FrameLayout):

замените это:

<fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/placeFragment"
        android:name="com.example.akhilbatchu.makearecipe.BlankFragment"/>

на следующее:

<FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/placeFragment"/>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...