RecyclerView беспорядок при переработке предметов и изменения backgroundTint - PullRequest
0 голосов
/ 02 ноября 2019

Я использую RecyclerView с ListAdapter, и когда я обновляю список, 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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...