【发布时间】:2020-06-18 14:12:10
【问题描述】:
我有一个ReyclerView,它显示了用户手机上的所有图像。 I only want to allow one image to be selected at a time, and when the image is selected, a border appears around that image indicating the selection.
我已准备好所有内容,包括将出现在图像周围的边框,但是,我似乎无法找到将此背景应用于所选图像的方法。 onPhotoClick 有效,它成功识别出所选图像。但是我不确定从这里开始一次只将背景应用于一张图像。我只有路径。
这是我的代码:
SelectFileActivity.java
public class SelectFileActivity extends AppCompatActivity {
RecyclerView recyclerView;
GalleryAdapter galleryAdapter;
List < String > images;
private static final int REQUEST_CODE_STORAGE_PERMISSION = 101;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_file);
// Remove status bar
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
// Initialize content
recyclerView = findViewById(R.id.recyclerview_gallery_images);
// Check for permission
if (ContextCompat.checkSelfPermission(SelectFileActivity.this,
Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(SelectFileActivity.this,
new String[] {
Manifest.permission.READ_EXTERNAL_STORAGE
}, REQUEST_CODE_STORAGE_PERMISSION);
} else {
loadImages();
}
}
@Override
public void onBackPressed() {
startActivity(new Intent(this, HomeActivity.class));
finish();
}
private void loadImages() {
// All images will be same size
recyclerView.setHasFixedSize(true);
// Set the number of pictures per a row
recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
images = SelectImagesGallery.listOfImages(this);
galleryAdapter = new GalleryAdapter(this, images, new GalleryAdapter.PhotoListener() {
@Override
public void onPhotoClick(String path) {
// Highlight the selected photo with a border
Drawable highlight = getResources().getDrawable(R.drawable.background_highlight_border);
I DO NOT KNOW WHAT TO PUT HERE.setBackground(highlight);
Toast.makeText(SelectFileActivity.this, "" + path, Toast.LENGTH_SHORT).show();
// Do something with the selected photo
}
});
recyclerView.setAdapter(galleryAdapter);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_CODE_STORAGE_PERMISSION) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
loadImages();
} else {
Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
}
}
}
GalleryAdapter.java
public class GalleryAdapter extends RecyclerView.Adapter < GalleryAdapter.ViewHolder > {
private Context context;
private List < String > images;
protected PhotoListener photoListener;
public GalleryAdapter(Context context, List < String > images, PhotoListener photoListener) {
this.context = context;
this.images = images;
this.photoListener = photoListener;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ViewHolder(
LayoutInflater.from(context).inflate(R.layout.gallery_item, parent, false)
);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
String image = images.get(position);
// Load images to Glide
Glide.with(context)
.load(image)
.transform(new CenterCrop(), new RoundedCorners(30))
.into(holder.image);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
photoListener.onPhotoClick(image);
}
});
}
@Override
public int getItemCount() {
return images.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView image;
public ViewHolder(@NonNull View itemView) {
super(itemView);
image = itemView.findViewById(R.id.image);
}
}
public interface PhotoListener {
void onPhotoClick(String path);
}
}
【问题讨论】:
标签: java android android-layout