猿问

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

我希望 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;


        }


慕少森
浏览 101回答 1
1回答

UYOU

您正在尝试按优先级区分您的项目,但您正在将项目位置从适配器传递给您的活动。您需要传递点击项目的优先级而不是其位置。我已经更改了您的 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&nbsp; extends RecyclerView.Adapter<Adaptador2.ViewHolder>{private List<Entidad2> listItems;private OnRecipeListener mOnRecipeListener;public&nbsp; Adaptador2(List<Entidad2> listItems, OnRecipeListener onRecipeListener) {&nbsp; &nbsp; this.listItems = listItems;&nbsp; &nbsp; this.mOnRecipeListener = onRecipeListener;}@Overridepublic ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {&nbsp; &nbsp; View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.elemento_lista2, parent, false);&nbsp; &nbsp; return new ViewHolder(view, mOnRecipeListener);}@Overridepublic void onBindViewHolder(ViewHolder viewholder, int position) {&nbsp; &nbsp; Entidad2 entidad = listItems.get(position);&nbsp; &nbsp; int resource = entidad.getImgFoto();&nbsp; &nbsp; String title = entidad.getTitulo();&nbsp; &nbsp; String time = entidad.getTiempo();&nbsp; &nbsp; int barra = entidad.getRating();&nbsp; &nbsp; final int priority = entidad.getPriority();&nbsp; &nbsp; //int fondo = listItems.get(position).getColorfondo();&nbsp; &nbsp; viewholder.setData(resource, title, time, barra);//You can pass the clicked item's priority back to your activity like this&nbsp; &nbsp; viewholder.itemView.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; mOnRecipeListener.OnRecipe(priority);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; &nbsp; // por si necesito color de fondo viewholder.setData(resource, title, time, barra, fondo);}@Overridepublic int getItemCount() {&nbsp; &nbsp; return listItems.size();}class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {&nbsp; &nbsp; private ImageView imgFoto;&nbsp; &nbsp; private TextView titulo;&nbsp; &nbsp; private TextView tiempo;&nbsp; &nbsp; private RatingBar ratingBar;&nbsp; &nbsp; //private ImageView colorfondo;&nbsp; &nbsp; OnRecipeListener onRecipeListener;&nbsp; &nbsp; public ViewHolder(View itemView, OnRecipeListener onRecipeListener) {&nbsp; &nbsp; &nbsp; &nbsp; super(itemView);&nbsp; &nbsp; &nbsp; &nbsp; imgFoto = itemView.findViewById(R.id.imgFoto);&nbsp; &nbsp; &nbsp; &nbsp; titulo = itemView.findViewById(R.id.tvTitulo);&nbsp; &nbsp; &nbsp; &nbsp; tiempo = itemView.findViewById(R.id.tvTiempo);&nbsp; &nbsp; &nbsp; &nbsp; ratingBar = itemView.findViewById(R.id.ratingBarVerd);&nbsp; &nbsp; &nbsp; &nbsp; //colorfondo = itemView.findViewById(R.id.colorfondo);&nbsp; &nbsp; &nbsp; &nbsp; //This is useless&nbsp; &nbsp; &nbsp; &nbsp; //this.onRecipeListener = onRecipeListener;&nbsp; &nbsp; }&nbsp; &nbsp; //por si necesito color de fondo private void setData(int resource, String title, String time, int barra, int fondo){&nbsp; &nbsp; private void setData(int resource, String title, String time, int barra){&nbsp; &nbsp; &nbsp; &nbsp; imgFoto.setImageResource(resource);&nbsp; &nbsp; &nbsp; &nbsp; titulo.setText(title);&nbsp; &nbsp; &nbsp; &nbsp; tiempo.setText(time);&nbsp; &nbsp; &nbsp; &nbsp; ratingBar.setRating(barra);&nbsp; &nbsp; &nbsp; &nbsp; //colorfondo.setImageResource(fondo);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; }}public interface OnRecipeListener{&nbsp; &nbsp; void OnRecipe(int priority);}}
随时随地看视频慕课网APP

相关分类

Java
我要回答