单击recyclerView项时将图像放大到全屏

时间:2018-08-12 15:56:34

标签: android android-recyclerview recycler-adapter

在将其标记为重复之前。请阅读。

我正在构建一个聊天应用程序。我在recyclerView中接收到消息(文本和图像),并且有一个典型的适配器类。

现在,我想每当单击o图像时,图像都应放大到全屏。我已经看到了解决这种情况的方法,但是他们并没有帮助我。有人说打开另一个活动并传递URL,但随后将图像下载了两次。

任何帮助将不胜感激。如果没有代码,那么仅批准者就可以了。谢谢。

以下是我的适配器类的一部分:

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
    switch (holder.getItemViewType()){
        case 1:
            ViewHolder1 viewHolder1 = (ViewHolder1)holder;
            viewHolder1.Text.setText(messageList.get(position).getMessage());
            break;

        case 2:
            ViewHolder2 viewHolder2 = (ViewHolder2)holder;
            Glide.with(viewHolder2.Image.getContext())
                    .load(messageList.get(position).getMessage())
                    .placeholder(R.drawable.ic_image_send_24dp)
                    .crossFade()
                    .centerCrop()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(viewHolder2.Image);

            viewHolder2.Image.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent= new Intent(context,FullScreenImage.class);
                    intent.putExtra("image_url", messageList.get(position).getMessage());
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent);
                }
            });
            break;
    }
}

1 个答案:

答案 0 :(得分:0)

所以这可以工作:

在适配器类中:

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
    switch (holder.getItemViewType()){
        case 1:
            ViewHolder1 viewHolder1 = (ViewHolder1)holder;
            viewHolder1.Text.setText(messageList.get(position).getMessage());
            break;

        case 2:
            ViewHolder2 viewHolder2 = (ViewHolder2)holder;
            Glide.with(viewHolder2.Image.getContext())
                    .load(messageList.get(position).getMessage())
                    .placeholder(R.drawable.ic_image_send_24dp)
                    .crossFade()
                    .centerCrop()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(viewHolder2.Image);

            viewHolder2.Image.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent= new Intent(context,FullScreenImage.class);
                    intent.putExtra("image_url", messageList.get(position).getMessage());
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent);
                }
            });
            break;
    }
}

然后在新的活动中:

    public class FullScreenImage extends AppCompatActivity {


    ImageView myImage;
    String url = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_full_screen_image);

        url = getIntent().getStringExtra("image_url");

        myImage = findViewById(R.id.myImage);
        Glide.with(this).load(url)
                .placeholder(R.drawable.ic_image_send_24dp)
                .error(R.drawable.ic_image_send_24dp)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(myImage);
    }
}