3

I have a View containing a list of items. When I click on a particular listitem, it shows the details of that particular object in a new activity (and a new fragment). That all works fine.

Now I wanted to add a function, that the user can swipe from selected in detail activity shown item to the next/previous item, so he doesn't have to go back to the list and choose another item. I tried using ViewPager and FragmentStatePagerAdapter and it works and really has nice effects.

My Activity.java:

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

        // Instantiate a ViewPager and a PagerAdapter.
        mPager = (ViewPager) findViewById(R.id.pager);
        mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
        mPager.setAdapter(mPagerAdapter);

    ...
    }

   private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
        public ScreenSlidePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
        // pos is the position of the object in the list    
            Fragment fragment = ExerciseFragment.newInstance(exercisesArray, pos+position);
            return fragment;
        }

        @Override
        public int getCount() {
            return exercisesArray.size();
        }
    }

My questions:

  1. Is ViewPager the right way for my intention? Ever time the user swipes to next item, a new fragment is instantiated. The list can contain about 100 items... so maybe 100 fragments are created? Can this be a problem?

  2. My implementation only works fine, if the first item is selected and swiped. For example: I have 5 items in a list and the first one is clicked. Now I can swipe to second item, to third item, back to second. BUT if the user clicks on third item of the list, I can't swipe left to second and first item! Anyone have an idea?

Thanks a lot!

Raspberry
  • 139
  • 1
  • 11
  • 4
    Please provide code snippets so it will be easier to understand and identify potential problems with your implementation. – Dmitri Timofti Dec 01 '16 at 18:35
  • I added some code of my detail activity. My problem is, that the pagerAdapter is called for clicked and next item (and on swipe for next and next...). I can't swipe to an item before the clicked one. – Raspberry Dec 02 '16 at 18:40
  • about the first question, you can limit that how many fragments gets instantiated for you upfront, like if you put a limit of 5, then 5 fragments will be instantiated,and when you will swipe through the 6th fragment, the first one will be removed from the stack. see [this](http://stackoverflow.com/questions/11650152/viewpager-offscreen-page-limit) – nobalG Dec 02 '16 at 18:41

1 Answers1

2

You dont need to load all the fragments upfront in your viewpager, for deciding the limit of the number of fragments you want to load upfront , use viewPager.setOffscreenPageLimit(yourDesiredIntegerValue); for more info, see this n


Note: You can't set this limit to zero, but even if you try to set it, viewpager will ignore it and will set this limit to 1(default).

And about the second question, keep your activity in the backstack, when your user will return back to that, the normal flow will follow.Its simple as that.

Community
  • 1
  • 1
nobalG
  • 4,544
  • 3
  • 34
  • 72
  • Thanks nobalG, I'm going to try setOffscreenPageLimit :-) About second question: I'm sorry, I'm new to android and don't understand your answer. If I have 5 items in my list and click on the third one, I can see it in detail view (getItem() is called with position 0) and I can swipe to the right to see the fourth item. But I can't swipe left to see the second item. Any idea how to do this? – Raspberry Dec 02 '16 at 18:59
  • By keeping activity in backstack,I mean that when your user clicks on an item in viewpager, Start a new activity and display the detailed informations there, when user will come back to the first activity(the one with the view pager) by pressing back, he will be able to move in either direction. – nobalG Dec 02 '16 at 19:06
  • Is there any possibility to support a swipe to the left to show previous item without going back to the activity with my list? That's what I'm trying to do – Raspberry Dec 02 '16 at 19:15
  • I got it... getItem() always started with position 0 and I added the position of the selected item. Instead I'm using mPager.setCurrentItem(pos) in onCreate() and "ExerciseFragment.newInstance(exercisesArray, position);" inside getItem(). Verry simple - thanks for your help! – Raspberry Dec 03 '16 at 16:29