简体   繁体   English

如何在不依赖于位置的 recyclerview 上设置 OnClickListener

[英]How to set an OnClickListener on a recyclerview not depending on the position

I want the onclicklistener method to open the activity which is related to the object:entidad1, entidad2 or entidad3.我希望 onclicklistener 方法打开与对象相关的活动:entidad1、entidad2 或 entidad3。

The OnRecipe method on the MainActivity.java which i want it to make that if the entidad1 appears it takes me to x activity, and if entidad2 appears to y activity and so, any idea of how to do it, because now it takes me all the time to the activity of the entidad1. MainActivity.java 上的 OnRecipe 方法,我希望它实现,如果 entidad1 出现,它会将我带到 x 活动,如果 entidad2 出现在 y 活动中,那么,不知道如何去做,因为现在它需要我所有entidad1 活动的时间。 I guess it must be related with using priority to decide which to open instead of the position.我想这一定与使用优先级来决定打开哪个而不是位置有关。

That's my Adapter:那是我的适配器:

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;
        //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.onRecipeListener = onRecipeListener;

            itemView.setOnClickListener(this);
        }

        //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) {
            onRecipeListener.OnRecipe(getAdapterPosition());
        }
    }

    public interface OnRecipeListener{
        void OnRecipe(int priority);
    }
}

And that is the MainActivity.java:这就是 MainActivity.java:

public class Comida extends AppCompatActivity implements Adaptador2.OnRecipeListener {
    private RecyclerView recyclerView1;
    List<Entidad2> listItems;
    Adaptador2 adaptor;
    private Entidad2 entidad1,entidad2,entidad3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_comida);

        recyclerView1 = findViewById(R.id.lv_1);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        recyclerView1.setLayoutManager(layoutManager);

        listItems = new ArrayList<>();
        entidad1 = new Entidad2(R.drawable.calabacines_3, "Solomillo a la plancha", " 10 min.", 4, 20);
        entidad2 = new Entidad2(R.drawable.patatas_deluxe_especiadas_70523_300_150, "Entrecot", " 15 min.", 2, 50);
        entidad3 = new Entidad2(R.drawable.tomate, "Hamburguesa", " 2 min.", 5, 100);


        listItems.add(entidad1);
        listItems.add(entidad2);
        listItems.add(entidad3);

        adaptor = new Adaptador2(listItems, this);
        recyclerView1.setAdapter(adaptor);
        adaptor.notifyDataSetChanged();
        pickEntidad();
    }
    @Override
    public void OnRecipe(int priority) {

        if (priority == 20) {
            Intent in = new Intent(this, Solomillo.class);
            startActivity(in);
        }
        if (priority == 50) {
            Intent in = new Intent(this, Entrecot.class);
            startActivity(in);
        }
        if (priority == 100) {
            Intent in = new Intent(this, Hamburguesa.class);
            startActivity(in);
        }
    }

    private void pickEntidad(){
        final int random = new Random().nextInt(101);

        int priority1 = entidad1.getPriority();
        int priority2 = entidad2.getPriority();
        int priority3 = entidad3.getPriority();


        listItems.clear();
        if(random < priority1){

            listItems.add(entidad1);

        }else if(random < priority2){

            listItems.add(entidad2);

        }else if (random <= priority3){

            listItems.add(entidad3);

        }
        adaptor.notifyDataSetChanged();
    }
}

And the Entidad.java:和 Entidad.java:

public class Entidad2 {

    private int imgFoto;
    private String titulo;
    private String tiempo;
    private int ratingBar;
    private int priority;
    private int d;


    public Entidad2(int imgFoto, String titulo, String tiempo, int ratingBar, int priority) {

        this.imgFoto = imgFoto;
        this.titulo = titulo;
        this.tiempo = tiempo;
        this.ratingBar = ratingBar;
        this.priority = priority;
    }

    public int getImgFoto() {
        return imgFoto;
    }

    public String getTitulo() {
        return titulo;
    }

    public String getTiempo() {
        return tiempo;
    }

    public int getRating() { return ratingBar; }

    public  int getPriority() {
        return  priority;
    }
}

Please help me to solve the problem and if you need more information just tell me i will post it.请帮助我解决问题,如果您需要更多信息,请告诉我我会发布。

You are trying to differentiate your items by Priority but you are passing item position from your adapter to your activity.您正在尝试按优先级区分您的项目,但您正在将项目位置从您的适配器传递到您的活动。 You need to pass clicked item's Priority instead of its position.您需要传递单击项目的优先级而不是其位置。 I have changed your Adaptor2 class我已经改变了你的 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);
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM