RecyclerView 单击并替换另一个活动中的图像

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

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

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

我不知道我是否解释得很好,我给你留下了截图和代码,看看你是否可以帮助我。提前致谢。PS 我使用 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();

    }


}



Smart猫小萌
浏览 83回答 1
1回答

慕虎7371278

您需要在活动中实现接口侦听器并将其传递给适配器。将其添加到您的适配器并通过构造函数传递它interface OnImageClickListener{&nbsp; &nbsp; void onSelected(String url);}您的整个适配器应该如下所示。public class AdaptadorX extends RecyclerView.Adapter<AdaptadorX.ViewHolder> {private ArrayList<Items> itemsLi;private Context context;pruvate OnImageClickListener listener;interface OnImageClickListener{&nbsp; &nbsp; void onSelected(String url);}public static class ViewHolder extends RecyclerView.ViewHolder{&nbsp; &nbsp; public ImageView idSrcImagen;&nbsp; &nbsp; public ViewHolder(@NonNull View itemView) {&nbsp; &nbsp; &nbsp; &nbsp; super(itemView);&nbsp; &nbsp; &nbsp; &nbsp; idSrcImagen = itemView.findViewById(R.id.idImagen);&nbsp; &nbsp; }}public AdaptadorX(ArrayList<Items> itemsListado, Context context_L, OnImageClickListener listener){&nbsp; &nbsp; itemsLi = itemsListado;&nbsp; &nbsp; context = context_L;&nbsp; &nbsp; this.listener = listener;}@NonNull@Overridepublic ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {&nbsp; &nbsp; View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_item, parent, false);&nbsp; &nbsp; ViewHolder content = new ViewHolder(view);&nbsp; &nbsp; return content;}@Overridepublic void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {&nbsp; &nbsp; final Items contarItems =&nbsp; itemsLi.get(position);&nbsp; &nbsp; Glide.with(context).load(contarItems.getxNombre_imagen()).into(holder.idSrcImagen);&nbsp; &nbsp; holder.idSrcImagen.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View view) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listener.onSelected(contarItems.getxNombre_imagen())&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}@Overridepublic int getItemCount() {&nbsp; &nbsp; Log.d("Mensaje_Size_Tamaño", String.valueOf(itemsLi.size()));&nbsp; &nbsp; return itemsLi.size();}}在您的 Activity 中,您实现此接口并将其传递给适配器。public class MainActivity extends AppCompatActivity implements OnImageClickListener&nbsp; &nbsp;{public ImageView idImgHead;private ArrayList<Items> items;private RecyclerView idRecyclerView;private RecyclerView.Adapter adapter;private RecyclerView.LayoutManager layoutManager;@Overrideprotected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; setContentView(R.layout.activity_main);&nbsp; &nbsp; idRecyclerView = findViewById(R.id.idRecyclerView);&nbsp; &nbsp; idImgHead = findViewById(R.id.idImgHead);&nbsp; &nbsp; Glide.with(this).load("https://www.midominio.com/Imagen_GRANDE_head_01.jpg").into(idImgHead); //IMAGE BIG&nbsp; &nbsp; listadoXhead();}@Overridepublic onSelected(String url) {&nbsp; &nbsp; &nbsp;Glide.with(this).load(url).into(idImgHead);}private void listadoXhead() {&nbsp; &nbsp; ArrayList<Items> items = new ArrayList<>();&nbsp; &nbsp; items.add(new Items("https://www.midominio.com/Imagen_Pequeña_head_01.jpg")); //IMAGE Small&nbsp; &nbsp; items.add(new Items("https://www.midominio.com/Imagen_Pequeña_head_02.jpg")); //IMAGE Small&nbsp; &nbsp; items.add(new Items("https://www.midominio.com/Imagen_Pequeña_head_03.jpg")); //IMAGE Small&nbsp; &nbsp; idRecyclerView.setHasFixedSize(true);&nbsp; &nbsp; layoutManager = new LinearLayoutManager(MainActivity.this);&nbsp; &nbsp; ((LinearLayoutManager) layoutManager).setOrientation(RecyclerView.HORIZONTAL);&nbsp; &nbsp; adapter = new AdaptadorX(items, MainActivity.this, this);&nbsp; &nbsp; idRecyclerView.setLayoutManager(layoutManager);&nbsp; &nbsp; idRecyclerView.setAdapter(adapter);}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java