From a01c9405346513b1175faec8ddeb50d556cbb1b3 Mon Sep 17 00:00:00 2001 From: lion Date: Wed, 28 Sep 2016 11:49:18 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A2=84=E8=A7=88=E6=97=B6=EF=BC=8C=E8=8B=A5?= =?UTF-8?q?=E6=B2=A1=E6=9C=89=E9=80=89=E4=B8=AD=E5=9B=BE=E7=89=87=EF=BC=8C?= =?UTF-8?q?=E7=A1=AE=E5=AE=9A=E6=8C=89=E9=92=AE=E5=8F=AF=E7=82=B9=E5=87=BB?= =?UTF-8?q?=EF=BC=8C=E9=BB=98=E8=AE=A4=E5=BD=93=E5=89=8D=E9=A2=84=E8=A7=88?= =?UTF-8?q?=E5=9B=BE=E7=89=87=E4=B8=BA=E9=80=89=E4=B8=AD=E5=9B=BE=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../iwf/photopicker/PhotoPickerActivity.java | 45 ++++++---- .../fragment/ImagePagerFragment.java | 14 +++ .../fragment/PhotoPickerFragment.java | 89 +++++++++++++------ 3 files changed, 104 insertions(+), 44 deletions(-) diff --git a/PhotoPicker/src/main/java/me/iwf/photopicker/PhotoPickerActivity.java b/PhotoPicker/src/main/java/me/iwf/photopicker/PhotoPickerActivity.java index 9856740..aca1574 100644 --- a/PhotoPicker/src/main/java/me/iwf/photopicker/PhotoPickerActivity.java +++ b/PhotoPicker/src/main/java/me/iwf/photopicker/PhotoPickerActivity.java @@ -33,11 +33,13 @@ public class PhotoPickerActivity extends AppCompatActivity { private PhotoPickerFragment pickerFragment; private ImagePagerFragment imagePagerFragment; - private MenuItem menuDoneItem; + public MenuItem menuDoneItem; private int maxCount = DEFAULT_MAX_COUNT; - /** to prevent multiple calls to inflate menu */ + /** + * to prevent multiple calls to inflate menu + */ private boolean menuIsInflated = false; private boolean showGif = false; @@ -45,12 +47,13 @@ public class PhotoPickerActivity extends AppCompatActivity { private ArrayList originalPhotos = null; - @Override protected void onCreate(Bundle savedInstanceState) { + @Override + protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - boolean showCamera = getIntent().getBooleanExtra(EXTRA_SHOW_CAMERA, true); - boolean showGif = getIntent().getBooleanExtra(EXTRA_SHOW_GIF, false); - boolean previewEnabled = getIntent().getBooleanExtra(EXTRA_PREVIEW_ENABLED, true); + boolean showCamera = getIntent().getBooleanExtra(EXTRA_SHOW_CAMERA, true); + boolean showGif = getIntent().getBooleanExtra(EXTRA_SHOW_GIF, false); + boolean previewEnabled = getIntent().getBooleanExtra(EXTRA_PREVIEW_ENABLED, true); setShowGif(showGif); @@ -75,16 +78,17 @@ public class PhotoPickerActivity extends AppCompatActivity { pickerFragment = (PhotoPickerFragment) getSupportFragmentManager().findFragmentByTag("tag"); if (pickerFragment == null) { pickerFragment = PhotoPickerFragment - .newInstance(showCamera, showGif, previewEnabled, columnNumber, maxCount, originalPhotos); + .newInstance(showCamera, showGif, previewEnabled, columnNumber, maxCount, originalPhotos); getSupportFragmentManager() - .beginTransaction() - .replace(R.id.container, pickerFragment, "tag") - .commit(); + .beginTransaction() + .replace(R.id.container, pickerFragment, "tag") + .commit(); getSupportFragmentManager().executePendingTransactions(); } pickerFragment.getPhotoGridAdapter().setOnItemCheckListener(new OnItemCheckListener() { - @Override public boolean OnItemCheck(int position, Photo photo, final boolean isCheck, int selectedItemCount) { + @Override + public boolean OnItemCheck(int position, Photo photo, final boolean isCheck, int selectedItemCount) { int total = selectedItemCount + (isCheck ? -1 : 1); @@ -101,7 +105,7 @@ public class PhotoPickerActivity extends AppCompatActivity { if (total > maxCount) { Toast.makeText(getActivity(), getString(R.string.__picker_over_max_count_tips, maxCount), - LENGTH_LONG).show(); + LENGTH_LONG).show(); return false; } menuDoneItem.setTitle(getString(R.string.__picker_done_with_count, total, maxCount)); @@ -116,7 +120,8 @@ public class PhotoPickerActivity extends AppCompatActivity { * Overriding this method allows us to run our exit animation first, then exiting * the activity when it complete. */ - @Override public void onBackPressed() { + @Override + public void onBackPressed() { if (imagePagerFragment != null && imagePagerFragment.isVisible()) { imagePagerFragment.runExitAnimation(new Runnable() { public void run() { @@ -134,13 +139,14 @@ public void run() { public void addImagePagerFragment(ImagePagerFragment imagePagerFragment) { this.imagePagerFragment = imagePagerFragment; getSupportFragmentManager() - .beginTransaction() - .replace(R.id.container, this.imagePagerFragment) - .addToBackStack(null) - .commit(); + .beginTransaction() + .replace(R.id.container, this.imagePagerFragment) + .addToBackStack(null) + .commit(); } - @Override public boolean onCreateOptionsMenu(Menu menu) { + @Override + public boolean onCreateOptionsMenu(Menu menu) { if (!menuIsInflated) { getMenuInflater().inflate(R.menu.__picker_menu_picker, menu); menuDoneItem = menu.findItem(R.id.done); @@ -168,6 +174,9 @@ public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == R.id.done) { Intent intent = new Intent(); ArrayList selectedPhotos = pickerFragment.getPhotoGridAdapter().getSelectedPhotoPaths(); + if (selectedPhotos == null || selectedPhotos.size() <= 0) { + selectedPhotos = imagePagerFragment.getSelectedItem(); + } intent.putStringArrayListExtra(KEY_SELECTED_PHOTOS, selectedPhotos); setResult(RESULT_OK, intent); finish(); diff --git a/PhotoPicker/src/main/java/me/iwf/photopicker/fragment/ImagePagerFragment.java b/PhotoPicker/src/main/java/me/iwf/photopicker/fragment/ImagePagerFragment.java index 6ec710c..9ffe58b 100644 --- a/PhotoPicker/src/main/java/me/iwf/photopicker/fragment/ImagePagerFragment.java +++ b/PhotoPicker/src/main/java/me/iwf/photopicker/fragment/ImagePagerFragment.java @@ -12,14 +12,17 @@ import android.view.ViewTreeObserver; import android.view.animation.AccelerateInterpolator; import android.view.animation.DecelerateInterpolator; + import com.bumptech.glide.Glide; import com.nineoldandroids.animation.Animator; import com.nineoldandroids.animation.ObjectAnimator; import com.nineoldandroids.view.ViewHelper; import com.nineoldandroids.view.ViewPropertyAnimator; + import java.util.ArrayList; import java.util.Arrays; import java.util.List; + import me.iwf.photopicker.R; import me.iwf.photopicker.adapter.PhotoPagerAdapter; @@ -295,6 +298,17 @@ public int getCurrentItem() { return mViewPager.getCurrentItem(); } + public ArrayList getSelectedItem() { + if ( paths != null && paths.size() > getCurrentItem() ){ + String path = paths.get(getCurrentItem()); + ArrayList temp = new ArrayList<>(); + temp.add(path); + return temp; + } + + return null; + } + @Override public void onDestroy() { super.onDestroy(); diff --git a/PhotoPicker/src/main/java/me/iwf/photopicker/fragment/PhotoPickerFragment.java b/PhotoPicker/src/main/java/me/iwf/photopicker/fragment/PhotoPickerFragment.java index 7e17ad2..4901ddb 100644 --- a/PhotoPicker/src/main/java/me/iwf/photopicker/fragment/PhotoPickerFragment.java +++ b/PhotoPicker/src/main/java/me/iwf/photopicker/fragment/PhotoPickerFragment.java @@ -68,7 +68,7 @@ public class PhotoPickerFragment extends Fragment { private RequestManager mGlideRequestManager; public static PhotoPickerFragment newInstance(boolean showCamera, boolean showGif, - boolean previewEnable, int column, int maxCount, ArrayList originalPhotos) { + boolean previewEnable, int column, int maxCount, ArrayList originalPhotos) { Bundle args = new Bundle(); args.putBoolean(EXTRA_CAMERA, showCamera); args.putBoolean(EXTRA_GIF, showGif); @@ -81,7 +81,8 @@ public static PhotoPickerFragment newInstance(boolean showCamera, boolean showGi return fragment; } - @Override public void onCreate(Bundle savedInstanceState) { + @Override + public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRetainInstance(true); @@ -104,26 +105,44 @@ public static PhotoPickerFragment newInstance(boolean showCamera, boolean showGi boolean showGif = getArguments().getBoolean(EXTRA_GIF); mediaStoreArgs.putBoolean(EXTRA_SHOW_GIF, showGif); MediaStoreHelper.getPhotoDirs(getActivity(), mediaStoreArgs, - new MediaStoreHelper.PhotosResultCallback() { - @Override public void onResultCallback(List dirs) { - directories.clear(); - directories.addAll(dirs); - photoGridAdapter.notifyDataSetChanged(); - listAdapter.notifyDataSetChanged(); - adjustHeight(); - } - }); + new MediaStoreHelper.PhotosResultCallback() { + @Override + public void onResultCallback(List dirs) { + directories.clear(); + directories.addAll(dirs); + photoGridAdapter.notifyDataSetChanged(); + listAdapter.notifyDataSetChanged(); + adjustHeight(); + } + }); captureManager = new ImageCaptureManager(getActivity()); } + private void enableMenuPicker(boolean enable) { + if (getActivity() instanceof PhotoPickerActivity && + ((PhotoPickerActivity) getActivity()).menuDoneItem != null) { + ((PhotoPickerActivity) getActivity()).menuDoneItem.setEnabled(enable); + } + } - @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { + private void checkMenuPickerShouldEnable() { + if (photoGridAdapter != null) { + if (photoGridAdapter.getSelectedItemCount() <= 0) { + enableMenuPicker(false); + } + } else { + enableMenuPicker(false); + } + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { final View rootView = inflater.inflate(R.layout.__picker_fragment_photo_picker, container, false); - listAdapter = new PopupDirectoryListAdapter(mGlideRequestManager, directories); + listAdapter = new PopupDirectoryListAdapter(mGlideRequestManager, directories); RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.rv_photos); StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(column, OrientationHelper.VERTICAL); @@ -143,7 +162,8 @@ public static PhotoPickerFragment newInstance(boolean showCamera, boolean showGi //listPopupWindow.setAnimationStyle(R.style.Animation_AppCompat_DropDownUp); listPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() { - @Override public void onItemClick(AdapterView parent, View view, int position, long id) { + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) { listPopupWindow.dismiss(); PhotoDirectory directory = directories.get(position); @@ -156,7 +176,8 @@ public static PhotoPickerFragment newInstance(boolean showCamera, boolean showGi }); photoGridAdapter.setOnPhotoClickListener(new OnPhotoClickListener() { - @Override public void onClick(View v, int position, boolean showCamera) { + @Override + public void onClick(View v, int position, boolean showCamera) { final int index = showCamera ? position - 1 : position; List photos = photoGridAdapter.getCurrentPhotoPaths(); @@ -164,15 +185,17 @@ public static PhotoPickerFragment newInstance(boolean showCamera, boolean showGi int[] screenLocation = new int[2]; v.getLocationOnScreen(screenLocation); ImagePagerFragment imagePagerFragment = - ImagePagerFragment.newInstance(photos, index, screenLocation, v.getWidth(), - v.getHeight()); + ImagePagerFragment.newInstance(photos, index, screenLocation, v.getWidth(), + v.getHeight()); ((PhotoPickerActivity) getActivity()).addImagePagerFragment(imagePagerFragment); + enableMenuPicker(true); } }); photoGridAdapter.setOnCameraClickListener(new OnClickListener() { - @Override public void onClick(View view) { + @Override + public void onClick(View view) { try { Intent intent = captureManager.dispatchTakePictureIntent(); startActivityForResult(intent, ImageCaptureManager.REQUEST_TAKE_PHOTO); @@ -183,7 +206,8 @@ public static PhotoPickerFragment newInstance(boolean showCamera, boolean showGi }); btSwitchDirectory.setOnClickListener(new OnClickListener() { - @Override public void onClick(View v) { + @Override + public void onClick(View v) { if (listPopupWindow.isShowing()) { listPopupWindow.dismiss(); @@ -196,7 +220,8 @@ public static PhotoPickerFragment newInstance(boolean showCamera, boolean showGi recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { - @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { + @Override + public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); // Log.d(">>> Picker >>>", "dy = " + dy); if (Math.abs(dy) > SCROLL_THRESHOLD) { @@ -205,7 +230,9 @@ public static PhotoPickerFragment newInstance(boolean showCamera, boolean showGi resumeRequestsIfNotDestroyed(); } } - @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { + + @Override + public void onScrollStateChanged(RecyclerView recyclerView, int newState) { if (newState == RecyclerView.SCROLL_STATE_IDLE) { resumeRequestsIfNotDestroyed(); } @@ -215,7 +242,8 @@ public static PhotoPickerFragment newInstance(boolean showCamera, boolean showGi return rootView; } - @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { + @Override + public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == ImageCaptureManager.REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) { if (captureManager == null) { @@ -240,13 +268,15 @@ public PhotoGridAdapter getPhotoGridAdapter() { } - @Override public void onSaveInstanceState(Bundle outState) { + @Override + public void onSaveInstanceState(Bundle outState) { captureManager.onSaveInstanceState(outState); super.onSaveInstanceState(outState); } - @Override public void onViewStateRestored(Bundle savedInstanceState) { + @Override + public void onViewStateRestored(Bundle savedInstanceState) { captureManager.onRestoreInstanceState(savedInstanceState); super.onViewStateRestored(savedInstanceState); } @@ -264,7 +294,14 @@ public void adjustHeight() { } } - @Override public void onDestroy() { + @Override + public void onResume() { + super.onResume(); + checkMenuPickerShouldEnable(); + } + + @Override + public void onDestroy() { super.onDestroy(); if (directories == null) {