1

I've follow a tutorial for use ViewPAger widget and implement swiping view for navigate to my app. Every tutorial i've follow use justo one simple static xml file to show in each tab (i just modify a textview). I need to create a different view with different content and swipe it. How can i do?

Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
giozh
  • 9,868
  • 30
  • 102
  • 183

1 Answers1

2

I am not sure if I understand your question correctly, but I think this is what you are looking for:

How to implement a ViewPager with different Fragments / Layouts

The basic idea is that you have a different Fragment class for each different layout you have. In this case, all Fragments have a TextView, but their layouts also differ in a separate background color.

Community
  • 1
  • 1
Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
  • ok, it's what i'm looking for. But in myPagerClass, if implemented in that way, every time i swipe to another fragment, a new object of that class will be created (in newInstance method ? Or FragmentPageradapter superclass guarantee that each fragment it's created only once, then it's keep in memory? – giozh Sep 07 '13 at 08:53
  • 1
    You can have a look at the ViewPagers method setOffscreenPageLimit(int limit) - here you can specify how many pages will be kept in memory. Furthermore, you can modify your adapter and save instances of the Fragments there, if an instance is already created, you will not have to create a new one. – Philipp Jahoda Sep 07 '13 at 08:54
  • Ok, i just modify FragmentAdapter constructor for create instance of my Fragment then return it in getItem method – giozh Sep 07 '13 at 08:59
  • You would have a List of Fragments in your Adapter. Inside the getItem() method you would check if a Fragment is alread in the List. If so, you take the Fragment from the list, otherwise you create a new fragment and save it in the list. – Philipp Jahoda Sep 07 '13 at 09:33