When changing the fragment, the former is visible under the new

1

I'm using fragments instead of activity, but when I call the second fragment with the replace method, it does not completely overlap the previous fragment, it overlaps, but the drawing of the other fragment keeps popping up, how do I this does not happen

First Screen xml code

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:id="@+id/frame_layout"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="br.com.demmy.demet.gerador.MainActivity"
tools:showIn="@layout/app_bar_main">

<fragment
    android:id="@+id/primeiro_fragment"
    android:name="br.com.demmy.demet.gerador.PrimeiroFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</fragment>
</FrameLayout>

Primerfragment.java code

public class PrimeiroFragment extends Fragment {

View minha_view;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup       container, Bundle savedInstanceState) {
    minha_view = inflater.inflate(R.layout.tela_principal, container, false);
    return minha_view;
}

}

Home screen code.xml

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:id="@+id/frame_layout"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="br.com.demmy.demet.gerador.MainActivity"
tools:showIn="@layout/app_bar_main">

<LinearLayout
    android:id="@+id/linear_layout"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">

    <TextView
        android:id="@+id/textView_hora"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        android:textSize="18sp"
        android:text="texto1" />

    <EditText
        android:id="@+id/editText_hora"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/oval"
        android:padding="8dp"
        android:layout_margin="8dp"
        android:ems="4"
        android:inputType="number" />

    <TextView
        android:id="@+id/textView3"
        android:layout_gravity="center"
        android:textSize="18sp"
        android:layout_marginTop="3dp"
        android:layout_marginBottom="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Texto 2" />

    <EditText
        android:id="@+id/texto_notification"
        android:padding="14dp"
        android:gravity="start"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_margin="8dp"
        android:layout_weight="1"
        android:background="@drawable/oval"
        android:inputType="textMultiLine" />
    <Button
        android:id="@+id/button"
        style="@style/estilo_botao"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:text="Cadastrar" />
</LinearLayout>
</FrameLayout>

SecondFragment.java Code

public class SegundoFragment extends Fragment {

View minha_view;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    minha_view = inflater.inflate(R.layout.segundo_layout, container, false);
    return minha_view;
}

}

second_layout.xml code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="138dp"
    android:layout_marginStart="138dp"
    android:layout_marginTop="196dp"
    android:text="SEGUNDO LAYOUT" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignTop="@+id/textView2"
    android:layout_marginLeft="68dp"
    android:layout_marginStart="68dp"
    android:text="Button" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignEnd="@+id/textView2"
    android:layout_alignRight="@+id/textView2"
    android:layout_below="@+id/button2"
    android:layout_marginTop="16dp"
    android:text="Button" />

<Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button3"
    android:layout_alignParentTop="true"
    android:layout_alignStart="@+id/button3"
    android:layout_marginLeft="17dp"
    android:layout_marginStart="17dp"
    android:layout_marginTop="60dp"
    android:text="Button" />

<Button
    android:id="@+id/button5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/textView2"
    android:text="Button" />
</RelativeLayout>

code that is calling fragments

lista_drawer.setOnItemClickListener(new AdapterView.OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            android.app.FragmentManager fragmentManager = getFragmentManager();
            if (position == 0){
                fragmentManager.beginTransaction().replace(R.id.frame_layout, new PrimeiroFragment()).commit();
            }else if(position == 1){
                fragmentManager.beginTransaction().replace(R.id.frame_layout, new SegundoFragment()).commit();
            }else if(position == 2){
                fragmentManager.beginTransaction().replace(R.id.frame_layout, new TerceiroFragment()).commit();
            }
        }
    });

    
asked by anonymous 23.03.2017 / 19:12

2 answers

1

I think the problem is to be mixing up statically added fragments with dynamically added. That block <fragment/> should not be in the "xml code of the first screen".

XML to be used by activity (replaces "xml from the first screen").

activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="br.com.demmy.demet.gerador.MainActivity"
tools:showIn="@layout/app_bar_main">

    <FrameLayout
        android:id="@+id/frame_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>
</LinearLayout>

onCreate() of activity:

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);

    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.frame_layout, new PrimeiroFragment());
    ft.commit();

    -----
    -----
}
    
23.03.2017 / 22:48
0

This happens because you just put a new fragment instead of replacing it. The best method is always to assign a fragment to a certain layout and then always always substitute it in the same layout .

You can do this as follows:

FragmentManager manager = getSupportFragmentManager();
                    fragment_name fragment_name = new fragment_name();
                    manager.beginTransaction().replace(R.id.layoutasubstituir, fragment_name, fragment_name.getTag()).commit();
    
24.03.2017 / 15:04