2011-06-04 82 views
0

在谷歌博客的片段佈局的附圖中,我在我的代碼中有一個列表視圖。我的問題是設置前兩個項目(亨利四世和亨利五世)的背景顏色紅色和其他人仍然作爲默認backgroudn值。這可能嗎?ListView - 設置不同背景顏色的前兩個項目

任何幫助將非常感激。

謝謝, 薩那。

,我用它來獲取ListView控件的代碼是

Log.i(TAG, "Selected item position = "+getListView().getCount()+" Child count = "+getListView().getChildCount()); 
for (int i=0; i<getListView().getCount();i++) { 
    Log.i(TAG, "Item id = "+getListView().getItemIdAtPosition(i)); 
    View v = getActivity().findViewById((int) getListView().getItemIdAtPosition(i)); 
    View v1 = getListView().getChildAt(i); 
    Log.i(TAG, "Condition = "+(v == null)); //Here I get the display as true 
    Log.i(TAG, "Condition1 = "+(v1 == null)); //Here I get again a true, so basically both are none! 
} 

enter image description here

+0

PLZ添加您的getView()代碼 – 2011-06-04 06:27:57

回答

1

是的,它是可能的...請查看以下鏈接

http://www.androidpeople.com/android-custom-listview-tutorial-part-1

http://www.androidpeople.com/android-custom-listview-tutorial-part-2

並添加一個在自定義listview裏面使用下面的代碼。

public View getView(int position, View convertView, ViewGroup parent) { 
    ...... your code add extra line. 
    if (position == 0 || position == 1){ 
    holder.layout.setBackgroundColor(Color.RED); 
    } 
} 
+0

這是一個簡單的ListView,我只是用一個setListAdapter(新ArrayAdapter (getActivity(), android.R.layout.simple_list_item_activated_1,object.from));誇大名單。我不建立一個用於充氣的自定義適配器! – Sana 2011-06-04 06:37:31

2

最好的辦法是保持一個數據項的國王,並使用陣列適配器,使它們,您可以更改通知,並設置適當的顏色,因爲它渲染。

的你以後完整的例子是在這裏:

public class TestStuff extends Activity { 

    private class KingItem { 
     public KingItem(String name, String description) { 
      this.name = name; 
      this.description = description; 
      this.hexColor = "#000000"; 
     } 
     public String name; 
     public final String description; 
     public String hexColor; 
    } 

    private List<KingItem> kings = new ArrayList<TestStuff.KingItem>(); 
    private ListView kingsList; 
    private ArrayAdapter<String> kingAdapter; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     kings.add(new KingItem("Henry IV(1)", "Description form Henry IV(1)")); 
     kings.add(new KingItem("Henry V", "Description for Henry V")); 
     kings.add(new KingItem("Henry VIII", "Description for Henry VIII")); 
     kings.add(new KingItem("Richard II", "Description for Richard II")); 

     kingsList = (ListView) findViewById(R.id.list); 
     if (kingAdapter == null) { 
      kingAdapter = new KingsListAdapter(this, this, android.R.layout.simple_list_item_1, new ArrayList<String>()); 
     } 
     kingAdapter.clear(); 
     kingsList.setAdapter(kingAdapter); 
     kingsList.setOnItemClickListener(new OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       TextView desc = (TextView) TestStuff.this.findViewById(R.id.text); 
       KingItem king = kings.get(position); 
       desc.setText(king.description); 
       king.hexColor = "#0000ff"; 
       for (int i=0; i<position; i++) { 
        kings.get(i).hexColor = "#ff0000"; 
       } 
       for (int i=position+1; i<kings.size(); i++) { 
        kings.get(i).hexColor = "#000000"; 
       } 
       kingAdapter.notifyDataSetChanged(); 
      } 
     }); 
     for (KingItem king : kings) { 
      kingAdapter.add(king.name); 
     } 
     kingAdapter.notifyDataSetChanged(); 
    } 

    private class KingsListAdapter extends ArrayAdapter<String> { 

     private final Activity activity; 
     private final int textViewResourceId; 

     public KingsListAdapter(Activity activity, Context context, int textViewResourceId, List<String> kingsNames) { 
      super(context, textViewResourceId, kingsNames); 
      this.activity = activity; 
      this.textViewResourceId = textViewResourceId; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View v = convertView; 
      if (v == null) { 
       LayoutInflater vi = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       v = vi.inflate(textViewResourceId, null); 
      } 
      KingItem kingItem = kings.get(position); 

      TextView text1 = (TextView) v.findViewById(android.R.id.text1); 
      text1.setText(kingItem.name); 
      v.setBackgroundColor(Color.parseColor(kingItem.hexColor)); 
      return v; 
     } 

    } 
} 

這將每一個項目渲染到當前選定爲紅色,選擇一個藍色,當前選定項目後,任何事情都會發生回到黑色背景。

注意我只是將KingItem設置爲正確的顏色,然後告訴列表重新渲染。沒有必要得到意見,或父母,並嘗試巧妙地設置背景。相反,我將這些信息保存在KingItem中,並使適配器變得非常簡單。

相關問題