Search This Blog

Support my cause by downloading

Friday, 24 January 2014

Hack trick : Endless ViewPager example in android

I was trying to implement endless ViewPager, Suddenly a thought strike my mind and develop this tutorial which damn simple to integrate with any kind of adapter. I just showed here with ViewPager.

MainConcept


Return infinite value in getCount() and then take your own position to display values.


Support


Its support in every version of android. Look at output



So lets have a look of code which lead to an endless ViewPager in android application

Step 1) Take one array of Image which will be show in ViewPager elements. Change your MainActivity code to as follows

package com.fragmentpageradapter;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;

public class MainActivity extends FragmentActivity {
    ViewPager mPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mPager = (ViewPager) findViewById(R.id.frame);
        mPager.setOffscreenPageLimit(1);
        mPager.setAdapter(new EndLessAdapter(this, mImageArray));
    }

    private int[] mImageArray = { R.drawable.a, R.drawable.a1, R.drawable.a2,
            R.drawable.a3 };
}


Step 2) Create one simple layout for MainActivity which contain one ViewPager

<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=".MainActivity" >

    <android.support.v4.view.ViewPager
        android:id="@+id/frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true" >
    </android.support.v4.view.ViewPager>

</RelativeLayout>

Step 3) Create one PagerAdapter name EndLessAdapter

package com.fragmentpageradapter;

import android.os.Parcelable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

public class EndLessAdapter extends PagerAdapter {

    FragmentActivity activity;
    int imageArray[];

    public EndLessAdapter(FragmentActivity act, int[] imgArra) {
        imageArray = imgArra;
        activity = act;
    }

    public int getCount() {
        return Integer.MAX_VALUE;
    }

    private int pos = 0;

    public Object instantiateItem(View collection, int position) {

        ImageView mwebView = new ImageView(activity);
        ((ViewPager) collection).addView(mwebView, 0);
        mwebView.setScaleType(ScaleType.FIT_XY);
        mwebView.setImageResource(imageArray[pos]);

        if (pos >= imageArray.length - 1)
            pos = 0;
        else
            ++pos;

        return mwebView;
    }

    @Override
    public void destroyItem(View arg0, int arg1, Object arg2) {
        ((ViewPager) arg0).removeView((View) arg2);
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == ((View) arg1);
    }

    @Override
    public Parcelable saveState() {
        return null;
    }

}

What make it endless

    public int getCount() {
        return Integer.MAX_VALUE;
    }

Now you will start unrelated position so implement your own way of attaching data to ViewPager child.

    private int pos = 0;

    public Object instantiateItem(View collection, int position) {

        ImageView mwebView = new ImageView(activity);
        ((ViewPager) collection).addView(mwebView, 0);
        mwebView.setScaleType(ScaleType.FIT_XY);
        mwebView.setImageResource(imageArray[pos]);

        if (pos >= imageArray.length - 1)
            pos = 0;
        else
            ++pos;

        return mwebView;
    }


Memory Issue : Because ViewPager keep one child at each side so this solution work without any memory issue. If you are going to implement same solution for ListView then you may get memory issue as all row kept inside memory


Android News and source code