4

All of the ViewPager examples that I've found online seem to have a static number of pages that you can swipe through. My app is for my music blog, and I want the user to be able to click a post which opens my SinglePostActivity. It should show the post, then allow the user to swipe the views to be able to see the previous post and next post.

Obviously, I don't want to have to load all of the posts and add them to a ViewPager, but load the post that the user clicked, and the previous and next posts so the user will see them as they're swiping. Once a swipe to the next post is complete, it would have to load the post after that so the user can swipe to that one (and vice versa if they swipe to view the previous post).

Are there any Android examples of how to achieve something like this?

jas7457
  • 1,971
  • 5
  • 30
  • 47
  • Just make the pager adapter loop around the items, with count set to `Integer.MAX_VALUE`. – S.D. Feb 21 '14 at 04:32

2 Answers2

3

You have to create your own PostStatePagerAdapter class by extending FragmentStatePagerAdapter:

public PostStatePagerAdapter extends FragmentStatePagerAdapter {
    private List data = null;

    public PostStatePagerAdapter(FragmentManager fm, List data){
        super(fm);
        this.data = data;

    }

@Override
public Fragment getItem(int arg0) {
        fragmentdata = data.get(arg0)

       // create your new fragment that displays your music blog entry by using fragment data got from data list
       return <your fragment>;
    }


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

and in your activity class' onCreate()

        PostStatePagerAdapter adapter = new                PostStatePagerAdapter(getSupportFragmentManager(), dataList);

        ViewPager viewPager = (ViewPager) findViewById(R.id.postPager); // defined in layout xml
        viewPager.setAdapter(adapter);
           // pass your clicked item index to this activity and set it as view pager's current item
        viewPager.setCurrentItem(index);

Remember to extend this activity class from FragmentActivity

Hope this helps.

Sanjeev
  • 9,876
  • 2
  • 22
  • 33
  • Thanks everyone for the help. I'm sure with this I will be able to figure out how to implement it. I <3 stackoverflow :P – jas7457 Feb 21 '14 at 04:27
1

I would suggest you use a FragmentStatePagerAdapter with a ViewPager.

Android Developer - Lateral Navigation

You'll notice they say,

FragmentStatePagerAdapter - This is best for paging across a collection of objects for which the number of pages is undetermined. It destroys fragments as the user navigates to other pages, minimizing memory usage.

The implementation will vary, as I'm not sure what your data source looks like. In my last implementation, however, I simply constructed the PagerAdapter with an array of data and pushed new data in when necessary (i.e. I'd load 10 posts, and upon the user view the second to last post, load the next 10 or so, updating the array).

Example (rough example, off-hand):

public class PostPagerAdapter extends FragmentStatePagerAdapter {

    private ArrayList<Post> mPosts;
    private FetchPostAsyncTask mFetchPostAsyncTask;

    public PostPagerAdapter(FragmentManager fm, ArrayList<Post> posts) {
        super(fm);

        mPosts = posts;
        mFetchPostAsyncTask = new FetchPostAsyncTask(mPosts);
    }

    @Override
    public Fragment getItem(int i) {

        ImageFragment fragment = ImageFragment.newInstance(mPosts.get(i));

        //If we're almost at the last post, fetch some more
        if(i == getCount() - 2) {
            if(mFetchPostAsyncTask.getStatus() == AsyncTask.Status.FINISHED) {
                mFetchPostAsyncTask = new FetchPostAsyncTask(mPosts);
            }

            if(mFetchPostAsyncTask.getStatus() == AsyncTask.Status.PENDING) {
                mFetchPostAsyncTask.execute();
            }
        }

        return fragment;
    }

    @Override
    public int getCount() {
        return mPosts.size();
    }
}
Grant Amos
  • 2,256
  • 17
  • 11
  • 1
    I was actually looking at this, but was unsure how to update the array/catch swipes. Could you provide your `FragmentPagerAdapter` or `FragmentStatePagerAdapter` implementation for me? Thanks – jas7457 Feb 21 '14 at 04:16
  • I've edited my post with an off-hand implementation. Probably full of errors/typos but it should be alright. – Grant Amos Feb 21 '14 at 04:28