M
M
matveyboyko2018-03-25 11:43:12
Android
matveyboyko, 2018-03-25 11:43:12

How to open full image on click on item in Android RecyclerView?

Hey!
Images are displayed using RecyclerView.
You need to show the full size image.
As I understand it, for this you need to call a new activity in which to show the desired image in the ImageView field. But for this you need to pass some kind of identifier to this activity.
So far, I figured out how to display Toast when clicking on one of them :)
Here is the adapter code:

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import java.util.ArrayList;

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
    ArrayList<Album> arrayList = new ArrayList<>();
    Activity activity;

    public RecyclerAdapter(ArrayList<Album> arrayList, Context context) {
        this.arrayList = arrayList;
        activity = (Activity) context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.Title.setText(arrayList.get(position).getTitle());
        String path = Config.img_path + arrayList.get(position).getId();
       Glide.with(activity).load(path).placeholder(R.drawable.placeholder).error(R.drawable.notfound).into(holder.Thumbnail);
    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        ImageView Thumbnail;
        TextView Title;

        public MyViewHolder(View itemView) {
            super(itemView);
            Thumbnail = (ImageView) itemView.findViewById(R.id.thumbnail);
            Title = (TextView) itemView.findViewById(R.id.album_title);
            itemView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    int position = getAdapterPosition();
                    if(position != RecyclerView.NO_POSITION) {
                        Toast.makeText(v.getContext(), "test", Toast.LENGTH_LONG).show();
                    }
                }
            });
        }

        @Override
        public void onClick(View v) {
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Nekrasov, 2018-03-25
@LemonRX

In order to open a new activity, use intent, you can also pass the required data together with the intent
startandroid.ru/ru/uroki/vse-uroki-spiskom/67-urok...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question