java - RecyclerView 单击并替换另一个 Activity 中的图像

标签 java android mobile android-recyclerview

我对如何解决如何单击 RecyclerView 并从另一个 Activity(例如 MainActivity)更改图像的疑问有一些疑问。

在图片中您可以看到我正在尝试做什么。 我总共有 6 张图片,其中 3 张小图片和另外 3 张大图片

在 MainActivity 的页脚中,我有 ReciclerView 加载 3 个小图像,我希望当它们单击 image_Small_2/item_Small_2 时,将中间的图像替换为 image_Large_2/item_Large_2。

我不知道我是否能很好地解释自己,我给你留下了截图和代码,看看你是否可以帮助我。提前致谢。附:我使用 glide 库来加载图像。

我的适配器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();
    }

}

类别项目

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;
    }
}

主要 Activity

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

最佳答案

您需要在 Activity 中实现接口(interface)监听器并将其传递给适配器。将其添加到您的适配器并通过构造函数传递它

interface OnImageClickListener{
    void onSelected(String url);
}

您的整个适配器应如下所示。

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();
}

}

在您的 Activity 中,您实现此接口(interface)并将其传递给适配器。

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);
}

}

关于java - RecyclerView 单击并替换另一个 Activity 中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58053804/

相关文章:

javascript - 在 javascript 中检测屏幕上的虚拟键盘和横向

java - 循环绘制不同的图像?

java - 使用扫描仪分割字符串

android - 启动和停止notificationlistener服务

Android/Facebook - 没有权限/应用程序登录屏幕

Android 2.2 SDK - Nexus One 上的 Camera API setParameters 失败

java jsp网页缓存POST请求

java - JUnit:在被测类中启用断言

android - 在哪里插入应用程序启动的代码?

javascript - 处理电话 : anchor