package com.example.serhat.fragmenttransitionexample;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.transition.TransitionInflater;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

/**
 * Created by Serhat on 7.12.15.
 *
 * To change Transitions, use res/transition/change_image_transform.xml file
 * there are several transitions with commented to try change them..
 */
public class Fragment1 extends Fragment {


    TextView text;
    ImageView image;
    Button btn;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragment1, container, false);

        text = (TextView) v.findViewById(R.id.text1);
        image = (ImageView) v.findViewById(R.id.imageView1);
        btn = (Button) v.findViewById(R.id.btn1);

        Picasso.with(getActivity())
                .load("http://i.kinja-img.com/gawker-media/image/upload/s--a2Uo7FWr--/c_fit,fl_progressive,q_80,w_636/ulagisgaezgi371h7qxv.jpg")
                .into(image);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                setSharedElementReturnTransition(TransitionInflater.from(getActivity()).inflateTransition(R.transition.change_image_transform));
                setExitTransition(TransitionInflater.from(getActivity()).inflateTransition(android.R.transition.explode));

                // Create new fragment to add (Fragment B)
                Fragment fragment = new Fragment2();
                fragment.setSharedElementEnterTransition(TransitionInflater.from(getActivity()).inflateTransition(R.transition.change_image_transform));
                fragment.setEnterTransition(TransitionInflater.from(getActivity()).inflateTransition(android.R.transition.explode));

                // Add Fragment B
                FragmentTransaction ft = getFragmentManager().beginTransaction()
                        .replace(R.id.frameLayout, fragment)
                        .addToBackStack("transaction")
                        .addSharedElement(image, "image")
                        .addSharedElement(text, "text");
                ft.commit();
            }
        });

        return v;
    }
}
