1 /* 2 * Copyright 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.receivecontent; 18 19 import android.net.Uri; 20 import android.view.LayoutInflater; 21 import android.view.ViewGroup; 22 23 import androidx.annotation.NonNull; 24 import androidx.appcompat.widget.AppCompatImageView; 25 import androidx.recyclerview.widget.RecyclerView; 26 27 import java.util.ArrayList; 28 import java.util.Collection; 29 import java.util.List; 30 31 final class AttachmentsRecyclerViewAdapter extends 32 RecyclerView.Adapter<AttachmentsRecyclerViewAdapter.MyViewHolder> { 33 34 static final class MyViewHolder extends RecyclerView.ViewHolder { 35 public AppCompatImageView mAttachmentThumbnailView; 36 MyViewHolder(AppCompatImageView attachmentThumbnailView)37 MyViewHolder(AppCompatImageView attachmentThumbnailView) { 38 super(attachmentThumbnailView); 39 mAttachmentThumbnailView = attachmentThumbnailView; 40 } 41 } 42 43 private final List<Uri> mAttachments; 44 AttachmentsRecyclerViewAdapter(List<Uri> attachments)45 AttachmentsRecyclerViewAdapter(List<Uri> attachments) { 46 mAttachments = new ArrayList<>(attachments); 47 } 48 addAttachment(Uri uri)49 public void addAttachment(Uri uri) { 50 mAttachments.add(uri); 51 } 52 addAttachments(Collection<Uri> uris)53 public void addAttachments(Collection<Uri> uris) { 54 mAttachments.addAll(uris); 55 } 56 clearAttachments()57 public void clearAttachments() { 58 mAttachments.clear(); 59 } 60 61 @Override getItemCount()62 public int getItemCount() { 63 return mAttachments.size(); 64 } 65 66 @NonNull 67 @Override onCreateViewHolder(ViewGroup parent, int viewType)68 public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 69 AppCompatImageView view = (AppCompatImageView) LayoutInflater.from(parent.getContext()) 70 .inflate(R.layout.attachment, parent, false); 71 return new MyViewHolder(view); 72 } 73 74 @Override onBindViewHolder(@onNull MyViewHolder holder, int position)75 public void onBindViewHolder(@NonNull MyViewHolder holder, int position) { 76 Uri uri = mAttachments.get(position); 77 holder.mAttachmentThumbnailView.setImageURI(uri); 78 holder.mAttachmentThumbnailView.setClipToOutline(true); 79 } 80 } 81