L
L
Lordao2018-03-09 16:06:00
Java
Lordao, 2018-03-09 16:06:00

How to access fragments in MainActivity?

The application has three tabs. They are created from MainActivity using ViewPager and ViewPageAdapter. Each tab has a recyclerview. When clicking on the tab title, the specific recyclerview scrolls to the top. To do this, I want to put a listener for the menu in MainActivity.
How to access fragment elements from the main activity through findViewById?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2018-03-09
@zagayevskiy

In general, of course, you can do this, but it is not necessary. A fragment must completely encapsulate the behavior of everything it has inside. The activity doesn't need to know what's inside the fragment. A fragment should not know anything about other fragments.
The standard decision such - becomes the interface with the necessary methods. The fragment implements this interface. Activity pulls methods. Activity can also implement similar interfaces and the fragment can then pull these methods.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question