Android Open Source - Android-Jake-Wharton-ViewPager-Indicator-Tutorial View Pager Adapter






From Project

Back to project page Android-Jake-Wharton-ViewPager-Indicator-Tutorial.

License

The source code is released under:

Apache License

If you think the Android project Android-Jake-Wharton-ViewPager-Indicator-Tutorial listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.androidbegin.viewpagerindicatortutorial;
//  w w w  .  j av a  2s . c  om
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ViewPagerAdapter extends PagerAdapter {
  // Declare Variables
  Context context;
  String[] rank;
  String[] country;
  String[] population;
  int[] flag;
  LayoutInflater inflater;

  public ViewPagerAdapter(Context context, String[] rank, String[] country,
      String[] population, int[] flag) {
    this.context = context;
    this.rank = rank;
    this.country = country;
    this.population = population;
    this.flag = flag;
  }

  @Override
  public int getCount() {
    return rank.length;
  }

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

  @Override
  public Object instantiateItem(ViewGroup container, int position) {

    // Declare Variables
    TextView txtrank;
    TextView txtcountry;
    TextView txtpopulation;
    ImageView imgflag;

    inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View itemView = inflater.inflate(R.layout.viewpager_item, container,
        false);

    // Locate the TextViews in viewpager_item.xml
    txtrank = (TextView) itemView.findViewById(R.id.rank);
    txtcountry = (TextView) itemView.findViewById(R.id.country);
    txtpopulation = (TextView) itemView.findViewById(R.id.population);

    // Capture position and set to the TextViews
    txtrank.setText(rank[position]);
    txtcountry.setText(country[position]);
    txtpopulation.setText(population[position]);

    // Locate the ImageView in viewpager_item.xml
    imgflag = (ImageView) itemView.findViewById(R.id.flag);
    // Capture position and set to the ImageView
    imgflag.setImageResource(flag[position]);

    // Add viewpager_item.xml to ViewPager
    ((ViewPager) container).addView(itemView);

    return itemView;
  }

  @Override
  public void destroyItem(ViewGroup container, int position, Object object) {
    // Remove viewpager_item.xml from ViewPager
    ((ViewPager) container).removeView((RelativeLayout) object);

  }
}




Java Source Code List

com.androidbegin.viewpagerindicatortutorial.MainActivity.java
com.androidbegin.viewpagerindicatortutorial.ViewPagerAdapter.java
com.viewpagerindicator.CirclePageIndicator.java
com.viewpagerindicator.IconPageIndicator.java
com.viewpagerindicator.IconPagerAdapter.java
com.viewpagerindicator.IcsLinearLayout.java
com.viewpagerindicator.LinePageIndicator.java
com.viewpagerindicator.PageIndicator.java
com.viewpagerindicator.TabPageIndicator.java
com.viewpagerindicator.TitlePageIndicator.java
com.viewpagerindicator.UnderlinePageIndicator.java