Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why ViewPager is slow

Tags:

java

android

xml

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"

like image 265
4mahmoud Avatar asked Aug 23 '15 19:08

4mahmoud


2 Answers

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.

The reason why ViewPager is slow without this is that any page which is off screen gets destroyed and is recreated every time you switch to that page. If you have time consuming code in your Fragment's onActivityCreated(), they're going to be executed on every page switch. If you set the off screen page limit right, your Fragments will not be recreated every time you switch pages.

like image 91
David Heisnam Avatar answered Oct 08 '22 11:10

David Heisnam


In my case - I have more then 10 images and each image have weight more 800kb, and I check this issue, and realy fine work if pass as a parameter 3 page

viewPager.setOffscreenPageLimit(3);

Also if you store your images in res/drawable - rename drawable to drawable-nodpi, because -nodpi mean next

These are density-independent resources. The system does not scale resources tagged with this qualifier, regardless of the current screen's density.

like image 25
nicolas asinovich Avatar answered Oct 08 '22 11:10

nicolas asinovich