Example #1
0
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.image_detail_pager);

    pagerPosition = getIntent().getIntExtra(EXTRA_IMAGE_INDEX, 0);
    String[] urls = getIntent().getStringArrayExtra(EXTRA_IMAGE_URLS);

    mPager = (HackyViewPager) findViewById(R.id.pager);
    ImagePagerAdapter mAdapter = new ImagePagerAdapter(getSupportFragmentManager(), urls);
    mPager.setAdapter(mAdapter);
    indicator = (TextView) findViewById(R.id.indicator);

    CharSequence text = getString(R.string.viewpager_indicator, 1, mPager.getAdapter().getCount());
    indicator.setText(text);
    // 更新下标
    mPager.setOnPageChangeListener(
        new OnPageChangeListener() {

          @Override
          public void onPageScrollStateChanged(int arg0) {}

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

          @Override
          public void onPageSelected(int arg0) {
            CharSequence text =
                getString(R.string.viewpager_indicator, arg0 + 1, mPager.getAdapter().getCount());
            indicator.setText(text);
          }
        });
    if (savedInstanceState != null) {
      pagerPosition = savedInstanceState.getInt(STATE_POSITION);
    }

    mPager.setCurrentItem(pagerPosition);
  }
Example #2
0
 @Override
 public void onSaveInstanceState(Bundle outState) {
   outState.putInt(STATE_POSITION, mPager.getCurrentItem());
 }