ListView视图回收混乱行

时间:2015-02-11 22:21:39

标签: android listview recycle

我有一个应用程序,显示篮球队的日程安排。它列出了他们已经玩过的游戏以及即将推出的游戏。我正在改变行布局,具体取决于游戏是否已经播放。例如,如果游戏已经播放,我在TextView中显示“W”或“L”,但如果游戏尚未播放,我隐藏了这个TextView。

我的问题是,当我滚动到底部,隐藏所有TextViews,然后滚动回到顶部,现在所有的“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;
    }
}

1 个答案:

答案 0 :(得分:1)

你隐藏它们,但是你不能再将它们看得见。由于listview重用了视图,因此它们保持隐藏状态。如果已经进行了比赛,请致电tvWin.setVisibility(View.VISIBLE);。还可以使用视图来提高性能。