Android Open Source - Android-ImgSlider My View Pager Activity






From Project

Back to project page Android-ImgSlider.

License

The source code is released under:

GNU General Public License

If you think the Android project Android-ImgSlider 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.example.android.skeletonapp;
//from   w  w  w . j ava  2 s . c o  m
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;


public class MyViewPagerActivity extends Activity {
  private ViewPager viewPager;
  private List<ImageView> imageViews;

  private String[] titles;
  private int[] imageResId;
  private List<View> dots;

  private TextView tv_title;
  private int currentItem = 0;

  // An ExecutorService that can schedule commands to run after a given delay,
  // or to execute periodically.
  private ScheduledExecutorService scheduledExecutorService;


  private Handler handler = new Handler() {
    public void handleMessage(android.os.Message msg) {
      viewPager.setCurrentItem(currentItem);
    };
  };

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    imageResId = new int[] { R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e };
    titles = new String[imageResId.length];
    titles[0] = "????1";
    titles[1] = "????2";
    titles[2] = "????3";
    titles[3] = "????4";
    titles[4] = "????5";

    imageViews = new ArrayList<ImageView>();


    for (int i = 0; i < imageResId.length; i++) {
      ImageView imageView = new ImageView(this);
      imageView.setImageResource(imageResId[i]);
      imageView.setScaleType(ScaleType.CENTER_CROP);
      imageViews.add(imageView);
    }

    dots = new ArrayList<View>();
    dots.add(findViewById(R.id.v_dot0));
    dots.add(findViewById(R.id.v_dot1));
    dots.add(findViewById(R.id.v_dot2));
    dots.add(findViewById(R.id.v_dot3));
    dots.add(findViewById(R.id.v_dot4));

    tv_title = (TextView) findViewById(R.id.tv_title);
    tv_title.setText(titles[0]);//

    viewPager = (ViewPager) findViewById(R.id.vp);
    viewPager.setAdapter(new MyAdapter());

    viewPager.setOnPageChangeListener(new MyPageChangeListener());

  }

  @Override
  protected void onStart() {
    scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();

    scheduledExecutorService.scheduleAtFixedRate(new ScrollTask(), 1, 2, TimeUnit.SECONDS);
    super.onStart();
  }

  @Override
  protected void onStop() {

    scheduledExecutorService.shutdown();
    super.onStop();
  }


  private class ScrollTask implements Runnable {

    public void run() {
      synchronized (viewPager) {
        System.out.println("currentItem: " + currentItem);
        currentItem = (currentItem + 1) % imageViews.size();
        handler.obtainMessage().sendToTarget();
      }
    }

  }

  private class MyPageChangeListener implements OnPageChangeListener {
    private int oldPosition = 0;

    /**
     * This method will be invoked when a new page becomes selected.
     * position: Position index of the new selected page.
     */
    public void onPageSelected(int position) {
      currentItem = position;
      tv_title.setText(titles[position]);
      dots.get(oldPosition).setBackgroundResource(R.drawable.dot_normal);
      dots.get(position).setBackgroundResource(R.drawable.dot_focused);
      oldPosition = position;
    }

    public void onPageScrollStateChanged(int arg0) {

    }

    public void onPageScrolled(int arg0, float arg1, int arg2) {

    }
  }


  private class MyAdapter extends PagerAdapter {

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

    @Override
    public Object instantiateItem(View arg0, int arg1) {
      ((ViewPager) arg0).addView(imageViews.get(arg1));
      return imageViews.get(arg1);
    }

    @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 == arg1;
    }

    @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1) {

    }

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

    @Override
    public void startUpdate(View arg0) {

    }

    @Override
    public void finishUpdate(View arg0) {

    }
  }
}




Java Source Code List

com.example.android.skeletonapp.MyViewPagerActivity.java
com.example.android.skeletonapp.SkeletonActivity.java