我有一个活动,允许用户选择约500张图像,然后转到下一个活动,这些图像将在RecyclerView中填充,我应该如何使其全部工作,以实现流畅的用户体验和更好的性能。
目前,我正在使用意图将图像作为位图进行传递。
我正在使用photopicker库进行照片选择,这里允许用户选择多个图像,所有选定图像的文件路径都存储在字符串Arraylist中,该字符串在活动的onActivityResult中返回,我将结果存储在下面名为mArray的mUserSelectedPhotos字符串数组中的代码可供参考。
PhotoPickingActivity.java
mPickImagesButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PhotoPicker.builder()
.setPhotoCount(200)
.setShowCamera(true)
.setShowGif(true)
.setPreviewEnabled(true)
.start(ChoosePhotoBookImagesActivity.this, PhotoPicker.REQUEST_CODE);
}
});
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == PhotoPicker.REQUEST_CODE) {
if (data != null) {
mUserSelectedPhotos =
data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
}
}
}
单击下一步按钮,用户选择的图像字符串数组列表将通过意图传递到下一个活动。
PhotoPickingActivity.java
mNextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(PhotoPickingActivity.this, PhotoBookListViewActivity.class);
intent.putStringArrayListExtra("USER_SELECTED_IMAGES", mUserSelectedPhotos);
startActivity(intent);
}
});
在包含recyclerview的下一个活动中,我正在此活动中接收发送的意图,并且在意图中接收的数据是字符串的数组列表,其中包含用户在上一个活动中选择的图像的文件路径。 / p>
PhotoBookListViewActivity.java
Intent intent = getIntent();
mReceivedImageList = intent.getStringArrayListExtra("USER_SELECTED_IMAGES");
我正在阅读mReceivedImageList并将文件路径转换为位图,并将其存储在新的位图数组列表中。
PhotoBookListViewActivity.java
final ArrayList<PhotoBookViewModel> bitmapImages = new ArrayList<PhotoBookViewModel>();
for (int i = 0; i < mImageList.size(); i++) {
mPhotoUrl = mImageList.get(i).toString();
Bitmap myBitmap = BitmapFactory.decodeFile(mPhotoUrl);
bitmapImages.add(new PhotoBookViewModel(myBitmap));
}
将bitmapImages arraylist(这是位图图像的列表)传递给适配器。
PhotoBookListViewActivity.java
mPhotoBookViewAdapter = new PhotoBookViewAdapter(PhotoBookListViewActivity.this, bitmapImages);
mPhotoBookViewRecyclerView.setAdapter(mPhotoBookViewAdapter);
mPhotoBookViewRecyclerView.setLayoutManager(new LinearLayoutManager(PhotoBookListViewActivity.this, LinearLayoutManager.VERTICAL, false));
PhotoBookViewModel.java
import android.graphics.Bitmap;
public class PhotoBookViewModel {
private Bitmap mImageRes;
public PhotoBookViewModel(Bitmap imageRes) {
this.mImageRes = imageRes;
}
public Bitmap getImageRes() {
return mImageRes;
}
}
PhotoBookViewAdapter.java
public class PhotoBookViewAdapter extends RecyclerView.Adapter<PhotoBookViewAdapter.PhotoViewHolder> {
private ArrayList<PhotoBookViewModel> mData;
private LayoutInflater mLayoutInflater;
private View mImageItemView;
private ArrayList<String> mSelectedPhotos;
private Bitmap mBitmap;
private Context mContext;
public class PhotoViewHolder extends RecyclerView.ViewHolder {
private ImageView mImageView;
public PhotoViewHolder(@NonNull View itemView) {
super(itemView);
mImageView = itemView.findViewById(R.id.imageview);
}
public void setData(PhotoBookViewModel currentObj) {
this.mImageView.setImageBitmap(currentObj.getImageRes());
}
}
public PhotoBookViewAdapter(Context context, ArrayList<String> selectedPhotos) {
this.mContext = context;
this.mSelectedPhotos = selectedPhotos;
mBitmap = BitmapFactory.decodeFile (mSelectedPhotos.get(0).toString());
}
public PhotoBookViewAdapter(Context context, ArrayList<PhotoBookViewModel> mData) {
this.mData = mData;
mLayoutInflater = LayoutInflater.from(context);
}
@NonNull
@Override
public PhotoViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
mImageItemView = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.photo_book_view_image_item, viewGroup, false);
PhotoViewHolder photoViewHolder = new PhotoViewHolder(mImageItemView);
mContext = viewGroup.getContext();
return photoViewHolder;
}
@Override
public void onBindViewHolder(@NonNull PhotoViewHolder photoViewHolder, int position) {
PhotoBookViewModel currentObj = mData.get(position);
photoViewHolder.setData(currentObj);;
}
@Override
public int getItemCount() {
return mData.size();
}
}