Java tutorial
/* * Copyright (C) 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.digitallifelab.environmentmonitor; import android.annotation.TargetApi; import android.content.Intent; import android.os.Build; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentStatePagerAdapter; import android.support.v4.app.NavUtils; import android.support.v4.view.ViewPager; import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.AppCompatActivity; import android.util.DisplayMetrics; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.Window; import android.view.WindowManager; import com.digitallifelab.environmentmonitor.Utils.Utility; import java.util.ArrayList; public class ImageDetailActivity extends AppCompatActivity implements View.OnClickListener { public static final String EXTRA_IMAGE = "extra_image"; public static final String IMAGES_ARRAY = "Viewer_ImagesArray"; private ImagePagerAdapter mAdapter; private ViewPager mPager; private ArrayList<String> imagesList; @TargetApi(Build.VERSION_CODES.HONEYCOMB) @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.image_detail_pager); // Fetch screen height and width, to use as our max size when loading images as this // activity runs full screen final DisplayMetrics displayMetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); final int height = displayMetrics.heightPixels; final int width = displayMetrics.widthPixels; Intent data = getIntent(); if (data.hasExtra(IMAGES_ARRAY)) { imagesList = data.getStringArrayListExtra(IMAGES_ARRAY); } else { imagesList = new ArrayList<String>(); } // Set up ViewPager and backing adapter mAdapter = new ImagePagerAdapter(getSupportFragmentManager(), imagesList.size()); mPager = (ViewPager) findViewById(R.id.pager); mPager.setAdapter(mAdapter); mPager.setPageMargin((int) getResources().getDimension(R.dimen.activity_horizontal_margin)); mPager.setOffscreenPageLimit(2); // Set up activity to go full screen getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); // Enable some additional newer visibility and ActionBar features to create a more // immersive photo viewing experience if (Utility.hasHoneycomb()) { //setSupportActionBar(null); final ActionBar actionBar = getSupportActionBar(); // Hide title text and set home as up actionBar.setDisplayShowTitleEnabled(false); actionBar.setDisplayHomeAsUpEnabled(true); // Hide and show the ActionBar as the visibility changes mPager.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() { @Override public void onSystemUiVisibilityChange(int vis) { if ((vis & View.SYSTEM_UI_FLAG_LOW_PROFILE) != 0) { actionBar.hide(); } else { actionBar.show(); } } }); // Start low profile mode and hide ActionBar mPager.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE); actionBar.hide(); } // Set the current item based on the extra passed in to this activity final int extraCurrentItem = getIntent().getIntExtra(EXTRA_IMAGE, -1); if (extraCurrentItem != -1) { mPager.setCurrentItem(extraCurrentItem); } } /** * The main adapter that backs the ViewPager. A subclass of FragmentStatePagerAdapter as there * could be a large number of items in the ViewPager and we don't want to retain them all in * memory at once but create/destroy them on the fly. */ private class ImagePagerAdapter extends FragmentStatePagerAdapter { private final int mSize; public ImagePagerAdapter(FragmentManager fm, int size) { super(fm); mSize = size; } @Override public int getCount() { return mSize; } @Override public Fragment getItem(int position) { //Get next image URL return ImageDetailFragment.newInstance(imagesList.get(position)); } } /** * Set on the ImageView in the ViewPager children fragments, to enable/disable low profile mode * when the ImageView is touched. */ @TargetApi(Build.VERSION_CODES.HONEYCOMB) @Override public void onClick(View v) { final int vis = mPager.getSystemUiVisibility(); if ((vis & View.SYSTEM_UI_FLAG_LOW_PROFILE) != 0) { mPager.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE); } else { mPager.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE); } } }