Android Open Source - NerdZoo Animal Fragment






From Project

Back to project page NerdZoo.

License

The source code is released under:

Apache License

If you think the Android project NerdZoo listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.bignerdranch.android.nerdzoo.controller;
/*from   ww  w .  j  a  v  a  2 s .  co  m*/
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bignerdranch.android.nerdzoo.BaseApplication;
import com.bignerdranch.android.nerdzoo.R;
import com.bignerdranch.android.nerdzoo.anim.PathAnimator;
import com.bignerdranch.android.nerdzoo.model.Animal;
import com.bignerdranch.android.nerdzoo.model.Zoo;
import com.squareup.picasso.Picasso;

import java.util.UUID;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

public class AnimalFragment extends Fragment {

    @InjectView(R.id.fragment_animal_layout) public RelativeLayout mLayout;
    @InjectView(R.id.fragment_animal_image) public ImageView mImageView;
    @InjectView(R.id.fragment_animal_description) public TextView mDescriptionTextView;
    @InjectView(R.id.fragment_animal_heart) public ImageButton mHeartImageButton;

    @Inject Zoo mZoo;

    private Animal mAnimal;

    public static AnimalFragment newInstance(UUID id) {
        Bundle args = new Bundle();
        args.putSerializable(ZooFragment.EXTRA_ANIMAL_ID, id);

        AnimalFragment fragment = new AnimalFragment();
        fragment.setArguments(args);

        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseApplication.get(getActivity()).inject(this);
        setHasOptionsMenu(true);

        UUID id = (UUID) getArguments().getSerializable(ZooFragment.EXTRA_ANIMAL_ID);
        mAnimal = mZoo.findAnimalById(id);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_animal, container, false);
        ButterKnife.inject(this, view);
        setHasOptionsMenu(true);

        setupLayoutListener();
        updateUI();
        return view;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.menu_animal, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_base_refresh:
                updateUI();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @OnClick(R.id.fragment_animal_heart)
    public void onClickHeartButton() {
        mAnimal.setFavorite(!mAnimal.isFavorite());
        mHeartImageButton.setBackgroundResource(mAnimal.isFavorite() ? R.drawable.animation_heart_filling : R.drawable.animation_heart_emptying);
        startHeartAnimation();
    }

    private void setupLayoutListener() {
        mLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                mLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                PathAnimator.curveInFromRight(getActivity(), mHeartImageButton, mLayout.getWidth(), mLayout.getHeight());
            }
        });
    }

    private void updateUI() {
        getActivity().setTitle(mAnimal.getNameResourceId());
        mDescriptionTextView.setText(mAnimal.getDescriptionResourceId());
        Picasso.with(getActivity())
                .load(mAnimal.getImageResourceId())
                .into(mImageView);
    }

    private void startHeartAnimation() {
        mHeartImageButton.setClickable(false);
        AnimationDrawable heartAnimation = (AnimationDrawable) mHeartImageButton.getBackground();
        heartAnimation.start();
        int time = heartAnimation.getDuration(0) * heartAnimation.getNumberOfFrames();
        new Handler().postDelayed(() -> {
            mHeartImageButton.setClickable(true);
            updateUI();
        }, time);
    }

}




Java Source Code List

com.bignerdranch.android.nerdzoo.ApplicationTest.java
com.bignerdranch.android.nerdzoo.BaseApplication.java
com.bignerdranch.android.nerdzoo.BaseModule.java
com.bignerdranch.android.nerdzoo.anim.BaseItemAnimator.java
com.bignerdranch.android.nerdzoo.anim.PathAnimator.java
com.bignerdranch.android.nerdzoo.anim.RevealAnimator.java
com.bignerdranch.android.nerdzoo.anim.ZooItemAnimator.java
com.bignerdranch.android.nerdzoo.base.BaseActivity.java
com.bignerdranch.android.nerdzoo.base.BaseDrawerActivity.java
com.bignerdranch.android.nerdzoo.base.BaseNormalActivity.java
com.bignerdranch.android.nerdzoo.controller.AboutFragment.java
com.bignerdranch.android.nerdzoo.controller.AnimalActivity.java
com.bignerdranch.android.nerdzoo.controller.AnimalFragment.java
com.bignerdranch.android.nerdzoo.controller.MainActivity.java
com.bignerdranch.android.nerdzoo.controller.ZooFragment.java
com.bignerdranch.android.nerdzoo.drawer.DrawerAdapter.java
com.bignerdranch.android.nerdzoo.drawer.DrawerItem.java
com.bignerdranch.android.nerdzoo.model.Animal.java
com.bignerdranch.android.nerdzoo.model.Zoo.java
com.bignerdranch.android.nerdzoo.util.BuildUtils.java