I am trying to display a particular page of a ViewPager on button click. It is working but scrolling to that particular page is not smooth i.e. suppose I am at page no. 1 but if I want to go to page no. 5, other pages 2,3,4 also scrolls. I just want to scroll to that particular page. How to do it. I have used the following code, please have a look:
Below code is the FragmentActivity:
public class ScreenSlideFragmentActivity extends FragmentActivity implements SlidingPageInterface{
private static final int NUM_PAGES = 6;
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_slide);
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
}
});
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(android.support.v4.app.FragmentManager fragmentManager) {
super(fragmentManager);
}
@Override
public android.support.v4.app.Fragment getItem(int position) {
return ScreenSlidePageFragment.create(position, NUM_PAGES, ScreenSlideFragmentActivity.this);
}
@Override
public int getCount() {
return NUM_PAGES;
}
}
@Override
public void notifyToSlideToRespectivePage(int pageNumber) {
mPager.setCurrentItem(pageNumber, true);
}
}
Below code is the Fragment:
public class ScreenSlidePageFragment extends android.support.v4.app.Fragment {
public static final String ARG_CURRENT_PAGE = "current_page";
public static final String ARG_TOTAL_PAGE_COUNT = "total_page_count";
private int mPageNumber;
private int mTotalPageCount;
private static ScreenSlideFragmentActivity mActivity;
public static ScreenSlidePageFragment create(int pageNumber, int totalNoPages, ScreenSlideFragmentActivity activity) {
Log.e("create", "create called");
ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
Bundle args = new Bundle();
args.putInt(ARG_CURRENT_PAGE, pageNumber);
args.putInt(ARG_TOTAL_PAGE_COUNT, totalNoPages);
fragment.setArguments(args);
mActivity = activity;
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPageNumber = getArguments().getInt(ARG_CURRENT_PAGE);
mTotalPageCount = getArguments().getInt(ARG_TOTAL_PAGE_COUNT);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater
.inflate(R.layout.fragment_screen_slide_page, container, false);
// Write Logic below to Dynamically Display Buttons as per "total_page_count"
return rootView;
}
public int getPageNumber() {
return mPageNumber;
}
public class HandleDotIVClickListener implements View.OnClickListener {
@Override
public void onClick(View view) {
int tag = (Integer) view.getTag();
mActivity.notifyToSlideToRespectivePage(tag);
}
}
}
Any ideas, how to achieve this.
I had an application where there was more than 100 pages, and an index page which leads to particular page number. From Index page if i tried to go to 20 page, i can see a blink in the screen which goes from 1 -19 then to 20.
I used the below if i want to go to 5th page
viewPager.setCurrentItem(4,false);
viewPager.setCurrentItem(5);
Which made the scroll stopped and made to go to particular page without showing the other pages and no blink in the screen.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With