如何在不堆积行的情况下更改奇数行的backgroundTint?

时间:2019-11-01 18:27:01

标签: android android-recyclerview recycler-adapter

我正在使用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)));
        }
    }

问题: https://imgur.com/a/TBTipZS

1 个答案:

答案 0 :(得分:1)

您必须为奇数行和偶数行显式设置背景色。

这很重要,因为从奇数编号的行回收的一行将具有深色背景。如果此行碰巧被重复使用以显示偶数行,则背景色将错误。