2016-01-15 126 views
0

我已經提到recycling mechanism here如何以編程方式切換Android中的listView中textview的可見性?

我有一個玩過的遊戲的歷史列表。遊戲可以是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> 

當我運行這個最初的值時顯示正確,但是當我滾動時,即使可見文本也被隱藏了爲什麼這樣呢?

回答

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

其監守你如何檢查空字符串的...更改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(); 

} 
相關問題