我有一系列游戏历史记录。游戏可以是2名玩家,3名玩家,4名玩家 我有这个代码用于列表适配器中的getView函数 我根据游戏是由2人还是3人还是4人玩,使文本视图不可见
@Override
public View getView(int position, View convertView, ViewGroup parent) {
System.out.println("getview:" + position + " " + convertView);
//getting the layout inflater
LayoutInflater inflater = activity.getLayoutInflater();
//viewHolder object which is the object used for list values
ViewHolder holder;
//if convert view is null then the list view must be populated
if (convertView == null) {
//inflate the layout of the listview into convertview
convertView = inflater.inflate(R.layout.history_layout, null);
//new holder object
holder = new ViewHolder();
//setting the name of the player 1 to textView name1
holder.txtPlayer1 = (TextView) convertView.findViewById(R.id.name1);
//setting the name of the player 2 to textView name2
holder.txtPlayer2 = (TextView) convertView.findViewById(R.id.name2);
//setting the name of the player 3 to textView name3
holder.txtPlayer3 = (TextView) convertView.findViewById(R.id.name3);
//setting the name of the player 4 to textView name4
holder.txtPlayer4 = (TextView) convertView.findViewById(R.id.name4);
//setting tag to the convertView
convertView.setTag(holder);
} else {
//convertView to get the viewHolder object from its tag
holder = (ViewHolder) convertView.getTag();
}
//getting the map of values from the list's specific position
HashMap<String, String> map = list.get(position);
//setting the text of the player 1 name textView
holder.txtPlayer1.setText(map.get("PLAYER1"));
//setting the text of the player 2 name textView
holder.txtPlayer2.setText(map.get("PLAYER2"));
//if there was a player 3 in the game then his name is also set in the text View, or else
//the textview is made invisible
if (map.get("PLAYER3") != "") {
//setting player 3 name
holder.txtPlayer3.setText(map.get("PLAYER3"));
} else {
//setting player 3 textView invisible
holder.txtPlayer3.setVisibility(View.INVISIBLE);
}
//if there was a player 3 in the game then his name is also set in the text View, or else
//the textview is made invisible
if (map.get("PLAYER4") != "") {
//setting player 3 name
holder.txtPlayer4.setText(map.get("PLAYER4"));
} else {
//setting player 4 textView invisible
holder.txtPlayer4.setVisibility(View.INVISIBLE);
}
//returning the convertView
return convertView;
}
private static class ViewHolder {
//Player 1 Name
TextView txtPlayer1;
//Player 2 Name
TextView txtPlayer2;
//Player 3 Name
TextView txtPlayer3;
//Player 4 Name
TextView txtPlayer4;
}
这些是我的布局
列表视图
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="@drawable/menu_bg"
android:orientation="vertical"
android:weightSum="6">
<logic.main.com.boardgame.custom.CustomTextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="5"
android:background="@drawable/score1"
android:gravity="center"
android:text="History"
android:textColor="@color/fontcolor"
android:textSize="45sp"></logic.main.com.boardgame.custom.CustomTextView>
<ListView
android:id="@+id/history_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:divider="@android:color/black"
android:dividerHeight="2dp"
>
</ListView>
</LinearLayout>
列表行
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<logic.main.com.boardgame.custom.CustomTextView
android:id="@+id/name1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3.20"
android:background="@drawable/winner2"
android:gravity="center"
android:text="Imran"
android:textColor="@color/fontcolor"
android:textSize="15sp"
android:textStyle="bold" />
<View
android:layout_width="10dp"
android:layout_height="48dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:background="#46495A" />
<logic.main.com.boardgame.custom.CustomTextView
android:id="@+id/name2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3.20"
android:background="@drawable/score1"
android:gravity="center"
android:textColor="@color/fontcolor"
android:textSize="15sp"
android:textStyle="bold" />
<View
android:id="@+id/seprt2nd"
android:layout_width="10dp"
android:layout_height="48dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:background="#46495A" />
<logic.main.com.boardgame.custom.CustomTextView
android:id="@+id/name3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3.20"
android:background="@drawable/score1"
android:gravity="center"
android:textColor="@color/fontcolor"
android:textSize="15sp"
android:textStyle="bold" />
<View
android:id="@+id/seprt3rd"
android:layout_width="10dp"
android:layout_height="48dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:background="#46495A" />
<logic.main.com.boardgame.custom.CustomTextView
android:id="@+id/name4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3.20"
android:background="@drawable/score1"
android:gravity="center"
android:textColor="@color/fontcolor"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
当我运行这个时,最初的值显示正确,但是当我滚动时,即使可见的文本视图也变得不可见,为什么会这样呢?
答案 0 :(得分:3)
因为您永远不会从我能看到的地方将其设置回visible
。
将您的代码更改为此代码,它应该有效:(对PLAYER4
执行相同的操作):
if (map.get("PLAYER3") != "") {
//setting player 3 name
holder.txtPlayer3.setVisibility(View.VISIBLE);
holder.txtPlayer3.setText(map.get("PLAYER3"));
} else {
//setting player 3 textView invisible
holder.txtPlayer3.setVisibility(View.INVISIBLE);
}
答案 1 :(得分:1)
这是因为你要检查空字符串...将map.get("PLAYER3") != ""
改为!map.get("PLAYER3").isEmpty()
或!map.get("PLAYER3").equals("")
。您无法使用布尔运算符检查字符串上的相等性。
if (!map.get("PLAYER3").isEmpty()) {
holder.txtPlayer3.setVisibility(View.VISIBLE);
holder.txtPlayer3.setText(map.get("PLAYER3"));
} else {
holder.txtPlayer3.setVisibility(View.INVISIBLE);
}
接近ViewHolder模式的更好和更易读的方法是
private static class ViewHolder {
//Player 1 Name
TextView txtPlayer1;
//Player 2 Name
TextView txtPlayer2;
//Player 3 Name
TextView txtPlayer3;
//Player 4 Name
TextView txtPlayer4;
public ViewHolder(View view) {
txtPlayer1 = (TextView) convertView.findViewById(R.id.name1);
txtPlayer2 = (TextView) convertView.findViewById(R.id.name2);
txtPlayer3 = (TextView) convertView.findViewById(R.id.name3);
txtPlayer4 = (TextView) convertView.findViewById(R.id.name4);
}
}
在你的getView方法中你可以简单地执行此操作
if (convertView == null) {
convertView = inflater.inflate(R.layout.history_layout, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
//convertView to get the viewHolder object from its tag
holder = (ViewHolder) convertView.getTag();
}