RecyclerView在回收物品和更改background时会弄乱

时间:2019-11-01 21:31:23

标签: android android-studio android-recyclerview

我将RecyclerViewListAdapter一起使用,并且在更新列表时,RecyclerView会刷新, 问题是我希望奇数行具有深色覆盖,因此我在OnBindViewHolder中定义了奇数行具有深色覆盖,而偶数行具有浅覆盖,但是当我刷新列表时,RecyclerView保留了旧的backgroundTint,并且我还显示了索引(从1开始),并且索引也保持不变。 我该怎么做才能更新backgroundTint和索引?

非常感谢您:)

我尝试使用holder.setIsRecyclable(false);,但是会导致延迟。

    public GroupStandingsDetailAdapter() {
        super(DIFF_CALLBACK);
    }

    private static final DiffUtil.ItemCallback<StandingsDetail>  DIFF_CALLBACK = new DiffUtil.ItemCallback<StandingsDetail>() {
        @Override
        public boolean areItemsTheSame(@NonNull StandingsDetail oldItem, @NonNull StandingsDetail newItem) {
            return oldItem.player.id == newItem.player.id
                    && oldItem.group.id == newItem.group.id;
        }

        @Override
        public boolean areContentsTheSame(@NonNull StandingsDetail oldItem, @NonNull StandingsDetail newItem) {
            return oldItem.player.getFirstName().equals(newItem.player.getFirstName())
                    && oldItem.player.getLastName().equals(newItem.player.getLastName())
                    && oldItem.player.getBirthDate().equals(newItem.player.getBirthDate())
                    && oldItem.group.getTitle().equals(newItem.group.getTitle())
                    && oldItem.group.getDescription().equals(newItem.group.getDescription())
                    && oldItem.group.getCreationDate().equals(newItem.group.getCreationDate())
                    && oldItem.standings.getGames() == newItem.standings.getGames()
                    && oldItem.standings.getWins() == newItem.standings.getWins()
                    && oldItem.standings.getLosses() == newItem.standings.getLosses()
                    && oldItem.standings.getGoals() == newItem.standings.getGoals()
                    && oldItem.standings.getAssists() == newItem.standings.getAssists()
                    && oldItem.standings.getRating() == newItem.standings.getRating();
        }
    };

    //todo: fix wrong standings color when changed
    @NonNull
    @Override
    public StandingsHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.standings_item, parent, false);
        return new StandingsHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull StandingsHolder holder, int position) {
        StandingsDetail currentStandingsDetail = getItem(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 == 0)
            holder.itemView.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(context, R.color.light_overlay)));
        else
            holder.itemView.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(context, R.color.dark_overlay)));

    }

实际结果-问题: https://imgur.com/a/xrncEBl

0 个答案:

没有答案