Jump to the next fragment on any fragment in FragmentStatePagerAdapter

流过昼夜 提交于 2019-12-11 01:35:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!