com.sociablue.nanodegree_p1.MovieDetailPagerFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.sociablue.nanodegree_p1.MovieDetailPagerFragment.java

Source

/*
 * Copyright (c) 2015. Sociablue Labs
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.sociablue.nanodegree_p1;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.transition.Transition;
import android.transition.TransitionSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.sociablue.nanodegree_p1.Constants.MdbConstants;
import com.sociablue.nanodegree_p1.base.BaseFragment;
import com.sociablue.nanodegree_p1.models.Movie;
import com.sociablue.nanodegree_p1.utils.Utils;

import java.util.List;

/**
 * Created by okikejiani on 2015-09-02.
 * Contains ViewPager that allows swiping between movie details views
 */
public class MovieDetailPagerFragment extends BaseFragment implements ViewPager.OnPageChangeListener {

    //Member Variables
    List<Movie> mMovieList;
    int mStartPosition;
    PagerAdapter mPagerAdapter;
    ViewPager mPager;
    boolean mHasJustBeenLoaded = true;
    int mCurrentPosition;

    //Constants
    final int NUM_OF_PAGES_TO_PRELOAD = 2;
    final int HEIGHT_OF_POSTER_IMAGE = 180;
    final int WIDTH_OF_POSTER_IMAGE = 120;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_movie_detail_pager, container, false);
        Movie currentMovie = mMovieList.get(mStartPosition);
        final PosterImageView posterImageView = (PosterImageView) rootView
                .findViewById(R.id.transition_poster_image);
        posterImageView.setIsSharedElement(true).setHeight((int) Utils.toPx(HEIGHT_OF_POSTER_IMAGE))
                .setWidth((int) Utils.toPx(WIDTH_OF_POSTER_IMAGE))
                .setImageResource(MdbConstants.IMAGE_BASE_URL + "w500" + currentMovie.getPosterPath());

        mPagerAdapter = new MovieDetailPagerAdapter(getActivity().getSupportFragmentManager(), mMovieList);
        mPager = (ViewPager) rootView.findViewById(R.id.details_view_pager);
        mPager.setAdapter(mPagerAdapter);
        mPager.setOffscreenPageLimit(NUM_OF_PAGES_TO_PRELOAD);
        mPager.setCurrentItem(mStartPosition);
        mPager.addOnPageChangeListener(this);
        return rootView;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setupTransitionListener();

    }

    /*
    * Initializes a transition listener that listens for the completion of the animation of the sharedElement.
    * Removes view containing the shared element when transition finishes
    */
    private void setupTransitionListener() {

        final TransitionSet transitionSet = (TransitionSet) getSharedElementEnterTransition();
        transitionSet.addListener(new Transition.TransitionListener() {
            @Override
            public void onTransitionStart(Transition transition) {
            }

            @Override
            public void onTransitionEnd(Transition transition) {
                transitionSet.removeListener(this);
                removeTransitionView();
            }

            @Override
            public void onTransitionCancel(Transition transition) {
            }

            @Override
            public void onTransitionPause(Transition transition) {
            }

            @Override
            public void onTransitionResume(Transition transition) {
            }
        });
    }

    /*
     * Removes interstitial view containing the shared element.
     */
    private void removeTransitionView() {
        View rootView = getView();
        (rootView.findViewById(R.id.details_view_pager)).setVisibility(View.VISIBLE);
        (rootView.findViewById(R.id.transition_view)).setVisibility(View.INVISIBLE);
        startEnterAnimations();
    }

    /*
     * Animates in the hidden views
     */
    private void startEnterAnimations() {
        int id = mMovieList.get(mCurrentPosition).getId();
        View currentPage = mPager.findViewWithTag(id);
        FrameLayout imgContainer = (FrameLayout) currentPage.findViewById(R.id.details_img_container);
        imgContainer.animate().translationY(0);
        LinearLayout contentContainer = (LinearLayout) currentPage.findViewById(R.id.details_copy_container);
        contentContainer.animate().translationY(0);
    }

    /*
     * Hides hero and content views in preparation for the enter transition
     */
    private void setupEnterAnimations() {
        int id = mMovieList.get(mCurrentPosition).getId();
        View currentPage = mPager.findViewWithTag(id);
        FrameLayout imgContainer = (FrameLayout) currentPage.findViewById(R.id.details_img_container);
        imgContainer.setTranslationY(-imgContainer.getMeasuredHeight());
        LinearLayout contentContainer = (LinearLayout) currentPage.findViewById(R.id.details_copy_container);
        contentContainer.setTranslationY(contentContainer.getMeasuredHeight());
    }

    public void setMovieList(List<Movie> movieList) {
        mMovieList = movieList;
    }

    public void setStartPosition(int position) {
        mStartPosition = position;
    }

    /*
     * Detects when the page has been scrolled and triggers horizontal scrolling parallax between pages.
     */
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        mCurrentPosition = position;
        //Only run enter animation on the first fragment
        if (mHasJustBeenLoaded) {
            mHasJustBeenLoaded = false;
            setupEnterAnimations();
        }

        horizontalParallax(position, positionOffsetPixels);

    }

    /*
     * Creates horizontal parallaxing effect between pages.
     */
    private void horizontalParallax(int position, float positionOffsetPixels) {

        int id = mMovieList.get(position).getId();
        View currentPage = mPager.findViewWithTag(id);

        FrameLayout imgContainer = (FrameLayout) currentPage.findViewById(R.id.details_img_container);
        imgContainer.setTranslationX(positionOffsetPixels * 0.5f);

        /*
         * Modify the X translation of the view to the right of the current view by half the distance scrolled.
         * The leftmost view is always the current view regardless of the direction and amount of image visible.
         */
        if (position < mMovieList.size() - 1) {
            View rightPage = mPager.findViewWithTag(mMovieList.get(position + 1).getId());
            FrameLayout rightImgContainer = (FrameLayout) rightPage.findViewById(R.id.details_img_container);
            rightImgContainer.setTranslationX(-(((rightImgContainer.getWidth() - positionOffsetPixels) * 0.5f)));
        }

    }

    @Override
    public void onPageSelected(int position) {
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }

}