JavaEar 专注于收集分享传播有价值的技术资料

RecyclerView click and replace image from another activity

问题描述


英文原文

I have some doubts about how to solve the doubt about how to click on a RecyclerView and change image from another activity, such as MainActivity.

In the image you can see what I'm trying to do. I have a total of 6 images, 3 Small images and another 3 Large images.

In the footer of MainActivity I have the ReciclerView that loads the 3 small images, I want that when they click for example to the image_Small_2/item_Small_2, that replaces the image that is in the center by the image_Large_2/item_Large_2.

I don't know if I'm explaining myself well, I leave you a screenshot and the code to see if you can give me a hand. Thanks in advance. P.S. I use the glide library to load the images.

My Adapter RecyclerView

public class AdaptadorX extends RecyclerView.Adapter<AdaptadorX.ViewHolder> {
    private ArrayList<Items> itemsLi;
    private Context context;

    public static class ViewHolder extends RecyclerView.ViewHolder{
        public ImageView idSrcImagen;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            idSrcImagen = itemView.findViewById(R.id.idImagen);
        }
    }

    public AdaptadorX(ArrayList<Items> itemsListado, Context context_L){
        itemsLi = itemsListado;
        context = context_L;
    }

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

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
        final Items contarItems =  itemsLi.get(position);
        Glide.with(context).load(contarItems.getxNombre_imagen()).into(holder.idSrcImagen);
        holder.idSrcImagen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("Mensaje_AdaptadorX.java", "Mi Posicion FOTO es: "+String.valueOf(position));
            }
        });
    }

    @Override
    public int getItemCount() {
        Log.d("Mensaje_Size_Tamaño", String.valueOf(itemsLi.size()));
        return itemsLi.size();
    }

}

Class Items

public class Items {
    private String xNombre_imagen;

    public Items (String nombre_imagen_M){
        xNombre_imagen = nombre_imagen_M;
    }

    public String getxNombre_imagen() {
        return xNombre_imagen;
    }
}

MainActivity

public class MainActivity extends AppCompatActivity {
    public ImageView idImgHead;
    private ArrayList<Items> items;
    private RecyclerView idRecyclerView;
    private RecyclerView.Adapter adapter;
    private RecyclerView.LayoutManager layoutManager;

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

        idRecyclerView = findViewById(R.id.idRecyclerView);
        idImgHead = findViewById(R.id.idImgHead);
        Glide.with(this).load("https://www.midominio.com/Imagen_GRANDE_head_01.jpg").into(idImgHead); //IMAGE BIG
        listadoXhead();
    }

    private void listadoXhead() {
        ArrayList<Items> items = new ArrayList<>();
        items.add(new Items("https://www.midominio.com/Imagen_Pequeña_head_01.jpg")); //IMAGE Small
        items.add(new Items("https://www.midominio.com/Imagen_Pequeña_head_02.jpg")); //IMAGE Small
        items.add(new Items("https://www.midominio.com/Imagen_Pequeña_head_03.jpg")); //IMAGE Small
        idRecyclerView.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(MainActivity.this);
        ((LinearLayoutManager) layoutManager).setOrientation(RecyclerView.HORIZONTAL);
        adapter = new AdaptadorX(items, MainActivity.this);
        idRecyclerView.setLayoutManager(layoutManager);
        idRecyclerView.setAdapter(adapter);
    }
}

activity_main

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/idImgHead"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:scaleType="fitCenter"
        app:layout_constraintBottom_toTopOf="@+id/linearLayout"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@mipmap/ic_launcher" />

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/idRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

activity_item

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/idImagen"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/ic_launcher_foreground"
        android:onClick="accionBoton"/>

</LinearLayout>

Example Image Here


中文翻译

I have some doubts about how to solve the doubt about how to click on a RecyclerView and change image from another activity, such as MainActivity.

In the image you can see what I'm trying to do. I have a total of 6 images, 3 Small images and another 3 Large images.

In the footer of MainActivity I have the ReciclerView that loads the 3 small images, I want that when they click for example to the image_Small_2/item_Small_2, that replaces the image that is in the center by the image_Large_2/item_Large_2.

I don't know if I'm explaining myself well, I leave you a screenshot and the code to see if you can give me a hand. Thanks in advance. P.S. I use the glide library to load the images.

My Adapter RecyclerView

public class AdaptadorX extends RecyclerView.Adapter<AdaptadorX.ViewHolder> {
    private ArrayList<Items> itemsLi;
    private Context context;

    public static class ViewHolder extends RecyclerView.ViewHolder{
        public ImageView idSrcImagen;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            idSrcImagen = itemView.findViewById(R.id.idImagen);
        }
    }

    public AdaptadorX(ArrayList<Items> itemsListado, Context context_L){
        itemsLi = itemsListado;
        context = context_L;
    }

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

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
        final Items contarItems =  itemsLi.get(position);
        Glide.with(context).load(contarItems.getxNombre_imagen()).into(holder.idSrcImagen);
        holder.idSrcImagen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("Mensaje_AdaptadorX.java", "Mi Posicion FOTO es: "+String.valueOf(position));
            }
        });
    }

    @Override
    public int getItemCount() {
        Log.d("Mensaje_Size_Tamaño", String.valueOf(itemsLi.size()));
        return itemsLi.size();
    }

}

Class Items

public class Items {
    private String xNombre_imagen;

    public Items (String nombre_imagen_M){
        xNombre_imagen = nombre_imagen_M;
    }

    public String getxNombre_imagen() {
        return xNombre_imagen;
    }
}

MainActivity

public class MainActivity extends AppCompatActivity {
    public ImageView idImgHead;
    private ArrayList<Items> items;
    private RecyclerView idRecyclerView;
    private RecyclerView.Adapter adapter;
    private RecyclerView.LayoutManager layoutManager;

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

        idRecyclerView = findViewById(R.id.idRecyclerView);
        idImgHead = findViewById(R.id.idImgHead);
        Glide.with(this).load("https://www.midominio.com/Imagen_GRANDE_head_01.jpg").into(idImgHead); //IMAGE BIG
        listadoXhead();
    }

    private void listadoXhead() {
        ArrayList<Items> items = new ArrayList<>();
        items.add(new Items("https://www.midominio.com/Imagen_Pequeña_head_01.jpg")); //IMAGE Small
        items.add(new Items("https://www.midominio.com/Imagen_Pequeña_head_02.jpg")); //IMAGE Small
        items.add(new Items("https://www.midominio.com/Imagen_Pequeña_head_03.jpg")); //IMAGE Small
        idRecyclerView.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(MainActivity.this);
        ((LinearLayoutManager) layoutManager).setOrientation(RecyclerView.HORIZONTAL);
        adapter = new AdaptadorX(items, MainActivity.this);
        idRecyclerView.setLayoutManager(layoutManager);
        idRecyclerView.setAdapter(adapter);
    }
}

activity_main

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/idImgHead"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:scaleType="fitCenter"
        app:layout_constraintBottom_toTopOf="@+id/linearLayout"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@mipmap/ic_launcher" />

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/idRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

activity_item

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/idImagen"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/ic_launcher_foreground"
        android:onClick="accionBoton"/>

</LinearLayout>

Example Image Here

I have some doubts about how to solve the doubt about how to click on a RecyclerView and change image from another activity, such as MainActivity.

In the image you can see what I'm trying to do. I have a total of 6 images, 3 Small images and another 3 Large images.

In the footer of MainActivity I have the ReciclerView that loads the 3 small images, I want that when they click for example to the image_Small_2/item_Small_2, that replaces the image that is in the center by the image_Large_2/item_Large_2.

I don't know if I'm explaining myself well, I leave you a screenshot and the code to see if you can give me a hand. Thanks in advance. P.S. I use the glide library to load the images.

My Adapter RecyclerView

public class AdaptadorX extends RecyclerView.Adapter<AdaptadorX.ViewHolder> {
    private ArrayList<Items> itemsLi;
    private Context context;

    public static class ViewHolder extends RecyclerView.ViewHolder{
        public ImageView idSrcImagen;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            idSrcImagen = itemView.findViewById(R.id.idImagen);
        }
    }

    public AdaptadorX(ArrayList<Items> itemsListado, Context context_L){
        itemsLi = itemsListado;
        context = context_L;
    }

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

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
        final Items contarItems =  itemsLi.get(position);
        Glide.with(context).load(contarItems.getxNombre_imagen()).into(holder.idSrcImagen);
        holder.idSrcImagen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("Mensaje_AdaptadorX.java", "Mi Posicion FOTO es: "+String.valueOf(position));
            }
        });
    }

    @Override
    public int getItemCount() {
        Log.d("Mensaje_Size_Tamaño", String.valueOf(itemsLi.size()));
        return itemsLi.size();
    }

}

Class Items

public class Items {
    private String xNombre_imagen;

    public Items (String nombre_imagen_M){
        xNombre_imagen = nombre_imagen_M;
    }

    public String getxNombre_imagen() {
        return xNombre_imagen;
    }
}

MainActivity

public class MainActivity extends AppCompatActivity {
    public ImageView idImgHead;
    private ArrayList<Items> items;
    private RecyclerView idRecyclerView;
    private RecyclerView.Adapter adapter;
    private RecyclerView.LayoutManager layoutManager;

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

        idRecyclerView = findViewById(R.id.idRecyclerView);
        idImgHead = findViewById(R.id.idImgHead);
        Glide.with(this).load("https://www.midominio.com/Imagen_GRANDE_head_01.jpg").into(idImgHead); //IMAGE BIG
        listadoXhead();
    }

    private void listadoXhead() {
        ArrayList<Items> items = new ArrayList<>();
        items.add(new Items("https://www.midominio.com/Imagen_Pequeña_head_01.jpg")); //IMAGE Small
        items.add(new Items("https://www.midominio.com/Imagen_Pequeña_head_02.jpg")); //IMAGE Small
        items.add(new Items("https://www.midominio.com/Imagen_Pequeña_head_03.jpg")); //IMAGE Small
        idRecyclerView.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(MainActivity.this);
        ((LinearLayoutManager) layoutManager).setOrientation(RecyclerView.HORIZONTAL);
        adapter = new AdaptadorX(items, MainActivity.this);
        idRecyclerView.setLayoutManager(layoutManager);
        idRecyclerView.setAdapter(adapter);
    }
}

activity_main

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/idImgHead"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:scaleType="fitCenter"
        app:layout_constraintBottom_toTopOf="@+id/linearLayout"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@mipmap/ic_launcher" />

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/idRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

activity_item

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/idImagen"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/ic_launcher_foreground"
        android:onClick="accionBoton"/>

</LinearLayout>

Example Image Here

I have some doubts about how to solve the doubt about how to click on a RecyclerView and change image from another activity, such as MainActivity.

In the image you can see what I'm trying to do. I have a total of 6 images, 3 Small images and another 3 Large images.

In the footer of MainActivity I have the ReciclerView that loads the 3 small images, I want that when they click for example to the image_Small_2/item_Small_2, that replaces the image that is in the center by the image_Large_2/item_Large_2.

I don't know if I'm explaining myself well, I leave you a screenshot and the code to see if you can give me a hand. Thanks in advance. P.S. I use the glide library to load the images.

My Adapter RecyclerView

public class AdaptadorX extends RecyclerView.Adapter<AdaptadorX.ViewHolder> {
    private ArrayList<Items> itemsLi;
    private Context context;

    public static class ViewHolder extends RecyclerView.ViewHolder{
        public ImageView idSrcImagen;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            idSrcImagen = itemView.findViewById(R.id.idImagen);
        }
    }

    public AdaptadorX(ArrayList<Items> itemsListado, Context context_L){
        itemsLi = itemsListado;
        context = context_L;
    }

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

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
        final Items contarItems =  itemsLi.get(position);
        Glide.with(context).load(contarItems.getxNombre_imagen()).into(holder.idSrcImagen);
        holder.idSrcImagen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("Mensaje_AdaptadorX.java", "Mi Posicion FOTO es: "+String.valueOf(position));
            }
        });
    }

    @Override
    public int getItemCount() {
        Log.d("Mensaje_Size_Tamaño", String.valueOf(itemsLi.size()));
        return itemsLi.size();
    }

}

Class Items

public class Items {
    private String xNombre_imagen;

    public Items (String nombre_imagen_M){
        xNombre_imagen = nombre_imagen_M;
    }

    public String getxNombre_imagen() {
        return xNombre_imagen;
    }
}

MainActivity

public class MainActivity extends AppCompatActivity {
    public ImageView idImgHead;
    private ArrayList<Items> items;
    private RecyclerView idRecyclerView;
    private RecyclerView.Adapter adapter;
    private RecyclerView.LayoutManager layoutManager;

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

        idRecyclerView = findViewById(R.id.idRecyclerView);
        idImgHead = findViewById(R.id.idImgHead);
        Glide.with(this).load("https://www.midominio.com/Imagen_GRANDE_head_01.jpg").into(idImgHead); //IMAGE BIG
        listadoXhead();
    }

    private void listadoXhead() {
        ArrayList<Items> items = new ArrayList<>();
        items.add(new Items("https://www.midominio.com/Imagen_Pequeña_head_01.jpg")); //IMAGE Small
        items.add(new Items("https://www.midominio.com/Imagen_Pequeña_head_02.jpg")); //IMAGE Small
        items.add(new Items("https://www.midominio.com/Imagen_Pequeña_head_03.jpg")); //IMAGE Small
        idRecyclerView.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(MainActivity.this);
        ((LinearLayoutManager) layoutManager).setOrientation(RecyclerView.HORIZONTAL);
        adapter = new AdaptadorX(items, MainActivity.this);
        idRecyclerView.setLayoutManager(layoutManager);
        idRecyclerView.setAdapter(adapter);
    }
}

activity_main

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/idImgHead"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:scaleType="fitCenter"
        app:layout_constraintBottom_toTopOf="@+id/linearLayout"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@mipmap/ic_launcher" />

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/idRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

activity_item

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/idImagen"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/ic_launcher_foreground"
        android:onClick="accionBoton"/>

</LinearLayout>

Example Image Here

1个回答

    最佳答案

  1. 英文原文

    You need to implement an interface listener in your activity and pass it to your adapter. Add this to your adapter and pass it through the constructor

    interface OnImageClickListener{
        void onSelected(String url);
    }
    

    Your whole adapter should look like this.

    public class AdaptadorX extends RecyclerView.Adapter<AdaptadorX.ViewHolder> {
    private ArrayList<Items> itemsLi;
    private Context context;
    pruvate OnImageClickListener listener;
    
    interface OnImageClickListener{
        void onSelected(String url);
    }
    
    public static class ViewHolder extends RecyclerView.ViewHolder{
        public ImageView idSrcImagen;
    
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            idSrcImagen = itemView.findViewById(R.id.idImagen);
        }
    }
    
    public AdaptadorX(ArrayList<Items> itemsListado, Context context_L, OnImageClickListener listener){
        itemsLi = itemsListado;
        context = context_L;
        this.listener = listener;
    }
    
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_item, parent, false);
        ViewHolder content = new ViewHolder(view);
        return content;
    }
    
    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
        final Items contarItems =  itemsLi.get(position);
        Glide.with(context).load(contarItems.getxNombre_imagen()).into(holder.idSrcImagen);
        holder.idSrcImagen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onSelected(contarItems.getxNombre_imagen())
            }
        });
    }
    
    @Override
    public int getItemCount() {
        Log.d("Mensaje_Size_Tamaño", String.valueOf(itemsLi.size()));
        return itemsLi.size();
    }
    

    }

    And in your Activity you implement this interface and pass it to the adapter.

    public class MainActivity extends AppCompatActivity implements OnImageClickListener   {
    public ImageView idImgHead;
    private ArrayList<Items> items;
    private RecyclerView idRecyclerView;
    private RecyclerView.Adapter adapter;
    private RecyclerView.LayoutManager layoutManager;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        idRecyclerView = findViewById(R.id.idRecyclerView);
        idImgHead = findViewById(R.id.idImgHead);
        Glide.with(this).load("https://www.midominio.com/Imagen_GRANDE_head_01.jpg").into(idImgHead); //IMAGE BIG
        listadoXhead();
    }
    
    @Override
    public onSelected(String url) {
         Glide.with(this).load(url).into(idImgHead);
    }
    
    private void listadoXhead() {
        ArrayList<Items> items = new ArrayList<>();
        items.add(new Items("https://www.midominio.com/Imagen_Pequeña_head_01.jpg")); //IMAGE Small
        items.add(new Items("https://www.midominio.com/Imagen_Pequeña_head_02.jpg")); //IMAGE Small
        items.add(new Items("https://www.midominio.com/Imagen_Pequeña_head_03.jpg")); //IMAGE Small
        idRecyclerView.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(MainActivity.this);
        ((LinearLayoutManager) layoutManager).setOrientation(RecyclerView.HORIZONTAL);
        adapter = new AdaptadorX(items, MainActivity.this, this);
        idRecyclerView.setLayoutManager(layoutManager);
        idRecyclerView.setAdapter(adapter);
    }
    

    }


    中文翻译

    You need to implement an interface listener in your activity and pass it to your adapter. Add this to your adapter and pass it through the constructor

    interface OnImageClickListener{
        void onSelected(String url);
    }
    

    Your whole adapter should look like this.

    public class AdaptadorX extends RecyclerView.Adapter<AdaptadorX.ViewHolder> {
    private ArrayList<Items> itemsLi;
    private Context context;
    pruvate OnImageClickListener listener;
    
    interface OnImageClickListener{
        void onSelected(String url);
    }
    
    public static class ViewHolder extends RecyclerView.ViewHolder{
        public ImageView idSrcImagen;
    
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            idSrcImagen = itemView.findViewById(R.id.idImagen);
        }
    }
    
    public AdaptadorX(ArrayList<Items> itemsListado, Context context_L, OnImageClickListener listener){
        itemsLi = itemsListado;
        context = context_L;
        this.listener = listener;
    }
    
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_item, parent, false);
        ViewHolder content = new ViewHolder(view);
        return content;
    }
    
    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
        final Items contarItems =  itemsLi.get(position);
        Glide.with(context).load(contarItems.getxNombre_imagen()).into(holder.idSrcImagen);
        holder.idSrcImagen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onSelected(contarItems.getxNombre_imagen())
            }
        });
    }
    
    @Override
    public int getItemCount() {
        Log.d("Mensaje_Size_Tamaño", String.valueOf(itemsLi.size()));
        return itemsLi.size();
    }
    

    }

    And in your Activity you implement this interface and pass it to the adapter.

    public class MainActivity extends AppCompatActivity implements OnImageClickListener   {
    public ImageView idImgHead;
    private ArrayList<Items> items;
    private RecyclerView idRecyclerView;
    private RecyclerView.Adapter adapter;
    private RecyclerView.LayoutManager layoutManager;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        idRecyclerView = findViewById(R.id.idRecyclerView);
        idImgHead = findViewById(R.id.idImgHead);
        Glide.with(this).load("https://www.midominio.com/Imagen_GRANDE_head_01.jpg").into(idImgHead); //IMAGE BIG
        listadoXhead();
    }
    
    @Override
    public onSelected(String url) {
         Glide.with(this).load(url).into(idImgHead);
    }
    
    private void listadoXhead() {
        ArrayList<Items> items = new ArrayList<>();
        items.add(new Items("https://www.midominio.com/Imagen_Pequeña_head_01.jpg")); //IMAGE Small
        items.add(new Items("https://www.midominio.com/Imagen_Pequeña_head_02.jpg")); //IMAGE Small
        items.add(new Items("https://www.midominio.com/Imagen_Pequeña_head_03.jpg")); //IMAGE Small
        idRecyclerView.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(MainActivity.this);
        ((LinearLayoutManager) layoutManager).setOrientation(RecyclerView.HORIZONTAL);
        adapter = new AdaptadorX(items, MainActivity.this, this);
        idRecyclerView.setLayoutManager(layoutManager);
        idRecyclerView.setAdapter(adapter);
    }
    

    }