2015-02-11 113 views
0

我有一個應用程序顯示一個籃球隊的時間表。它列出他們已經玩過的遊戲,然後列出即將到來的遊戲。根據是否玩過遊戲,我正在改變行佈局。例如,如果遊戲已經播放,我在TextView中顯示「W」或「L」,但如果遊戲未播放,我隱藏了此TextView。ListView查看回收行弄錯

我的問題是,當我滾動到底部,其中所有的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; 
    } 
} 
+0

使用viewHolder。它可以解決你的問題 – 2015-02-11 22:27:58

回答

1

您可以隱藏它們,但不會讓它們再次顯示。由於listview重用視圖,它們保持隱藏狀態。如果比賽已進行,請致電tvWin.setVisibility(View.VISIBLE);。也使用視圖來表現。

+0

僅僅因爲它強制執行ViewHolders而轉移到RecyclerView是值得的。 – 2015-02-11 22:49:58

+0

啊,這可能只是這個簡單的大聲笑我會嘗試,謝謝 – user1282637 2015-02-11 22:50:38