Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

预览时,若没有选中图片,确定按钮可点击,默认当前预览图片为选中图片 #132

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,27 @@ 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;
private int columnNumber = DEFAULT_COLUMN_NUMBER;
private ArrayList<String> 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);

Expand All @@ -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);

Expand All @@ -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));
Expand All @@ -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() {
Expand All @@ -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);
Expand Down Expand Up @@ -168,6 +174,9 @@ public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.done) {
Intent intent = new Intent();
ArrayList<String> selectedPhotos = pickerFragment.getPhotoGridAdapter().getSelectedPhotoPaths();
if (selectedPhotos == null || selectedPhotos.size() <= 0) {
selectedPhotos = imagePagerFragment.getSelectedItem();
}
intent.putStringArrayListExtra(KEY_SELECTED_PHOTOS, selectedPhotos);
setResult(RESULT_OK, intent);
finish();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -295,6 +298,17 @@ public int getCurrentItem() {
return mViewPager.getCurrentItem();
}

public ArrayList<String> getSelectedItem() {
if ( paths != null && paths.size() > getCurrentItem() ){
String path = paths.get(getCurrentItem());
ArrayList<String> temp = new ArrayList<>();
temp.add(path);
return temp;
}

return null;
}

@Override public void onDestroy() {
super.onDestroy();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> originalPhotos) {
boolean previewEnable, int column, int maxCount, ArrayList<String> originalPhotos) {
Bundle args = new Bundle();
args.putBoolean(EXTRA_CAMERA, showCamera);
args.putBoolean(EXTRA_GIF, showGif);
Expand All @@ -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);
Expand All @@ -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<PhotoDirectory> dirs) {
directories.clear();
directories.addAll(dirs);
photoGridAdapter.notifyDataSetChanged();
listAdapter.notifyDataSetChanged();
adjustHeight();
}
});
new MediaStoreHelper.PhotosResultCallback() {
@Override
public void onResultCallback(List<PhotoDirectory> 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);
Expand All @@ -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);
Expand All @@ -156,23 +176,26 @@ 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<String> photos = photoGridAdapter.getCurrentPhotoPaths();

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);
Expand All @@ -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();
Expand All @@ -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) {
Expand All @@ -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();
}
Expand All @@ -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) {
Expand All @@ -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);
}
Expand All @@ -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) {
Expand Down