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.util.ArrayList; import android.app.Activity; import android.content.DialogInterface; import android.content.Intent; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AlertDialog; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.ProgressBar; 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/2/20 ?8:48 */ public class PhotoSelectedPreviewActivity extends BaseActivity { public static final String BUNDLE_SELECTED_PHOTOS = "bundle_selected_photos"; public static final String BUNDLE_SELECT_INDEX = "bundle_select_index"; public static void luanch(Activity activity, ArrayList<Photo> selectedPhotos, int selectIndex, int requsetCode) { Intent intent = new Intent(activity, PhotoSelectedPreviewActivity.class); intent.putParcelableArrayListExtra(BUNDLE_SELECTED_PHOTOS, selectedPhotos); intent.putExtra(BUNDLE_SELECT_INDEX, selectIndex); activity.startActivityForResult(intent, requsetCode); } private ViewPager mViewPager; private PagerAdapter mPagerAdapter; private ArrayList<Photo> mSelectedPhotos; private int mSelectIndex; @Override protected int getContentLayoutRes() { return R.layout.activity_photo_selected_preview; } @Override protected void initView() { mViewPager = getView(R.id.viewpage); } @Override protected void initData() { if (getIntent() != null) { Intent intent = getIntent(); mSelectedPhotos = intent.getParcelableArrayListExtra(BUNDLE_SELECTED_PHOTOS); mSelectIndex = intent.getIntExtra(BUNDLE_SELECT_INDEX, 0); } if (mSelectedPhotos == null || mSelectedPhotos.isEmpty()) { throw new IllegalStateException("selectPhotos must > 0"); } mPagerAdapter = new PagerAdapter() { @Override public int getCount() { return mSelectedPhotos.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 int getItemPosition(Object object) { return POSITION_NONE; } @Override public Object instantiateItem(ViewGroup container, int position) { View rootView = LayoutInflater.from(PhotoSelectedPreviewActivity.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 = mSelectedPhotos.get(position); Picasso.with(PhotoSelectedPreviewActivity.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) { mSelectIndex = position + 1; setActionTitle(); } @Override public void onPageScrollStateChanged(int state) { } }); if (mSelectIndex == 0) { mSelectIndex = mSelectIndex + 1; setActionTitle(); } else { mViewPager.setCurrentItem(mSelectIndex, false); } } private void setActionTitle() { String title = "%s/%s"; if (getSupportActionBar() != null) { getSupportActionBar().setTitle(String.format(title, mSelectIndex, mSelectedPhotos.size())); } } @Override protected void initEvent() { } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_photo_selected_preview, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == R.id.action_del_photo) { confirmDelete(); return false; } return super.onOptionsItemSelected(item); } private void confirmDelete() { if (mSelectedPhotos == null || mSelectedPhotos.isEmpty()) { onBackPressed(); return; } AlertDialog.Builder dialog = new AlertDialog.Builder(this); dialog.setTitle(R.string.dialog_confirm_title); dialog.setMessage(R.string.dialog_confirm_del); dialog.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { int currentIndex = mViewPager.getCurrentItem(); mSelectedPhotos.remove(currentIndex); mSelectIndex = currentIndex + 1; if (mSelectedPhotos.isEmpty()) { onBackPressed(); return; } mPagerAdapter.notifyDataSetChanged(); setActionTitle(); } }); dialog.setNegativeButton(android.R.string.no, null); dialog.show(); } @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); } }