I wrote in the adapter to RecyclerView onClick , but it turns out that the action is transmitted to every 15th element ... What is the problem? And how can I, after clicking on CardView correctly pull up more data on the Id element of the DataForecast.DataBean class and shove? thank

 public class ForecastAdapter extends RecyclerView.Adapter<ForecastAdapter.ForecastViewHolder> { private List<DataForecast.DataBean> list; public ForecastAdapter(List<DataForecast.DataBean> list) { this.list = list; } private List<GetDescriptionForecastModel> descrption; @Override public ForecastViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { Context context = parent.getContext(); LayoutInflater inflater = LayoutInflater.from(context); View view = inflater.inflate(R.layout.listitem_forecast, parent, false); return new ForecastViewHolder(view); } @Override public void onBindViewHolder(ForecastViewHolder holder, int position) { DataForecast.DataBean searchModel = list.get(position); holder.timedate.setText(searchModel.getDate()); holder.game.setText(searchModel.getCommand()); holder.forecast.setText("Фора1 по очкам (-4.5) @ " + searchModel.getKf()); //GetDescriptionForecastModel searchDescription = descrption.get(position); -- это дополнительные данные из другого класса которые мне нужны после onClick //holder.about.setText(searchDescription.getData()); } @Override public int getItemCount() { return list.size(); } public class ForecastViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { TextView timedate, game, forecast, score, about; CardView cv_forecast; public ForecastViewHolder(View itemView) { super(itemView); itemView.setOnClickListener(this); timedate = (TextView) itemView.findViewById(R.id.txt_forecast_timedate); game = (TextView) itemView.findViewById(R.id.txt_forecast_game); forecast = (TextView) itemView.findViewById(R.id.txt_forecast_forecast); score = (TextView) itemView.findViewById(R.id.txt_forecast_score); about = (TextView) itemView.findViewById(R.id.forecast_txt_about); cv_forecast = (CardView) itemView.findViewById(R.id.cv_forecast); } @Override public void onClick(View v) { View cardView = (View)itemView.findViewById(R.id.forecast_cardview_all); if (about.getVisibility()==View.GONE) { about.setVisibility(View.VISIBLE); cardView.setBackgroundResource(R.color.forecast_about_all); //TODO здесь делаю видимым поле для данных после onClick }else if (about.getVisibility()==View.VISIBLE){ about.setVisibility(View.GONE); cardView.setBackgroundResource(R.color.white); } } } 

}

    1 answer 1

    In your case, I see no reason to transfer the click listener to the class holder. Assign a onBindViewHolder to onBindViewHolder , especially since you and other data have it

    • Tell me how to put the listener in onBindViewHolder? - Taras Zhupnik
    • one
      Just like for normal View. Just someView.setOnClickListener(...); - Yuriy SPb
    • excuse me please, but I can’t expose the listener by myself ... I am just starting to program. If you could throw how it should look in my code I would be very grateful (I try holder.setOnClickListener (...) and it does not work) - Taras Zhupnik
    • one
      @TarasZhupnik, holder - not View, therefore it does not work. Hang the listener on View. like this: holder.cv_forecast.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){Log.d("TAG", "НАЖАТО");}}); - Yuriy SPb