从ArrayList动态地在recyclerview中加载图像

时间:2019-03-27 09:52:34

标签: java android

在我的应用程序中,我定义了一个ArrayList,其默认元素为空。

然后用户可以动态选择图像文件或拍照,然后结果将被容纳到那些ArrayList中。

问题是已经包含这些元素的ArrayList显示给使用CardView的RecylerView无响应。如果向上或向下滚动,则会有延迟。

我真的很感谢所有帮助。

活动

private ArrayList<Image> images = new ArrayList<>();
private ImageAdapter imageAdapter;
private Uri mCapturedImageURI;
private RecyclerView recyclerView;

public class IncomingLokalActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_incoming_lokal);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        recyclerView = (RecyclerView) findViewById(R.id.main_list_view);
        imageAdapter = new ImageAdapter(this, images);

        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setAdapter(imageAdapter);
        recyclerView.setHasFixedSize(true);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) { //taken from camera
            String[] projection = {MediaStore.Images.Media.DATA};
            Cursor cursor =
                    managedQuery(mCapturedImageURI, projection, null,
                            null, null);
            int column_index_data = cursor.getColumnIndexOrThrow(
                    MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            String picturePath = cursor.getString(column_index_data);

            preparedObjectDataImage("_file", "Taken From Camera", picturePath);

        } else if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { //taken from gallery

            DzilFileHelper dzilFileHelper = new DzilFileHelper();

            String[] filePathColumn = {MediaStore.Images.Media.DATA};
            if (data.getClipData() != null) {
                int count = data.getClipData().getItemCount(); //evaluate the count before the for loop --- otherwise, the count is evaluated every loop.
                Cursor cursor;
                for (int i = 0; i < count; i++) {
                    Uri selectedImage;
                    selectedImage = data.getClipData().getItemAt(i).getUri();
                    cursor = getContentResolver()
                            .query(selectedImage, filePathColumn, null, null,
                                    null);
                    cursor.moveToFirst();

                    cursor.close();
                    preparedObjectDataImage("_file", "Taken From Gallery Multi", dzilFileHelper.getPathFromURI(this, selectedImage));
                }
            } else if (data.getData() != null) {
                Uri selectedImage = data.getData();

                Cursor cursor = getContentResolver()
                        .query(selectedImage, filePathColumn, null, null,
                                null);
                cursor.moveToFirst();
                cursor.close();
                preparedObjectDataImage("_file", "Taken From Gallery", dzilFileHelper.getPathFromURI(this, selectedImage));

            }
        }
    }
}

处理图像对象

private void preparedObjectDataImage(String title, String description, String picturePath) {
    Image image = new Image();
    image.setTitle(title);
    image.setDescription(description);
    image.setDatetime(System.currentTimeMillis());
    image.setPath(picturePath);
    images.add(image);
    imageAdapter.notifyDataSetChanged(); // set new item to recylerview
}

在我的适配器中,我还将图像设置为缩略图格式,但是滚动时效果仍然不佳。

适配器

public class ImageAdapter extends RecyclerView.Adapter<ImageViewHolder> {

    Context c;
    ArrayList<Image> images;

    public ImageAdapter(Context c, ArrayList<Image> images) {
        this.c = c;
        this.images = images;
    }

    @Override
    public ImageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(c).inflate(R.layout.item_image, parent, false);
        return new ImageViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ImageViewHolder holder, int position) {
        final int THUMBSIZE = 96;
        Image image = images.get(position);
        holder.nameTxt.setText(image.getPath());

        // Resize to thumbnail
        holder.img.setImageBitmap(ThumbnailUtils
                .extractThumbnail(BitmapFactory.decodeFile(image.getPath()),
                        THUMBSIZE, THUMBSIZE));
    }

    @Override
    public int getItemCount() {
        return images.size();
    }
}

持有人

public class ImageViewHolder extends RecyclerView.ViewHolder {

    TextView nameTxt;
    ImageView img;

    public ImageViewHolder(View itemView) {
        super(itemView);

        nameTxt = (TextView) itemView.findViewById(R.id.item_img_infor);
        img = (ImageView) itemView.findViewById(R.id.item_img_icon);

    }
}

2 个答案:

答案 0 :(得分:0)

将您的观看者定为决赛

 final Holder holder;

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.itemlayoutforscheduledmeetings, null);
        holder = new Holder(convertView);
        convertView.setTag(holder);


    } else {
        holder = (Holder) convertView.getTag();
    }

答案 1 :(得分:0)

在滑行时表现出色。

   @Override
public void onBindViewHolder(ImageViewHolder holder, int position) {

    final Image image = images.get(position);
    holder.nameTxt.setText(image.getPath());

    //Handle to Glide
    Glide.with(holder.itemView.getContext()).load(new File(image.getPath())).into(holder.img);

谢谢...

}