D
D
DarkByte20152018-08-19 21:14:33
Java
DarkByte2015, 2018-08-19 21:14:33

How to change fragments?

The form has a BottomNavigationView. I want that on pressing of one button there was one fragment, on pressing of another - another. How to do it? Read about fragments. I just know how to add them. But I do not understand exactly how to organize their change.
upd. Found this way:

supportFragmentManager.beginTransaction().replace(R.id.content, MyFragment()).commit()

But the problem is that the text starts to overlap. Those. I put for example on each fragment on TextView with a name of a fragment. At the beginning (when the application is opened), the first fragment is on the main layout, I press the button and it changes to the second fragment, but the contents of the first one, apparently, remain visible! It is not replaced, but somehow added. And because of this, the text begins to overlap. This is what it looks like:
dz12wT3L.png
I don't know what to do :(

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew_21, 2018-08-20
@Andrew_21

In the fragment markup file for the main container, add

<android.support.constraint.ConstraintLayout // ваш контейнер
...
android:background="@color/white"
android:clickable="true"
android:focusable="true" />

There are also methods
supportFragmentManager.beginTransaction().hide(R.id.content, MyFragment()).commit()
supportFragmentManager.beginTransaction().show(R.id.content, MyFragment()).commit()
may be for your tasks are better suited

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question