问题
I am using a FragmentStatePagerAdapter in my app. this is my adapter:
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
List<FeedItem> posts = FeedListFragment.getPosts();
public ScreenSlidePagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
@Override
public Fragment getItem(int position) {
FeedItem post = posts.get(position);
String url = post.getUrl();
String title = post.getTitle();
String imagUrl = null;
if (post.getType() == FeedItem.ARTICLE_VIEW_TYPE) {
imagUrl = posts.get(position).getArticle().getImageUrl();
}
ScreenSlidePageFragment fragment = new ScreenSlidePageFragment(url, title, imagUrl);
fragment.setPostsFragmentOnItemClickListener(PostActivity.this);
return fragment;
}
@Override
public int getCount() {
return posts.size();
}
}
What I need is to jump to a specific position in this adapter and also the ability to jump to the next and prev view. How can I do that? Thanks!
回答1:
you can use viewPagerInstance.setCurrentItem(int position)
. Remember the position it is 0 based (you can use values between 0
and adapter.getCount() - 1
).
For the next and previuos item use :
int currentItem = pager.getCurrentItem();
and add +/- 1
to currentItem
. Of course you have to check if currentItem+1
is not beyond adapter.getCount()
and if currentItem-1
is greather than or equal to 0
Edit: I used mPager.arrowScroll(View.FOCUS_LEFT);
It works great.
来源:https://stackoverflow.com/questions/17807555/jump-to-the-next-fragment-on-any-fragment-in-fragmentstatepageradapter