Search code examples
androidandroid-fragmentsfragmentstatepageradapter

(Swipe Views) how to make Fragment objects dynamically for ViewPager at runtime?


The idea: In my app, it has 15 lessons, each lesson containing 30 couplets a user can swipe through. And each fragment/couplet_page has inflated a layout that contains 3 text views. I'm using FragmentStatePagerAdapter.

I've learned that use switch case statements to create swipe views but the problem is, there are lots of fragments to be created and I know that there is a better solution for it that I don't know. I just wanna use one fragment and somehow change the text with setText method.

here's my one fragment object code

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class CoupletOneFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.couplet_one, container, false);
    }
}

and here's the custom pager adapter

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;


public class MyFragmentAdapter extends FragmentStatePagerAdapter {

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

    @Override
    public Fragment getItem(int position) {
       switch (position) {
           case 0:
               return new CoupletOneFragment();
           case 1:
               return new CoupletTwoFragment();
           case 2:
               return new CoupletThreeFragment();
           case 3:
               return new CoupletFourFragment();
           case 4:
               return new CoupletFiveFragment();
and so on...
           default:
               break;
       }
       return null;
    }

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

Solution

  • Well I'm gonna answer my own question here

    Updated fragment object 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;
    }
    

    Updated 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);
        }
    }
    

    It was easy peasy!