Skip to content

Commit

Permalink
提供在打开选择器时传入原来已经选择过的图片,使这些图片在打开选择器的时候默认为选中状态。
Browse files Browse the repository at this point in the history
  • Loading branch information
梁任彦 committed Jan 20, 2018
1 parent 1ca4bee commit c99e148
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,16 @@ public void onClick(View v) {
case R.id.btn_limit:
//多选(最多9张)
ImageSelectorUtils.openPhoto(MainActivity.this, REQUEST_CODE, false, 9);
// ImageSelectorUtils.openPhoto(MainActivity.this, REQUEST_CODE, false, 9, mAdapter.getImages()); // 把已选的传入。
break;

case R.id.btn_unlimited:
//多选(不限数量)
ImageSelectorUtils.openPhoto(MainActivity.this, REQUEST_CODE);
// ImageSelectorUtils.openPhoto(MainActivity.this, REQUEST_CODE, mAdapter.getImages()); // 把已选的传入。
//或者
// ImageSelectorUtils.openPhoto(MainActivity.this, REQUEST_CODE, false, 0);
// ImageSelectorUtils.openPhoto(MainActivity.this, REQUEST_CODE, false, 0, mAdapter.getImages()); // 把已选的传入。
break;

case R.id.btn_clip:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public ImageAdapter(Context context) {
this.mInflater = LayoutInflater.from(mContext);
}

public ArrayList<String> getImages() {
return mImages;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.adapter_image, parent, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
Expand Down Expand Up @@ -77,19 +78,26 @@ public void run() {
}
};

//用于接收从外面传进来的已选择的图片列表。当用户原来已经有选择过图片,现在重新打开选择器,允许用
// 户把先前选过的图片传进来,并把这些图片默认为选中状态。
private ArrayList<String> mSelectedImages;

/**
* 启动图片选择器
*
* @param activity
* @param requestCode
* @param isSingle 是否单选
* @param maxSelectCount 图片的最大选择数量,小于等于0时,不限数量,isSingle为false时才有用。
* @param selected 接收从外面传进来的已选择的图片列表。当用户原来已经有选择过图片,现在重新打开
* 选择器,允许用户把先前选过的图片传进来,并把这些图片默认为选中状态。
*/
public static void openActivity(Activity activity, int requestCode,
boolean isSingle, int maxSelectCount) {
boolean isSingle, int maxSelectCount, ArrayList<String> selected) {
Intent intent = new Intent(activity, ImageSelectorActivity.class);
intent.putExtra(Constants.MAX_SELECT_COUNT, maxSelectCount);
intent.putExtra(Constants.IS_SINGLE, isSingle);
intent.putStringArrayListExtra(Constants.SELECTED, selected);
activity.startActivityForResult(intent, requestCode);
}

Expand All @@ -101,6 +109,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
Intent intent = getIntent();
mMaxCount = intent.getIntExtra(Constants.MAX_SELECT_COUNT, 0);
isSingle = intent.getBooleanExtra(Constants.IS_SINGLE, false);
mSelectedImages = intent.getStringArrayListExtra(Constants.SELECTED);

setStatusBarColor();
initView();
Expand Down Expand Up @@ -529,6 +538,10 @@ public void run() {
if (mFolders != null && !mFolders.isEmpty()) {
initFolderList();
setFolder(mFolders.get(0));
if (mSelectedImages != null && mAdapter != null) {
mAdapter.setSelectedImages(mSelectedImages);
mSelectedImages = null;
}
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void onClick(View v) {
//如果是单选,就先清空已经选中的图片,再选中当前图片
clearImageSelect();
selectImage(image);
setItemSelect(holder,true);
setItemSelect(holder, true);
} else if (mMaxCount <= 0 || mSelectImages.size() < mMaxCount) {
//如果不限制图片的选中数量,或者图片的选中数量
// 还没有达到最大限制,就直接选中当前图片。
Expand All @@ -77,18 +77,19 @@ public void onClick(View v) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mItemClickListener != null){
mItemClickListener.OnItemClick(image,holder.getAdapterPosition());
if (mItemClickListener != null) {
mItemClickListener.OnItemClick(image, holder.getAdapterPosition());
}
}
});
}

/**
* 选中图片
*
* @param image
*/
private void selectImage(Image image){
private void selectImage(Image image) {
mSelectImages.add(image);
if (mSelectListener != null) {
mSelectListener.OnImageSelect(image, true, mSelectImages.size());
Expand All @@ -97,9 +98,10 @@ private void selectImage(Image image){

/**
* 取消选中图片
*
* @param image
*/
private void unSelectImage(Image image){
private void unSelectImage(Image image) {
mSelectImages.remove(image);
if (mSelectListener != null) {
mSelectListener.OnImageSelect(image, false, mSelectImages.size());
Expand Down Expand Up @@ -136,13 +138,43 @@ private void setItemSelect(ViewHolder holder, boolean isSelect) {
private void clearImageSelect() {
if (mImages != null && mSelectImages.size() == 1) {
int index = mImages.indexOf(mSelectImages.get(0));
if(index != -1){
if (index != -1) {
mSelectImages.clear();
notifyItemChanged(index);
}
}
}

public void setSelectedImages(ArrayList<String> selected) {
if (mImages != null && selected != null) {
for (String path : selected) {
if (isFull()) {
return;
}
for (Image image : mImages) {
if (path.equals(image.getPath())) {
if (!mSelectImages.contains(image)) {
mSelectImages.add(image);
}
break;
}
}
}
notifyDataSetChanged();
}
}


private boolean isFull() {
if (isSingle && mSelectImages.size() == 1) {
return true;
} else if (mMaxCount > 0 && mSelectImages.size() == mMaxCount) {
return true;
} else {
return false;
}
}

public ArrayList<Image> getSelectImages() {
return mSelectImages;
}
Expand Down Expand Up @@ -173,7 +205,7 @@ public interface OnImageSelectListener {
void OnImageSelect(Image image, boolean isSelect, int selectCount);
}

public interface OnItemClickListener{
public interface OnItemClickListener {
void OnItemClick(Image image, int position);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ public interface Constants {
String MAX_SELECT_COUNT = "max_select_count";
//是否单选
String IS_SINGLE = "is_single";
//原来已选择的图片
String SELECTED = "selected";
//初始位置
String POSITION = "position";

//初始位置
String IS_CONFIRM = "is_confirm";

int RESULT_CODE = 0x00000012;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.donkingliang.imageselector.ClipImageActivity;
import com.donkingliang.imageselector.ImageSelectorActivity;

import java.util.ArrayList;

/**
* 提供给外界相册的调用的工具类
*/
Expand All @@ -23,6 +25,18 @@ public static void openPhoto(Activity activity, int requestCode) {
openPhoto(activity, requestCode, false, 0);
}

/**
* 打开相册,选择图片,可多选,不限数量。
*
* @param activity
* @param requestCode
* @param selected 接收从外面传进来的已选择的图片列表。当用户原来已经有选择过图片,现在重新打开
* 选择器,允许用户把先前选过的图片传进来,并把这些图片默认为选中状态。
*/
public static void openPhoto(Activity activity, int requestCode, ArrayList<String> selected) {
openPhoto(activity, requestCode, false, 0, selected);
}

/**
* 打开相册,选择图片,可多选,限制最大的选择数量。
*
Expand All @@ -33,7 +47,22 @@ public static void openPhoto(Activity activity, int requestCode) {
*/
public static void openPhoto(Activity activity, int requestCode,
boolean isSingle, int maxSelectCount) {
ImageSelectorActivity.openActivity(activity, requestCode, isSingle, maxSelectCount);
openPhoto(activity, requestCode, isSingle, maxSelectCount, null);
}

/**
* 打开相册,选择图片,可多选,限制最大的选择数量。
*
* @param activity
* @param requestCode
* @param isSingle 是否单选
* @param maxSelectCount 图片的最大选择数量,小于等于0时,不限数量,isSingle为false时才有用。
* @param selected 接收从外面传进来的已选择的图片列表。当用户原来已经有选择过图片,现在重新打开
* 选择器,允许用户把先前选过的图片传进来,并把这些图片默认为选中状态。
*/
public static void openPhoto(Activity activity, int requestCode,
boolean isSingle, int maxSelectCount, ArrayList<String> selected) {
ImageSelectorActivity.openActivity(activity, requestCode, isSingle, maxSelectCount, selected);
}

/**
Expand Down

0 comments on commit c99e148

Please sign in to comment.