1

I want to have few activities and few layouts in my application. There should be 100 pages to read about science, nature and more. but I don't want to make 100 layouts. I want to add different pages to my view adapter as you would with a list. Use the same layout for all and just add new data to it (like a list). I want to create pages with same listview concept but different datas.

There is already a post about what I am talking about: Using viewpager in my application but I can't understand how to do it.. I did some attempts.. I put the android-support-v4.jar in libs.. I made the xml file:

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

android:orientation="vertical"

 >
<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="0px"
    android:layout_weight="1" >
</android.support.v4.view.ViewPager>

</LinearLayout>

Which is the next step?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38

2 Answers2

1

Have a look at this ViewPager example. Basically you need to create an adapter which will supply the pages that you need. If you have a lot of pages, I would recommend using a FragmentStatePagerAdapter as that's optimized for a large number of pages.

Better yet, look at this example on the Android docs.

Oleg Vaskevich
  • 12,444
  • 6
  • 63
  • 80
  • The first link helped me a lot.. i think this is waht i was looking for.. but i got a problem.. in that tutorial there is just one layout with the image file and from viewpagerexample.java there are 2 cases where that image is edited and changed with other image.. i would like to do the same thing but not with image but with text.. – Ronald Reagan Jan 11 '13 at 18:07
  • How can i use this system with text? @Override public Fragment getItem(int position) { switch (position) { case 0: return new DetailFragment(); case 1: return new ImageFragment(R.drawable.ic_launcher); case 2: return new ImageFragment(R.drawable.thumb); default: return null; } } } } i mean like case 3 new myclass(editthetext); – Ronald Reagan Jan 11 '13 at 18:10
  • You would just have to make your own subclasses of `Fragment`s that you put in and add to the `FragmentStatePagerAdapter` (see the second link's example). Then in `onCreateView()` you will be able to inflate the XML layout that you are trying to use (for example, a layout with a single `TextView` in it). – Oleg Vaskevich Jan 11 '13 at 22:25
0

Along with Oleg's answer I would add this simple tutorial which I found that has source code you can download and, once that is working for you, you can adapt it to your needs slowly as you learn how it works. This shows how to display a TextView which I believe is what you want instead of an Image. Once you get the basics down from one of the links you have been given, I think you may want to create an ArrayList (possibly in its own class) and get the data from there to add to each page's TextView due to the large number of pages that you plan on having. If you take your time and make sure you understand the basics of how these components work together then I think the rest will come easier for you.

I agree with Oleg's answer that you probably want to use a FragmentStatePagerAdapter because of the large amount of pages/data you will have. But give this tutorial a shot and see if that helps you make sense of how these all work together. Good Luck!

Update

As I haven't done this before I will give my best shot to show you something that might work. Without playing around with it myself, I can't say for sure but I would give this a try (using the code from the turorial and a made up class that returns an array)

 public Fragment getItem(int arg0) {
    MyFragment myFragment = new MyFragment();
    Bundle data = new Bundle();
    String myArrayString = new ArrayClassName().getText(arg0);  //This should get a string value from a method you create in your Array class called getText()
    data.putInt("current_page", arg0);
    myFragment.setArguments(data);
    return myFragment;  //this hopefully gets passed back to onCreateView()
}

Then use setText(mCurrentPage) as they did in the onCreateView() to set the page text. I may have missed something since I did all of this in my head but I would imagine something like this would get you started. Hopefully it works because that was a whole lot of work not using this before :D

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • Thank you guys.. In the link you give me codeMagic the 5 pages have got the same text what i need now is to set different texts for each page.. this is my problem.. for the other things like FragmentStatePagerAdapter I think i understand the general contest.. My questio is how can i set different texts? – Ronald Reagan Jan 11 '13 at 23:38
  • Which is why I suggested that particular tutorial ;) That's why I suggested some sort of `Array` and use the `position` that is sent to the adapter to get the next/previous index from the `Array`. I haven't done this but the logic makes sense to me. You could create a class that holds the `Array` and use the `position` to return a text string to add to the view – codeMagic Jan 11 '13 at 23:44
  • now i'm getting confused.. I just created the new class as you sad.. what next? can you give me the code? – Ronald Reagan Jan 11 '13 at 23:57
  • I got to make two other classes? One for array and another where to put this code right? And then i got to change or add something else.. – Ronald Reagan Jan 12 '13 at 09:43
  • I got to make two other classes? One for array and another where to put this code right? And then i got to change or add something else.. I cant follow you.. in really new to android programming.. so i would need a step by step tutorial or directly the complete file to learn how it works.. you are giving me a great help codeMagic i hope you understand.. can you tell me where to put the code you give me? and what to put in my class "ArrayTest"?? Thank you! – Ronald Reagan Jan 12 '13 at 09:50
  • @RonaldReagan I'm glad I could help. I don't have any code to give you. I'm going to start a small project soon using that to better understand it myself but that tutorial I posted a link to should be a big help using the code change that I posted. I was suggesting a class that holds an array just because you have so many pages unless you put them in a DB but I don't think you should tackle that yet. Just start with a couple pages for now and load them statically until you understand how the viewpager and fragments work. Just try some code then I can give you better help. Good luck! – codeMagic Jan 12 '13 at 21:15
  • I just meant for the "ArrayTest" class to hold the strings to display in the code above – codeMagic Jan 12 '13 at 21:16