@Result(FlickrApi.LOAD_IMAGES_EVENT)
  private void onPhotosLoaded(List<Photo> photos, boolean hasMore) {
    mPhotoCount = photos.size();
    mGridAdapter.setPhotos(photos, hasMore);
    mPagerAdapter.setPhotos(photos);
    mGridAdapter.onNextItemsLoaded();

    // Ensure listener called for 0 position
    mPagerListener.onPageSelected(mViews.pager.getCurrentItem());

    // Restoring saved state
    if (mPagerPhotoPosition != -1) {
      if (mPagerPhotoPosition < mPhotoCount) {
        mPagerAdapter.setActivated(true);
        mAnimator.enter(mPagerPhotoPosition, false);
      }
      mPagerPhotoPosition = -1;
    }

    if (mGridPosition != -1) {
      if (mGridPosition < mPhotoCount) {
        ((GridLayoutManager) mViews.grid.getLayoutManager())
            .scrollToPositionWithOffset(mGridPosition, mGridPositionFromTop);
      }
      mGridPosition = -1;
      mGridPositionFromTop = 0;
    }
  }
 private boolean onOptionsItemSelectedFullMode(MenuItem item) {
   switch (item.getItemId()) {
     case R.id.menu_crop:
       Photo photo = mPagerAdapter.getPhoto(mViews.pager.getCurrentItem());
       if (photo == null) return false;
       PhotoCropActivity.show(AdvancedDemoActivity.this, photo);
       return true;
     default:
       return false;
   }
 }
  @Override
  public void onPositionUpdate(float state, boolean isLeaving) {
    mViews.pagerBackground.setVisibility(state == 0f ? View.INVISIBLE : View.VISIBLE);
    mViews.pagerBackground.getBackground().setAlpha((int) (255 * state));

    mViews.toolbar.setVisibility(state == 1f ? View.INVISIBLE : View.VISIBLE);
    mViews.toolbar.setAlpha((float) Math.sqrt(1d - state)); // Slow down toolbar animation

    mViews.pagerToolbar.setVisibility(state == 0f ? View.INVISIBLE : View.VISIBLE);
    mViews.pagerToolbar.setAlpha(state);

    mViews.pagerTitle.setVisibility(state == 1f ? View.VISIBLE : View.INVISIBLE);

    if (isLeaving && state == 0f) mPagerAdapter.setActivated(false);
  }
  @Override
  protected void onSaveInstanceState(Bundle outState) {
    mPagerPhotoPosition =
        mAnimator.isLeaving() || mPagerAdapter.getCount() == 0 ? -1 : mViews.pager.getCurrentItem();

    if (mViews.grid.getChildCount() > 0) {
      View child = mViews.grid.getChildAt(0);
      mGridPosition = mViews.grid.getChildAdapterPosition(child);
      mGridPositionFromTop =
          child.getTop() - Views.getMarginParams(child).topMargin - mViews.grid.getPaddingTop();
    } else {
      mGridPosition = -1;
      mGridPositionFromTop = 0;
    }

    mSettingsMenu.onSaveInstanceState(outState);
    super.onSaveInstanceState(outState);
  }
 private void onPhotoInPagerSelected(int position) {
   Photo photo = mPagerAdapter.getPhoto(position);
   if (photo == null) {
     mViews.pagerTitle.setText(null);
   } else {
     SpannableBuilder title = new SpannableBuilder(AdvancedDemoActivity.this);
     title
         .append(photo.getTitle())
         .append("\n")
         .createStyle()
         .setColorResId(R.color.text_secondary_light)
         .apply()
         .append(R.string.photo_by)
         .append(" ")
         .append(photo.getOwner().getUsername());
     mViews.pagerTitle.setText(title.build());
   }
 }
  private void initPager() {
    // Setting up pager views
    mPagerAdapter = new FlickrPhotoPagerAdapter(mViews.pager);
    mPagerAdapter.setSetupListener(this);

    mPagerListener =
        new ViewPager.SimpleOnPageChangeListener() {
          @Override
          public void onPageSelected(int position) {
            onPhotoInPagerSelected(position);
          }
        };

    mViews.pager.setAdapter(mPagerAdapter);
    mViews.pager.addOnPageChangeListener(mPagerListener);
    mViews.pager.setPageTransformer(true, new DepthPageTransformer());

    mViews.pagerToolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
    mViews.pagerToolbar.setNavigationOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(@NonNull View v) {
            onBackPressed();
          }
        });

    onCreateOptionsMenuFullMode(mViews.pagerToolbar.getMenu());

    mViews.pagerToolbar.setOnMenuItemClickListener(
        new Toolbar.OnMenuItemClickListener() {
          @Override
          public boolean onMenuItemClick(MenuItem item) {
            return onOptionsItemSelectedFullMode(item);
          }
        });
  }
 @Override
 public void onPhotoClick(Photo photo, int position, ImageView image) {
   mPagerAdapter.setActivated(true);
   mAnimator.enter(position, true);
 }