我有一个ReyclerView
,可以显示用户手机上的所有图像。我只希望允许在同一时间被选的一个图象,并且选择了图像时,会出现一个边界周围的图像显示的选择。
我已经准备好一切,包括将出现在图像周围的边框,但是,我似乎找不到找到将背景应用于所选图像的方法。 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);
}
}
答案 0 :(得分:1)
您不必在这里使用界面,因为在SelectFileActivity中没有所选图像视图的引用。但是您可以像这样轻松完成。将此代码添加到您的onClickListener
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
holder.image.setBackgroundResource(R.drawable.background_highlight_border);
}
});
答案 1 :(得分:1)
创建一个int变量selectedPosition来存储单击的项目。
现在更新您的onBindViewHolder,将背景应用于所选图像。 您可以在OnClickListener中设置位置,然后调用notifyDataSetChanged()。
代码:
public class GalleryAdapter extends RecyclerView.Adapter < GalleryAdapter.ViewHolder > {
//rest of the code
private int selectedPos = 0;
// rest of the code
@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);
if(selectedPos == position)
holder.image.setBackgroundResource(R.drawable.background_highlight_border);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
photoListener.onPhotoClick(image);
selectedPos = getAdapterPosition()
notifyDataSetChanged()
}
});
}
}