Android: direct scrolling / displaying a specific page in ViewPager when a button is clicked

I am trying to display a specific ViewPager page when a button is clicked. It works, but scrolling to this particular page is not smooth, that is, suppose I am not on the page. 1, but if I want to go to page no. 5, other pages 2,3,4 also scroll. I just want to scroll this page. How to do it. I used the following code, please see:

Below is the FragmentActivity code:

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 the code is a snippet:

 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.

+4
source share
3 answers

I had an application where there were more than 100 pages, and an index page that leads to a specific page number. On the Index page, if I tried to go to 20 pages, I see a blinking on the screen, which goes from 1 -19 to 20.

I used below if I want to go to page 5

 viewPager.setCurrentItem(4,false); viewPager.setCurrentItem(5); 

Because of what, scrolling stopped and went to a specific page without showing other pages and without blinking on the screen.

+6
source

As I know, you cannot just scroll the ViewPager to a specific page without scrolling through other pages.

If you want to use a custom transition to change fragments, you should use FragmentTransaction.setCustomAnimations(int i, int i1, int i2, int i3) :

 FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); Fragment newFragment = Fragment.newInstance(); ft.setCustomAnimations(R.anim.fragment_slide_left_enter, R.anim.fragment_slide_left_exit, R.anim.fragment_slide_right_enter, R.anim.fragment_slide_right_exit); ft.replace(R.id.main_fragment_container, newFragment); ft.addToBackStack(null); ft.commit(); 

Fragment enter:

fragment_slide_left_enter.xml

 <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@anim/decelerate_quint"> <translate android:fromXDelta="33%" android:toXDelta="0%p" android:duration="@android:integer/config_mediumAnimTime"/> <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="@android:integer/config_mediumAnimTime" /> </set> 

Fragment Output:

fragment_slide_left_exit.xml

 <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@anim/decelerate_quint"> <translate android:fromXDelta="0%" android:toXDelta="-33%p" android:duration="@android:integer/config_mediumAnimTime"/> <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="@android:integer/config_mediumAnimTime" /> </set> 
0
source

use ( manager.beginTransaction().hide(f).commitNow(); ) to hide other pages.

then after the animation is complete use ( manager.beginTransaction().show(f).commitNow(); )

my code in CustomPagerAdapter

 for(Fragment f : fragmentList) { if(!current.equals(f) && !newF.equals(f)) { manager.beginTransaction().hide(f).commitNow(); } } viewPager.setCurrentItem(index, byAnimation); 
0
source

Source: https://habr.com/ru/post/1498379/


All Articles