-1

New to android and recently learned swipe views a bit. In my app, it has 17 chapters and in each chapter, there will be 30 pages containing some text data, a user can swipe through. I need to use FragmentStatePagerAdapter to save memory but I need to know that do I need to make 510 Fragment objects, means 510 XML layout? I just need to change the text in each page, and I've seen many using switch statement like this

@Override
public Fragment getItem(int position) {
    switch (position) {
        case 0:
            return  new FragmentOne();
        case 1:
            return  new FragmentTwo();
        default:
            break;
    }
    return null;
}

Do I/Should I write 30 cases in each chapter? or is there a better way to do this? I've Googled and seen lots of Youtube videos but couldn't find the solution. I request everyone that if you're answering or commenting, do explain your codes cause I believe in learning, not in copy pasting.

Arpit Chhabra
  • 193
  • 1
  • 12
  • 1
    you can reuse the layout , provided the inside content is generic – Raut Darpan Apr 26 '17 at 05:34
  • 1
    you dont need to make 150 layouts add layout dynamically... – Raut Darpan Apr 26 '17 at 05:35
  • `I believe in learning` We do believe in **using Google to search for tutorials**. This is not a learning place, in a strict sense. – Phantômaxx Apr 26 '17 at 06:55
  • This is not a tutorial/learning place. I hope it's clear to you. You must show some effort, rather than insulting senior members. As I told you earlier, if you want tutorials, search them - by using Google. Enough said. – Phantômaxx Apr 26 '17 at 08:59

2 Answers2

0

Using FragmentStatePagerAdapter is right approach here. It will manage the fragment states based on the viepager offscreenLimit() you set.

In your case, you can have just one fragment and an arrayList of your text and given different text based on position.

Something like this..

List<String> yourList = new ArrayList<>();

@Override
public Fragment getItem(int position) {
    return new YourFragment(yourList.get(position));
}
Ritt
  • 3,181
  • 3
  • 22
  • 51
  • What if @Ritesh I need to translate the text data to support more than one language? Should I make an ArrayList of integers referring to Strings? – Arpit Chhabra Apr 26 '17 at 05:56
  • yeah, to support for more than one language, your list should refer strings. – Ritt Apr 26 '17 at 06:26
  • trying to make an ArrayList of string data type in FragmentStatePagerAdapter but it couldn't be made there, I guess you need a make it inside a method and the second thing, could you explain how to use the argument "yourlist.get(position)" inside yourFragment? – Arpit Chhabra Apr 26 '17 at 08:16
  • do reply cause I gotta know! – Arpit Chhabra Apr 26 '17 at 08:17
  • 30 in each chapter – Arpit Chhabra Apr 26 '17 at 08:56
  • okay, so if there are 17 chapters and each chapter has 30 different texts, are you going to create 17*30 texts. If this is the case, then please change your approach. – Ritt Apr 26 '17 at 09:23
  • Coming back to string, create a string array in xml file and use it in your java class, android will give you string array, not list. From there onwards you can use it as a list or string array, it's up to you. – Ritt Apr 26 '17 at 09:25
-1

It's easy peasy man!

You need only one Fragment object, here's the code

public class FragmentChild extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

    // INFLATE THE LAYOUT THAT EACH FRAGMENT OBJECT WILL HAVE, PUT IT IN A VIEW
     View root = inflater.inflate(R.layout.couplets, container, false);

    // RECEIVE THE BUNDLE DATA SENT (ARGUMENTS)
    Bundle args = getArguments();

    // CREATE AN ARRAY LIST OF STRINGS THAT WILL HOLD TEXT
    ArrayList<String> someText = new ArrayList<>();
someText.add("one");
someText.add("two");
someText.add("three");
TextView txt = (TextView) root.findViewById(R.id.text_view);
    txt.setText(someText.get(args.getInt("position")));
return root;
}

custom pager adapter

class MyFragmentAdapter extends FragmentStatePagerAdapter {

 MyFragmentAdapter(FragmentManager fm) {super(fm);}

@Override
public Fragment getItem(int position) {
    Bundle args = new Bundle();
    args.putInt("position", position);

    Fragment fragment = new FragmentChild();
    fragment.setArguments(args);
    return fragment;
}

@Override
public int getCount() {
    return 3;
}
}

And finally host activity

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

    // FIND THE VIEWPAGER AND SET THE CUSTOM ADAPTER TO IT TO PROVIDE CHILD PAGES
        ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        MyFragmentAdapter adapter = new MyFragmentAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);
    }
}
Ari
  • 97
  • 1
  • 1
  • 11