Why my ViewPager is so slow?

Why ViewPager is slow

  • I'm using ViewPager to swipe between multiple images, but swiping between images is sluggish and not smooth although i only loaded few images. Here's my full code MainActivity.java public class MainActivity extends ActionBarActivity implements ActionBar.TabListener { public static SectionsPagerAdapter mSectionsPagerAdapter; public static ViewPager mViewPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); mViewPager = (ViewPager) findViewById(R.id.pager); mViewPager.setAdapter(mSectionsPagerAdapter); } public class SectionsPagerAdapter extends FragmentPagerAdapter { public SectionsPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { Fragment fragment = null; Bundle args = new Bundle(); fragment = new Page(); args.putString(Page.ARG_SECTION_NUMBER, position); fragment.setArguments(args); return fragment; } public int getItemPosition(Object object) { Log.v("MBG", "getItemPosition"); return POSITION_NONE; } @Override public int getCount() { return 6; } } Page.java public class Page extends Fragment { public static final String ARG_SECTION_NUMBER = "section_number"; public Page() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.activity_page, container, false); String index = getArguments().getString(ARG_SECTION_NUMBER); ImageView callImage = (ImageView) rootView.findViewById(R.id.imageView); int resID = getActivity().getResources().getIdentifier(index, "drawable", getActivity().getPackageName()); callImage.setImageResource(resID); return rootView; } MainActivity.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/pager" android:layout_marginTop="0dp" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".Prayers" /> </RelativeLayout> Page.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.mahmoud.qaloun.Page"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/imageView" android:src="@drawable/a1" android:scaleType="fitXY" /> </RelativeLayout> But when i change this line of code int resID = getActivity().getResources().getIdentifier(index, "drawable", getActivity().getPackageName()); to this: int resID = getActivity().getResources().getIdentifier("1", "drawable", getActivity().getPackageName()); it works fine where "1" is an image stored in the resources folder "1.jpg"

  • Answer:

    Put mViewPager.setOffscreenPageLimit(5); after mViewPager.setAdapter(); Replace 5 with the number of pages that are off screen, i.e. total number of pages minus 1. This will make your ViewPager smooth.

4mahmoud at Stack Overflow Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.