com.example.testtab.fragment.GalleryPageFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.example.testtab.fragment.GalleryPageFragment.java

Source

/*
 * Copyright 2012 The Android Open Source Project
 *
 * 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.example.testtab.fragment;

import java.util.ArrayList;

import com.example.testtab.R;
import com.example.testtab.gallery.ImageAdapter;
import com.example.testtab.gallery.ImageBean;

//import android.app.Fragment;
import android.support.v4.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.AnimationSet;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;

/**
 * A fragment representing a single step in a wizard. The fragment shows a dummy title indicating
 * the page number, along with some dummy text.
 *
 * <p>This class is used by the {@link CardFlipActivity} and {@link
 * ScreenSlideActivity} samples.</p>
 */
public class GalleryPageFragment extends Fragment implements OnItemClickListener, OnItemSelectedListener {
    /**
     * The argument key for the page number this fragment represents.
     */
    public static final String ARG_PAGE = "page";

    /**
     * The fragment's page number, which is set to the argument value for {@link #ARG_PAGE}.
     */
    private int mPageNumber;

    // Gallery ---------------------------------------- //
    private static Context mContext;

    private static LayoutInflater mInflater;

    private ImageView mImageView;
    private Integer[] mThumbIds = { R.drawable.photo1, R.drawable.photo2, R.drawable.photo3, R.drawable.photo4,
            R.drawable.photo5, R.drawable.photo6 };

    private String[] mTitles = { "MerryGate", "MerryGo", "JurianDay", "SunsClock", "TwitEyes", "Tones" };

    private Gallery mGallery;
    private ImageAdapter mImageAdapter;

    private ArrayList<ImageBean> mImageList;

    /**
     * Factory method for this fragment class. Constructs a new fragment for the given page number.
     */
    public static GalleryPageFragment create(int pageNumber, Context context) {
        // Gallery ------------------------------------ //
        mContext = context;

        mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        GalleryPageFragment fragment = new GalleryPageFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PAGE, pageNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public GalleryPageFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPageNumber = getArguments().getInt(ARG_PAGE);
    }

    // make ImageBean and set into Array List --------- //
    private ArrayList<ImageBean> prepareImageList() {
        ArrayList<ImageBean> list = new ArrayList<ImageBean>();
        for (int i = 0; i < mThumbIds.length; i++) {
            ImageBean bean = new ImageBean(mThumbIds[i]);
            bean.setTitle(mTitles[i]);
            list.add(bean);
        }
        return list;
    }

    public void setGallery(ViewGroup rootView) {
        this.mGallery = (Gallery) rootView.findViewById(R.id.gallery);
        this.mImageList = prepareImageList();
        this.mImageAdapter = new ImageAdapter(mContext, this.mImageList);
        this.mGallery.setAdapter(this.mImageAdapter);
        this.mGallery.setOnItemClickListener(this);
        this.mGallery.setOnItemSelectedListener(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        /*        // Inflate the layout containing a title and body text.
                ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_screen_slide_page, container, false);
                // Set the title view to show the page number.
                ((TextView) rootView.findViewById(android.R.id.text1)).setText(getString(R.string.title_template_step, mPageNumber + 10));
        */
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.gallery, container, false);

        this.mImageView = (ImageView) rootView.findViewById(R.id.image_view);
        this.mImageList = null;

        setGallery(rootView);

        return rootView;
    }

    /**
     * Returns the page number represented by this fragment object.
     */
    public int getPageNumber() {
        return mPageNumber;
    }

    public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
        this.mImageView.setImageResource(this.mThumbIds[position]);

        if (this.mThumbIds[position] != null) {
            AnimationSet set = new AnimationSet(true);

            AlphaAnimation alpha = new AlphaAnimation(0.0f, 1.0f);

            set.addAnimation(alpha);
            set.setDuration(500);
            set.setFillAfter(true);

            this.mImageView.startAnimation(set);
        }

    }

    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }

    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        Toast.makeText(mContext, "" + position, Toast.LENGTH_SHORT).show();

    }
}