How to select next tab from child fragment

雨燕双飞 提交于 2019-11-27 16:32:22

Just to make your UI more Interactive and good looking, I'm gonna share what I've implemented.

Just to access your viewPager from the fragment's java file, declare your viewPager as below in your parent activity.

public static ViewPager viewPager;

Then in the onClick method or onClickListener of a button in the fragment, you can just use this.

ParentActivity.viewPager.arrowScroll(View.FOCUS_RIGHT);

This will smooth scroll your viewpager to the Second Fragment, and yes, if you want to limit the user from scrolling directly you can disable the scrolling of viewPager, read here.

You can also shift the focus to left if user wants to go back to FirstFragment by just

ParentActivity.viewPager.arrowScroll(View.FOCUS_LEFT);

And yes, it's working as I first test before posting answers, silly me.

Mattia Gecchele

You should call

viewPager.setCurrentItem(fragmentIdex);

you should use setCurrentItem(int item)

Set the currently selected page. `

sample code

chkBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        viewPager.setCurrentItem(2);
    }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!