我有玩游戏的历史清单。游戏可以是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>当我运行这个初始值时,会正确地显示值,但是当我滚动时,即使是可见的文本视图也是不可见的,为什么会这样呢?
发布于 2016-01-15 16:21:37
因为从我所能看到的情况来看,您永远不会将其设置为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);
}发布于 2016-01-15 16:23:47
因为你是如何检查空字符串的..。将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();
}https://stackoverflow.com/questions/34815347
复制相似问题