【问题标题】:ListView view recycling messes up rowsListView 视图回收弄乱了行
【发布时间】:2015-04-12 11:50:32
【问题描述】:

我有一个显示篮球队日程的应用。它列出了他们已经玩过的游戏,然后是即将到来的游戏。我正在根据是否玩过游戏来更改行布局。例如,如果游戏已经玩过,我会在 TextView 中显示“W”或“L”,但如果游戏还没有玩过,我会隐藏这个 TextView。

我的问题是,当我滚动到底部时,所有 TextView 都被隐藏,然后滚动回顶部,所有开始的“L”和“W”现在都在隐。有没有办法防止这种情况发生?这是我的适配器:

package com.example.sixerstrivia;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class GameAdapter extends ArrayAdapter<Game> {

    public GameAdapter(Context context, ArrayList<Game> games) {
        super(context, R.layout.game_row, games);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Game game = getItem(position);

        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(
                    R.layout.game_row, parent, false);
        }

        TextView tvTopTeam = (TextView) convertView
                .findViewById(R.id.tvTopTeam);
        TextView tvBottomTeam = (TextView) convertView
                .findViewById(R.id.tvBottomTeam);
        TextView tvTopScore = (TextView) convertView
                .findViewById(R.id.tvTopScore);
        TextView tvBottomScore = (TextView) convertView
                .findViewById(R.id.tvBottomScore);
        TextView tvAt = (TextView) convertView.findViewById(R.id.tvAt);
        TextView tvVs = (TextView) convertView.findViewById(R.id.tvVs);
        TextView tvWin = (TextView) convertView.findViewById(R.id.tvWin);
        TextView tvDate = (TextView) convertView.findViewById(R.id.tvDate);

        if (game.completed) {
            tvDate.setText(game.gameDate);
            if (game.win) {
                tvWin.setText("W");
                tvWin.setTextColor(Color.GREEN);
            } else {
                tvWin.setText("L");
                tvWin.setTextColor(Color.RED);
            }
        } else {
            tvDate.setText(game.gameDate + "\n" + game.gameTime + " PM");
            tvWin.setVisibility(View.GONE);
            tvTopScore.setVisibility(View.INVISIBLE);
            tvBottomScore.setVisibility(View.INVISIBLE);
        }

        if (!game.homeTeam.equals("PHI")) {
            tvTopTeam.setText(game.awayTeam);
            tvVs.setVisibility(View.GONE);
            if (game.completed) {
                tvAt.setVisibility(View.GONE);
                tvTopScore.setText(game.awayScore);
                tvBottomScore.setText(game.homeScore);
            }
            tvBottomTeam.setText(game.homeTeam);
        } else {
            tvTopTeam.setText(game.homeTeam);
            tvAt.setVisibility(View.GONE);
            if (game.completed) {
                tvVs.setVisibility(View.GONE);
                tvTopScore.setText(game.homeScore);
                tvBottomScore.setText(game.awayScore);
            }
            tvBottomTeam.setText(game.awayTeam);
        }

        return convertView;
    }
}

【问题讨论】:

  • 使用 viewHolder。它可以解决您的问题

标签: android listview recycle


【解决方案1】:

你隐藏它们,但你不会让它们再次可见。由于列表视图重用视图,它们保持隐藏状态。如果比赛已经进行,请致电tvWin.setVisibility(View.VISIBLE);。还可以使用查看器来提高性能。

【讨论】:

  • 只需要迁移到 RecyclerView 是值得的,因为它强制执行 ViewHolders。
猜你喜欢
  • 2013-09-07
  • 1970-01-01
  • 1970-01-01
  • 2016-02-23
  • 2018-04-19
  • 2022-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多