我有一個recyclerView,它使用適配器綁定視圖並在每個視圖上執行單擊按鈕。但是,當我點擊項目1,然後項目+ = 7(例如:行0,7,14,...將由於onClick事件觸發器而被更改UI)也點擊。請幫幫我。 這裏是我的viewholder代碼:RecyclerView onClick每7行調用一次
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;
}
我在這裏呼籲onClick事件,我不得不刪除一些定義,因爲計算器不會讓我張貼太多代碼:
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));
}
}
}
});
}
}
而且OnBindViewHolder代碼:
@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);
更新:我已經弄清楚如何解決這個問題。只需使用rclMatchItem.setItemViewCacheSize(100);
設置recyclerView緩存視圖是問題,也許默認是7. 謝謝你們無論如何。
您是否在點擊偵聽器的視圖更改的其他情況下添加了默認視圖? –
是的,我在佈局文件中設置了默認視圖。這是問題嗎? – CloudKlose
檢查我的回答 –