CloudKlose CloudKlose - 8 months ago 81
Android Question

RecyclerView onClick called every 7 rows

I have a recyclerView that use a adapter to bind view and perform click button on each view. But when i click on item 1, then the item += 7(ex: row 0, 7, 14,... will be changed UI due to onClick event trigger) also click too. Please help me.
Here's my viewholder code:

public MatchItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View matchView = inflater.inflate(R.layout.match_item, parent, false);

MatchItemViewHolder matchItemViewHolder = null;
matchItemViewHolder = new MatchItemViewHolder(matchView);
return matchItemViewHolder;
}


I call onClick event here, i have to delete some definition because stackoverflow not let me post too much codes:

public class MatchItemViewHolder extends RecyclerView.ViewHolder {
public TextView txtTeamA;
public TextView txtTeamB;
public ImageView imvLive;
public Button btnPredict;

public boolean isLive = false;
public boolean hasUnderOver = false;

public MatchItemViewHolder(final View itemView) {
super(itemView);
txtTeamA = (TextView) itemView.findViewById(R.id.match_item_txt_teamA);
txtTeamB = (TextView) itemView.findViewById(R.id.match_item_txt_teamB);
txtRatioA = (TextView) itemView.findViewById(R.id.match_item_txt_ratioA);
txtRatioB = (TextView) itemView.findViewById(R.id.match_item_txt_ratioB);
txtPercentage = (TextView) itemView.findViewById(R.id.match_item_txt_percentage);
txtBetDirection = (TextView) itemView.findViewById(R.id.match_item_txt_betDirection);
txtMatchBo = (TextView) itemView.findViewById(R.id.match_item_txt_matchBo);
txtMatchNote = (TextView) itemView.findViewById(R.id.match_item_txt_matchNote);
txtMatchTime = (TextView) itemView.findViewById(R.id.match_item_txt_MatchTime);
txtMatchHour = (TextView) itemView.findViewById(R.id.match_item_txt_matchHour);
txtTourName = (TextView) itemView.findViewById(R.id.match_item_txt_TourName);
imvTeamA = (ImageView) itemView.findViewById(R.id.match_item_imv_TeamA);
imvTeamB = (ImageView) itemView.findViewById(R.id.match_item_imv_TeamB);
imvGameImage = (ImageView) itemView.findViewById(R.id.match_item_imv_GameImage);
imvLive = (ImageView) itemView.findViewById(R.id.match_item_imv_LiveImage);
btnPredict = (Button) itemView.findViewById(R.id.match_item_btn_Predict);

//set Item Click listener for item
btnPredict.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean winner = false;
PredictController predictController = new PredictController();
winner = predictController.getWinner(txtTeamA.getText().toString(), txtTeamB.getText().toString());
if (winner) {
txtBetDirection.setText("Xuôi nha");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
txtTeamA.setTextColor(context.getResources().getColor(R.color.colorPrimary, null));
txtTeamB.setTextColor(context.getResources().getColor(R.color.primary_text, null));
}else{
txtTeamA.setTextColor(ContextCompat.getColor(context, R.color.colorPrimary));
txtTeamB.setTextColor(ContextCompat.getColor(context, R.color.primary_text));
}
} else {
txtBetDirection.setText("Ngược nhé");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
txtTeamB.setTextColor(context.getResources().getColor(R.color.colorPrimary, null));
txtTeamA.setTextColor(context.getResources().getColor(R.color.primary_text, null));
}else{
txtTeamB.setTextColor(ContextCompat.getColor(context, R.color.colorPrimary));
txtTeamA.setTextColor(ContextCompat.getColor(context, R.color.primary_text));
}
}
}
});
}
}


And OnBindViewHolder Code:

@Override
public void onBindViewHolder(MatchItemViewHolder holder, int position) {
MatchesModel matchesModel = lstMatchItem.get(position);
holder.txtTeamA.setText(matchesModel.teamA);
holder.txtTeamB.setText(matchesModel.teamB);
holder.txtRatioA.setText(matchesModel.teamARatio);
holder.txtRatioB.setText(matchesModel.teamBRatio);
holder.txtPercentage.setText(matchesModel.teamPercentage);
holder.txtMatchBo.setText(matchesModel.matchBo);
if (matchesModel.matchDate != null) {
holder.txtMatchTime.setText(matchesModel.matchDate);
holder.txtMatchHour.setText(matchesModel.matchDate.split("\\s")[1]);
}
holder.txtMatchNote.setText(matchesModel.matchNote);
Picasso.with(context).load(matchesModel.teamAImage).fit().into(holder.imvTeamA);
Picasso.with(context).load(matchesModel.teamBImage).fit().into(holder.imvTeamB);
holder.txtTourName.setText(matchesModel.tourName);
Picasso.with(context).load(matchesModel.gameImage).fit().into(holder.imvGameImage);


UPDATE: I have figure it out how to solve the problem. Just use
rclMatchItem.setItemViewCacheSize(100);

Set recyclerView cache view is the problem maybe default it's 7.
Thank you guys anyway.

Answer Source

Thanks guys, i found the solution for this problem. In my Recycler Apdater I implement public long getItemId(int position) {return position;} Then use ItemCachedView for recyclerView rclMatchItem.setItemViewCacheSize(100); And i never get repeat item every 7 rows anymore.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download