【问题标题】:Set image background using path使用路径设置图像背景
【发布时间】: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


    【解决方案1】:

    创建一个int变量selectedPosition来存储被点击的item。

    现在更新您的 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()
          }
        });
    
    
      }
    
        }
    

    【讨论】:

    • 感谢您的回答,但是getAdapterPosition()方法似乎不起作用,找不到。您对此有解决方案吗?
    • 试试selectedPos = position
    【解决方案2】:

    您不必在此处使用界面,因为您在 SelectFileActivity 中没有选定图像视图的参考。但是你可以像这样轻松地做到这一点。在您的 onClickListener 中添加此代码

    holder.itemView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        holder.image.setBackgroundResource(R.drawable.background_highlight_border);
      }
    });
    

    【讨论】:

    • 这个解决方案是选择所有图像,我只想要其中一个
    猜你喜欢
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 2020-05-15
    • 1970-01-01
    • 1970-01-01
    • 2011-02-14
    • 1970-01-01
    • 2020-12-11
    相关资源
    最近更新 更多