0

I am trying to implement an activity with a button. This button, when pressed, will take you to the first page of a ViewPager. Sounds simple enough, right?

I'm a bit confused as to how the ViewPager and the FragmentStatePagerAdapter (FSPA) work, however.

In the android documentation for FragmentStatePageradapter, there is a very useful example which has taught me a lot but has left me with a couple questions. From what I can gather, in the example:

public class FragmentStatePagerSupport extends Activity {
    static final int NUM_ITEMS = 10;

    MyAdapter mAdapter;

    ViewPager mPager;

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

        mAdapter = new MyAdapter(getFragmentManager());

        mPager = (ViewPager)findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);

        // Watch for button clicks.
        Button button = (Button)findViewById(R.id.goto_first);
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mPager.setCurrentItem(0);
            }
        });
        button = (Button)findViewById(R.id.goto_last);
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mPager.setCurrentItem(NUM_ITEMS-1);
            }
        });
    }

    public static class MyAdapter extends FragmentStatePagerAdapter {
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

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

        @Override
        public Fragment getItem(int position) {
            return ArrayListFragment.newInstance(position);
        }
    }

    public static class ArrayListFragment extends ListFragment {
        int mNum;

        /**
         * Create a new instance of CountingFragment, providing "num"
         * as an argument.
         */
        static ArrayListFragment newInstance(int num) {
            ArrayListFragment f = new ArrayListFragment();

            // Supply num input as an argument.
            Bundle args = new Bundle();
            args.putInt("num", num);
            f.setArguments(args);

            return f;
        }

        /**
         * When creating, retrieve this instance's number from its arguments.
         */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mNum = getArguments() != null ? getArguments().getInt("num") : 1;
        }

        /**
         * The Fragment's UI is just a simple text view showing its
         * instance number.
         */
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.fragment_pager_list, container, false);
            View tv = v.findViewById(R.id.text);
            ((TextView)tv).setText("Fragment #" + mNum);
            return v;
        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            setListAdapter(new ArrayAdapter<String>(getActivity(),
                    android.R.layout.simple_list_item_1, Cheeses.sCheeseStrings));
        }

        @Override
        public void onListItemClick(ListView l, View v, int position, long id) {
            Log.i("FragmentList", "Item clicked: " + id);
        }
    }
}

the FragmentStatePagerAdapter is created to dynamically create a ListFragment for each list item in Cheeses.sCheeseStrings. This wasn't shown in the example but this Cheeses.sCheeseStrings is just a string array with multiple cheese types.

My questions:

  1. How does the ViewPager actually change pages? Is this something that isn't shown?

From the example, I can see that the implemented buttons can take the user to the first or last page but what about everything else in between?

  1. For the FSPA, it is only required to define the getCount method and the getItem() method. How does the ViewPager actually make use of these methods?

  2. Pertaining to my app, I want to have a main activity with a viewpager which isn't active until you press a button which takes you to the first page. From here each page created will hold a different string. Is this method suitable for such a task?

Thank you for anyone willing to help :)

  • 1. and 2. see the ViewPager source but 2nd in short: VP: Dear adapter how many pages i would have? Adapter: call getCount() ... VP: ok I need current, current -1, current +1 (+1,-1 for showing transition) item Adapter: ok call getItem(pos) ... 3. then ViewPager is not a valid widget ... you should find kind of "Wizard" widget – Selvin Sep 06 '17 at 12:31
  • @Selvin Thank you for your response. Also, what do you mean ViewPager isn't a valid widget? – Nestor Cortes Sep 06 '17 at 16:44
  • I meant that viewpager is for showing views/fragments independent. If you wana second page depends on data from first page then plain ViewPager is wrong choice. – Selvin Sep 06 '17 at 18:20

0 Answers0