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

    final Resources res = getResources();
    final GridViewPager pager = (GridViewPager) findViewById(R.id.pager);

    pager.setOnApplyWindowInsetsListener(
        new OnApplyWindowInsetsListener() {
          @Override
          public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
            final boolean isRound = insets.isRound();
            int rowMargin = res.getDimensionPixelOffset(R.dimen.page_row_margin);
            int colMargin =
                res.getDimensionPixelOffset(
                    isRound ? R.dimen.page_column_margin_round : R.dimen.page_column_margin);

            pager.setPageMargins(rowMargin, colMargin);
            pager.onApplyWindowInsets(insets);

            return insets;
          }
        });
    pager.setAdapter(new SampleGridPagerAdapter(this, getFragmentManager()));
    DotsPageIndicator dotsPageIndicator = (DotsPageIndicator) findViewById(R.id.page_indicator);
    dotsPageIndicator.setPager(pager);
  }
Пример #2
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setData();

    // Get UI references
    mImageView = (ImageView) findViewById(R.id.flickr_image);
    mImageView.setScaleType(ImageView.ScaleType.FIT_START);
    mImageView.setBackgroundColor(Color.parseColor("gray"));
    mPageIndicator = (DotsPageIndicator) findViewById(R.id.page_indicator);
    mViewPager = (GridViewPager) findViewById(R.id.pager);

    // Assigns an adapter to provide the content for this pager
    mViewPager.setAdapter(new FlickrGridPagerAdapter(this));

    mPageIndicator.setPager(mViewPager);
    mViewPager.setOnPageChangeListener(
        new GridViewPager.OnPageChangeListener() {

          @Override
          public void onPageScrolled(
              int row,
              int column,
              float rowOffset,
              float columnOffset,
              int rowOffsetPixels,
              int columnOffsetPixels) {
            mPageIndicator.onPageScrolled(
                row, column, rowOffset, columnOffset, rowOffsetPixels, columnOffsetPixels);
          }

          @Override
          public void onPageSelected(int row, int column) {
            mImageView.setImageBitmap(flickr_images.get(row).getImage());
            mPageIndicator.onPageSelected(row, column);
          }

          @Override
          public void onPageScrollStateChanged(int state) {
            mPageIndicator.onPageScrollStateChanged(state);
          }
        });

    mImageView.setImageBitmap(flickr_images.get(0).getImage());
  }