Wednesday, March 15, 2017

android - ViewPager with PagerAdapter example

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity{
    private Context mContext;
    private Activity mActivity;

    private RelativeLayout mRootLayout;
    private ViewPager mViewPager;

    private PagerAdapter mAdapter;
    private List<String> mColors = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the application context
        mContext = getApplicationContext();
        mActivity = MainActivity.this;

        // Get the widget reference from XML layout
        mRootLayout = (RelativeLayout) findViewById(R.id.root_layout);
        mViewPager = (ViewPager) findViewById(R.id.view_pager);

        // Put some values to the list
        mColors.add("Red");
        mColors.add("Green");
        mColors.add("Blue");
        mColors.add("Yellow");
        mColors.add("Black");
        mColors.add("Magenta");
        mColors.add("Orange");
        mColors.add("Crimson");
        mColors.add("Pink");

        // Initialize a new color pager adapter
        mAdapter = new ColorPagerAdapter(
                mContext, mColors.toArray(new String[mColors.size()])
        );

        // Data bind the view pager with pager adapter
        mViewPager.setAdapter(mAdapter);
    }
}
ColorPagerAdapter.java

package com.cfsuman.me.androidcodesnippets;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.RelativeLayout.LayoutParams;


public class ColorPagerAdapter extends PagerAdapter {
    private Context mContext;
    private String[] mDataArray;

    public ColorPagerAdapter(Context context, String[] array){
        mContext = context;
        mDataArray = array;
    }

    @Override
    public boolean isViewFromObject(View v, Object object){
        return v == (TextView) object;
    }

    @Override
    public int getCount(){
        // Count the items and return it
        return mDataArray.length;
    }

    @Override
    public Object instantiateItem(ViewGroup parent, int position){
        // Initialize a text view to represent a page on view pager
        TextView tv = new TextView(mContext);
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        tv.setLayoutParams(params);

        int counter = position+1;

        tv.setText("" + counter+ ". " + mDataArray[position]);
        tv.setTextSize(TypedValue.COMPLEX_UNIT_SP,50);
        tv.setGravity(Gravity.CENTER);

        // Add the text view to view group
        parent.addView(tv);

        // Return the text view
        return tv;
    }

    @Override
    public void destroyItem(ViewGroup parent, int position, Object object){
        // Remove the view from view group at specified position
        parent.removeView((TextView)object);
    }
}