我正在使用recycler-view
,我希望奇数行上的项目具有较深的色彩,现在我已经设法在OnBindViewHolder中做到这一点,但是问题是,如果我更改DataSet然后有彼此相同颜色的行...
我了解到,如果使用notifyDataSetChanged
可以解决此问题,但是据我了解,使用notifyDataSetChanged
效率不高,因此我正在寻找更好的解决方法。
预先非常感谢。
public void onBindViewHolder(@NonNull StandingsHolder holder, int position) {
StandingsDetail currentStandingsDetail = standingsDetail.get(position);
String playerName = (position+1) + " " + currentStandingsDetail.player.getFirstName() + " " + currentStandingsDetail.player.getLastName();
holder.name.setText(playerName);
holder.rating.setText(String.valueOf(currentStandingsDetail.standings.getRating()));
holder.games.setText(String.valueOf(currentStandingsDetail.standings.getGames()));
holder.wins.setText(String.valueOf(currentStandingsDetail.standings.getWins()));
holder.losses.setText(String.valueOf(currentStandingsDetail.standings.getLosses()));
holder.goals.setText(String.valueOf(currentStandingsDetail.standings.getGoals()));
holder.assists.setText(String.valueOf(currentStandingsDetail.standings.getAssists()));
if(position % 2 == 1){
holder.itemView.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(context, R.color.dark_overlay)));
}
}
答案 0 :(得分:1)
您必须为奇数行和偶数行显式设置背景色。
这很重要,因为从奇数编号的行回收的一行将具有深色背景。如果此行碰巧被重复使用以显示偶数行,则背景色将错误。