use of com.lzy.imagepicker.ui.ImageBaseActivity in project ImagePicker by jeasonlzy.
the class ImageGridAdapter method getView.
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
int itemViewType = getItemViewType(position);
if (itemViewType == ITEM_TYPE_CAMERA) {
convertView = LayoutInflater.from(mActivity).inflate(R.layout.adapter_camera_item, parent, false);
//让图片是个正方形
convertView.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mImageSize));
convertView.setTag(null);
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!((ImageBaseActivity) mActivity).checkPermission(Manifest.permission.CAMERA)) {
ActivityCompat.requestPermissions(mActivity, new String[] { Manifest.permission.CAMERA }, ImageGridActivity.REQUEST_PERMISSION_CAMERA);
} else {
imagePicker.takePicture(mActivity, ImagePicker.REQUEST_CODE_TAKE);
}
}
});
} else {
final ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(mActivity).inflate(R.layout.adapter_image_list_item, parent, false);
//让图片是个正方形
convertView.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mImageSize));
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final ImageItem imageItem = getItem(position);
holder.ivThumb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null)
listener.onImageItemClick(holder.rootView, imageItem, position);
}
});
holder.cbCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int selectLimit = imagePicker.getSelectLimit();
if (holder.cbCheck.isChecked() && mSelectedImages.size() >= selectLimit) {
Toast.makeText(mActivity.getApplicationContext(), mActivity.getString(R.string.select_limit, selectLimit), Toast.LENGTH_SHORT).show();
holder.cbCheck.setChecked(false);
holder.mask.setVisibility(View.GONE);
} else {
imagePicker.addSelectedImageItem(position, imageItem, holder.cbCheck.isChecked());
holder.mask.setVisibility(View.VISIBLE);
}
}
});
//根据是否多选,显示或隐藏checkbox
if (imagePicker.isMultiMode()) {
holder.cbCheck.setVisibility(View.VISIBLE);
boolean checked = mSelectedImages.contains(imageItem);
if (checked) {
holder.mask.setVisibility(View.VISIBLE);
holder.cbCheck.setChecked(true);
} else {
holder.mask.setVisibility(View.GONE);
holder.cbCheck.setChecked(false);
}
} else {
holder.cbCheck.setVisibility(View.GONE);
}
//显示图片
imagePicker.getImageLoader().displayImage(mActivity, imageItem.path, holder.ivThumb, mImageSize, mImageSize);
}
return convertView;
}
Aggregations