为了账号安全,请及时绑定邮箱和手机立即绑定

如何在不依赖于位置的回收视图上设置 OnClickListener

如何在不依赖于位置的回收视图上设置 OnClickListener

慕少森 2023-05-17 17:59:11
我希望 onclicklistener 方法打开与对象相关的活动:entidad1、entidad2 或 entidad3。MainActivity.java 上的 OnRecipe 方法,如果 entidad1 出现,它会带我到 x 活动,如果 entidad2 出现在 y 活动等,关于如何做的任何想法,因为现在它需要我所有entidad1 活动的时间。我想这一定与使用优先级来决定打开哪个而不是位置有关。那是我的适配器:package com.test.platos_4;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.RatingBar;import android.widget.TextView;import java.util.List;public class Adaptador2  extends RecyclerView.Adapter<Adaptador2.ViewHolder>{    private List<Entidad2> listItems;    private OnRecipeListener mOnRecipeListener;    public  Adaptador2(List<Entidad2> listItems, OnRecipeListener onRecipeListener) {        this.listItems = listItems;        this.mOnRecipeListener = onRecipeListener;    }    @Override    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.elemento_lista2, parent, false);        return new ViewHolder(view, mOnRecipeListener);    }    @Override    public void onBindViewHolder(ViewHolder viewholder, int position) {        int resource = listItems.get(position).getImgFoto();        String title = listItems.get(position).getTitulo();        String time = listItems.get(position).getTiempo();        int barra = listItems.get(position).getRating();        //int fondo = listItems.get(position).getColorfondo();        viewholder.setData(resource, title, time, barra);        // por si necesito color de fondo viewholder.setData(resource, title, time, barra, fondo);    }    @Override    public int getItemCount() {        return listItems.size();    }    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {        private ImageView imgFoto;        private TextView titulo;        private TextView tiempo;        private RatingBar ratingBar;        }
查看完整描述

1 回答

?
UYOU

TA贡献1878条经验 获得超4个赞

您正在尝试按优先级区分您的项目,但您正在将项目位置从适配器传递给您的活动。您需要传递点击项目的优先级而不是其位置。我已经更改了您的 Adaptor2 类


package com.test.platos_4;


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.RatingBar;

import android.widget.TextView;


import java.util.List;


public class Adaptador2  extends RecyclerView.Adapter<Adaptador2.ViewHolder>

{

private List<Entidad2> listItems;

private OnRecipeListener mOnRecipeListener;


public  Adaptador2(List<Entidad2> listItems, OnRecipeListener onRecipeListener) {

    this.listItems = listItems;

    this.mOnRecipeListener = onRecipeListener;

}



@Override

public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.elemento_lista2, parent, false);

    return new ViewHolder(view, mOnRecipeListener);

}


@Override

public void onBindViewHolder(ViewHolder viewholder, int position) {

    Entidad2 entidad = listItems.get(position);

    int resource = entidad.getImgFoto();

    String title = entidad.getTitulo();

    String time = entidad.getTiempo();

    int barra = entidad.getRating();

    final int priority = entidad.getPriority();

    //int fondo = listItems.get(position).getColorfondo();

    viewholder.setData(resource, title, time, barra);


//You can pass the clicked item's priority back to your activity like this

    viewholder.itemView.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View v) {

    mOnRecipeListener.OnRecipe(priority);

        }

    });

    // por si necesito color de fondo viewholder.setData(resource, title, time, barra, fondo);

}


@Override

public int getItemCount() {

    return listItems.size();

}

class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {


    private ImageView imgFoto;

    private TextView titulo;

    private TextView tiempo;

    private RatingBar ratingBar;

    //private ImageView colorfondo;

    OnRecipeListener onRecipeListener;


    public ViewHolder(View itemView, OnRecipeListener onRecipeListener) {

        super(itemView);


        imgFoto = itemView.findViewById(R.id.imgFoto);

        titulo = itemView.findViewById(R.id.tvTitulo);

        tiempo = itemView.findViewById(R.id.tvTiempo);

        ratingBar = itemView.findViewById(R.id.ratingBarVerd);

        //colorfondo = itemView.findViewById(R.id.colorfondo);


        //This is useless

        //this.onRecipeListener = onRecipeListener;


    }


    //por si necesito color de fondo private void setData(int resource, String title, String time, int barra, int fondo){

    private void setData(int resource, String title, String time, int barra){

        imgFoto.setImageResource(resource);

        titulo.setText(title);

        tiempo.setText(time);

        ratingBar.setRating(barra);

        //colorfondo.setImageResource(fondo);


    }


    @Override

    public void onClick(View v) {


    }

}


public interface OnRecipeListener{

    void OnRecipe(int priority);

}

}



查看完整回答
反对 回复 2023-05-17
  • 1 回答
  • 0 关注
  • 100 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信