ViewPager Image









MyActivity.java

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

public class MyActivity extends Activity {

    ViewPagerAdapter mSectionsPagerAdapter;
    ViewPager mViewPager;

    public static Integer[] mImageIds = {
            R.drawable.tn_a1,
            R.drawable.tn_a2,
            R.drawable.tn_a3,
    };

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

        mSectionsPagerAdapter = new ViewPagerAdapter(getFragmentManager());

        mViewPager = (ViewPager) findViewById(R.id.pager);

        mSectionsPagerAdapter.addFragment(Fragmentos.newInstance(4, getResources().getColor(R.color.android_blue), R.drawable.tn_a1));
        mSectionsPagerAdapter.addFragment(Fragmentos.newInstance(5, getResources().getColor(R.color.android_darkgreen), mImageIds[1]));
        mSectionsPagerAdapter.addFragment(Fragmentos.newInstance(6, getResources().getColor(R.color.android_red), mImageIds[2]));

        mViewPager.setAdapter(mSectionsPagerAdapter);
    }
}

ViewPagerAdapter.java

import android.app.Fragment;
import android.app.FragmentManager;
import android.support.v13.app.FragmentPagerAdapter;

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

public class ViewPagerAdapter extends FragmentPagerAdapter {
    List<Fragment> fragments; //acá voy a guardar los fragments

    //constructor
    public ViewPagerAdapter(FragmentManager fm) {
        super(fm);
        fragments = new ArrayList<Fragment>();
    }

    @Override
    public Fragment getItem(int position) {
        //return PlaceholderFragment.newInstance(position + 1);
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        //return 3;
        return this.fragments.size();
    }

    public void addFragment(Fragment xfragment){
        this.fragments.add(xfragment);
    }
}

Fragmentos.java

import android.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class Fragmentos extends Fragment {
    /**
     Agregue "color"
     */
    private static final String ARG_SECTION_NUMBER = "section_number";
    private static final String BACKGROUND_COLOR = "color";
    private static final String IMAGEVIEW = "image";

    private int section_number;
    private int color;
    private int image;

    public static Fragmentos newInstance(int sectionNumber, int color, int image) {

        Fragmentos fragment = new Fragmentos();   //instanciamos un nuevo fragment

        Bundle args = new Bundle();                                 //guardamos los parametros
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        args.putInt(BACKGROUND_COLOR, color); //agrego ademas el color de fondo
        args.putInt(IMAGEVIEW, image);
        fragment.setArguments(args);
        fragment.setRetainInstance(true);     //agrego para que no se pierda los valores de la instancia
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //cuando crea una instancia de tipo PlaceholderFragment
        //si lo enviamos parametros, guarda esos
        //si no le envio nada, toma el color gris y un número aleatroio
        if(getArguments() != null){
            this.section_number = getArguments().getInt(ARG_SECTION_NUMBER);
            this.color = getArguments().getInt(BACKGROUND_COLOR);
            this.image = getArguments().getInt(IMAGEVIEW);
        }
        else{
            this.color = Color.GRAY;
            this.section_number = (int)(Math.random() * 5);
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_my, container, false);

        TextView tv_section = (TextView) rootView.findViewById(R.id.tv_section_label);
        tv_section.setText("" + section_number);

        rootView.setBackgroundColor(this.color);

        ImageView frg_image = (ImageView) rootView.findViewById(R.id.frg_imageView);
        frg_image.setImageResource(image);

        return rootView;
    }
}

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="white">#FFffffff</color>
    <color name="black">#FF000000</color>
    <color name="gray">#FF8c8c8c</color>
    <color name="android_blue">#FF33b5e5</color>
    <color name="android_darkblue">#FF0099cc</color>
    <color name="android_green">#FF99cc00</color>
    <color name="android_darkgreen">#FF669900</color>
    <color name="android_red">#FFff4444</color>
    <color name="android_darkred">#FFcc0000</color>
    <color name="android_yellow">#FFffd75a</color>
    <color name="android_darkyellow">#FFffc40d</color>
    <color name="android_orange">#FFffbb33</color>
    <color name="android_darkorange">#FFff8800</color>
    <color name="android_pink">#FFd9698c</color>
    <color name="android_darkpink">#FFc3325f</color>
    <color name="android_purple">#FFaa66cc</color>
    <color name="android_darkpurple">#FF9933cc</color>

</resources>


activity_my.xml

<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_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MyActivity" /> 

fragment_my.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"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MyActivity$PlaceholderFragment">

    <TextView
        android:id="@+id/tv_section_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ramiro"
        android:textSize="50dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:id="@+id/frg_imageView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/tv_section_label"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

      


Imagenes utilizadas