Java tutorial
/* * Copyright 2015 ?? * * 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 me.fireant.photoselect.ui; import com.squareup.picasso.Callback; import com.squareup.picasso.Picasso; import java.io.File; import java.text.MessageFormat; import java.util.ArrayList; import android.app.Activity; import android.content.Intent; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.ActionBar; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.CheckBox; import android.widget.ImageView; import android.widget.ProgressBar; import android.widget.Toast; import me.fireant.photoselect.R; import me.fireant.photoselect.base.BaseActivity; import me.fireant.photoselect.bean.Photo; /** * ?? * * @author ?? * @email fireant.me@gmail.com * @created 16/1/30 ?12:13 */ public class PhotoPreviewActivity extends BaseActivity implements View.OnClickListener { public static final String BUNDLE_PHOTOS = "bundle_photos"; public static final String BUNDLE_SELECT_INDEX = "bundle_select_index"; public static final String BUNDLE_SELECTED_PHOTOS = "bundle_selected_photos"; public static final String BUNDLE_MAX_SELECT_COUNT = "bundle_max_select_count"; public static void launch(Activity activity, ArrayList<Photo> photos, int selectIndex, ArrayList<Photo> selectedPhotos, int maxSelectCount, int requestCode) { Intent intent = new Intent(activity, PhotoPreviewActivity.class); intent.putParcelableArrayListExtra(BUNDLE_PHOTOS, photos); intent.putParcelableArrayListExtra(BUNDLE_SELECTED_PHOTOS, selectedPhotos); intent.putExtra(BUNDLE_SELECT_INDEX, selectIndex); intent.putExtra(BUNDLE_MAX_SELECT_COUNT, maxSelectCount); activity.startActivityForResult(intent, requestCode); } private ViewPager mViewPager; private PagerAdapter mPagerAdapter; private CheckBox mCbSelected; private ActionBar mActionBar; private ArrayList<Photo> mPhotos;// private int mSelectIndex; private ArrayList<Photo> mSelectedPhotos;// ? private int mMaxSelectPhotoCount; @Override protected int getContentLayoutRes() { return R.layout.activity_photo_preview; } @Override protected void initView() { mViewPager = getView(R.id.vg_photos); mCbSelected = getView(R.id.cb_photo_select); mActionBar = getSupportActionBar(); } @Override protected void initData() { Intent intent = getIntent(); if (intent != null) { mPhotos = intent.getParcelableArrayListExtra(BUNDLE_PHOTOS); mSelectIndex = intent.getIntExtra(BUNDLE_SELECT_INDEX, 0); mSelectedPhotos = intent.getParcelableArrayListExtra(BUNDLE_SELECTED_PHOTOS); mMaxSelectPhotoCount = intent.getIntExtra(BUNDLE_MAX_SELECT_COUNT, 1); } if (mSelectedPhotos == null) { mSelectedPhotos = new ArrayList<>(); } mPagerAdapter = new PagerAdapter() { @Override public int getCount() { return mPhotos.size(); } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } @Override public Object instantiateItem(ViewGroup container, int position) { View rootView = LayoutInflater.from(PhotoPreviewActivity.this) .inflate(R.layout.list_cell_photo_preview, null); final ImageView photoView = (ImageView) rootView.findViewById(R.id.iv_photo); final ProgressBar pbLoading = (ProgressBar) rootView.findViewById(R.id.pb_loading); final Photo photo = mPhotos.get(position); Picasso.with(PhotoPreviewActivity.this).load(new File(photo.getPath())) .error(R.drawable.ic_photo_error).into(photoView, new Callback() { @Override public void onSuccess() { photoView.setVisibility(View.VISIBLE); pbLoading.setVisibility(View.GONE); } @Override public void onError() { } }); container.addView(rootView); return rootView; } }; mViewPager.setAdapter(mPagerAdapter); mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { checkChecked(position); mSelectIndex = position + 1; setActionTitle(); } @Override public void onPageScrollStateChanged(int state) { } }); if (mSelectIndex == 0) { checkChecked(mSelectIndex); mSelectIndex = mSelectIndex + 1; setActionTitle(); } else { mViewPager.setCurrentItem(mSelectIndex, false); } } private void setActionTitle() { String title = "%s/%s"; if (mActionBar != null) { mActionBar.setTitle(String.format(title, mSelectIndex, mPhotos.size())); } } @Override protected void initEvent() { mCbSelected.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.cb_photo_select: Photo currentPhoto = mPhotos.get(mViewPager.getCurrentItem()); if (mSelectedPhotos.contains(currentPhoto)) { mSelectedPhotos.remove(currentPhoto); } else { if (mSelectedPhotos.size() >= mMaxSelectPhotoCount) { String maxTip = getString(R.string.tip_max_photo_select); Toast.makeText(PhotoPreviewActivity.this, MessageFormat.format(maxTip, mMaxSelectPhotoCount), Toast.LENGTH_SHORT).show(); mCbSelected.setChecked(false); return; } mSelectedPhotos.add(currentPhoto); } break; } } private void checkChecked(int position) { final Photo photo = mPhotos.get(position); mCbSelected.setChecked(mSelectedPhotos.contains(photo)); } @Override public boolean onSupportNavigateUp() { onBack(); return super.onSupportNavigateUp(); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { onBack(); } return super.onKeyDown(keyCode, event); } private void onBack() { Intent intent = new Intent(); intent.putParcelableArrayListExtra(BUNDLE_SELECTED_PHOTOS, mSelectedPhotos); setResult(RESULT_OK, intent); } }