How to create a Fragment by clicking a button?

Asked

Viewed 71 times

0

I have the following Fragment:

public class ListaBimestresFragment extends Fragment {

    public ListaBimestresFragment() {
        // Required empty public constructor
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

            View view =  inflater.inflate(R.layout.fragment_lista_bimestres, container, false);

        return view;
    }
}

I put him inside the OnClickListener, thus:

public class FormularioDisciplina extends AppCompatActivity {

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

        Button adicionarBimeste = findViewById(R.id.formulario_adicionar_bim);
        adicionarBimeste.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager fragmentManager = getSupportFragmentManager();
                FragmentTransaction tx = fragmentManager.beginTransaction();
                tx.replace(R.id.formulario_main, new ListaBimestresFragment());
                tx.commit();
            }
        });

    }
}

It turns out that by turning and clicking the button, it gives a warning:

I/Choreographer: Skipped 36 frames!
  The application may be doing too much work on its main thread.

How can I fix this?

XML main:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:layout_marginTop="10dp"
        android:text="Preencha os campos abaixo:"
        android:textColor="#000"/>

    <EditText
        android:id="@+id/formulario_materia"
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/rounded_edittext"
        android:ems="10"
        android:inputType="textPersonName"
        tools:layout_editor_absoluteX="16dp"
        tools:layout_editor_absoluteY="135dp"
        android:hint="Matéria"
        android:padding="10dp"/>

    <EditText
        android:id="@+id/formulario_professor"
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/rounded_edittext"
        android:ems="10"
        android:inputType="textPersonName"
        tools:layout_editor_absoluteX="16dp"
        tools:layout_editor_absoluteY="135dp"
        android:hint="Professor"
        android:padding="10dp"/>

    <EditText
        android:id="@+id/formulario_obs"
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:background="@drawable/rounded_edittext"
        android:ems="10"
        android:inputType="textPersonName"
        tools:layout_editor_absoluteX="16dp"
        tools:layout_editor_absoluteY="135dp"
        android:hint="Observações"
        android:padding="10dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Adicione seus bimestres"
        android:textSize="19sp"
        android:layout_marginTop="10dp"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="+"
        android:layout_marginTop="10dp"
        android:id="@+id/formulario_adicionar_bim"/>
</LinearLayout>
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/insert_point"/>

XML Fragment:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/adicionar_Layout"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">

<EditText
    android:id="@+id/formulario_nota1"
    android:layout_width="159dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:background="@drawable/rounded_edittext"
    android:ems="10"
    android:hint="Primeira nota"
    android:inputType="textPersonName"
    android:padding="10dp"
    tools:layout_editor_absoluteX="16dp"
    tools:layout_editor_absoluteY="135dp" />

<EditText
    android:id="@+id/formulario_nota2"
    android:layout_width="159dp"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_marginTop="10dp"
    android:layout_marginEnd="0dp"
    android:background="@drawable/rounded_edittext"
    android:ems="10"
    android:hint="Segunda nota"
    android:inputType="textPersonName"
    android:padding="10dp"
    tools:layout_editor_absoluteX="16dp"
    tools:layout_editor_absoluteY="135dp" />
   </RelativeLayout>
  • This message can appear when you have long processes and so the frames are ignored or because of the emulator, 36 frames is a small number, if it is more than 100, you would need to do something to avoid this. Take a look here: Processes and links Regarding creating a Fragment by clicking the button, you need to specify some error message that is happening. Also provide the xml of your main and your Fragment.

  • @Murillocomino added their XML to the question. Regarding the error message, nothing appears when I click, only the message I put in the question when opening the Intent. Thanks in advance for replying!

1 answer

0


In your XML Main, you put your FrameLayout under the Button and within the LinearLayout.

And instead of:

tx.replace(R.id.formulario_main, new ListaBimestresFragment());

Place:

tx.replace(R.id.insert_point, new ListaBimestresFragment());

That adds up to your Fragment, but your code is only creating a field, and every time you press the "+" button it replaces the Fragment previous for a new one, instead of creating a new field. If you want to add new fields I advise to read on this:

Dynamically Add and Remove Views in Android

Browser other questions tagged

You are not signed in. Login or sign up in order to post.