Java tutorial
/******************************************************************************* * Copyright 2011, 2012 Chris Banes. * * 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.bjdz.brsse.ui.gallery; import java.util.ArrayList; import java.util.List; import com.bjdz.brsse.R; import com.bjdz.brsse.ui.post.ActivityPostStepC; import com.bjdz.brsse.utils.AppCutscenes; import com.bjdz.brsse.utils.Utility; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.view.Window; import android.widget.RelativeLayout; import android.widget.TextView; public class ActivityGallery extends Activity { private ViewPager mViewPager; private RelativeLayout mContent; private TextView mTitle; private View mRecycle; private View mBack; private int page = -1; private int oNum = 0; private List<String> imgList = new ArrayList<String>(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); AppCutscenes.setHorizontalIn(ActivityGallery.this); setContentView(R.layout.activity_gallery); //gallery mContent = (RelativeLayout) super.findViewById(R.id.gallery_content); mViewPager = new HackyViewPager(this); mViewPager.setBackgroundColor(Color.parseColor("#000000")); RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); mContent.addView(mViewPager, lp); //data imgList = this.getIntent().getExtras().getStringArrayList("photos"); oNum = imgList.size(); page = this.getIntent().getExtras().getInt("page"); //adapter mViewPager.setAdapter(new SamplePagerAdapter(imgList)); mViewPager.setCurrentItem(page); mViewPager.setOnPageChangeListener(pagerListener); //other views mTitle = (TextView) super.findViewById(R.id.gallery_num); mTitle.setText(page + 1 + "/" + imgList.size()); mBack = super.findViewById(R.id.gallery_back); mBack.setOnClickListener(btnsListener); mRecycle = super.findViewById(R.id.gallery_recycle); mRecycle.setOnClickListener(btnsListener); if (this.getIntent().hasExtra("read")) { mRecycle.setVisibility(View.INVISIBLE); } } private OnClickListener btnsListener = new OnClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case R.id.gallery_back: if (oNum != imgList.size()) { Intent it = new Intent(ActivityGallery.this, ActivityPostStepC.class); it.putStringArrayListExtra("newList", (ArrayList<String>) imgList); it.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); it.setAction("GALLERY"); ActivityGallery.this.startActivity(it); } ActivityGallery.this.finish(); AppCutscenes.setHorizontalOut(ActivityGallery.this); break; case R.id.gallery_recycle: AlertDialog.Builder builder = new AlertDialog.Builder(ActivityGallery.this); builder.setMessage("").setCancelable(false) .setPositiveButton("", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { int temp = mViewPager.getCurrentItem(); imgList.remove(mViewPager.getCurrentItem()); if (imgList.size() == 0) { mBack.performClick(); } mViewPager.setAdapter(new SamplePagerAdapter(imgList)); mViewPager.setOnPageChangeListener(pagerListener); mViewPager.setCurrentItem(temp - 1); if (temp == 0) { mTitle.setText("1/" + imgList.size()); } else { mTitle.setText(temp + "/" + imgList.size()); } } }).setNegativeButton("", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); AlertDialog alert = builder.create(); alert.show(); break; } } }; private OnPageChangeListener pagerListener = new OnPageChangeListener() { @Override public void onPageScrollStateChanged(int page) { } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageSelected(int page) { mTitle.setText((mViewPager.getCurrentItem() + 1) + "/" + imgList.size()); } }; static class SamplePagerAdapter extends PagerAdapter { private List<String> imgList = new ArrayList<String>(); private SamplePagerAdapter(List<String> l) { this.imgList = l; } @Override public int getCount() { return imgList.size(); } @Override public View instantiateItem(ViewGroup container, int position) { PhotoView photoView = new PhotoView(container.getContext()); photoView.setImageBitmap(Utility.getSmallBitmap(imgList.get(position))); System.out.println(imgList.get(position)); // Now just add PhotoView to ViewPager and return it container.addView(photoView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); return photoView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { mBack.performClick(); } return super.onKeyDown(keyCode, event); } }