2016-01-10 240 views
2

我正在製作一個遊戲,它將使用Listview來將某些文本顯示爲聊天。Android - 更改ListView特定項目的背景顏色

其中一些文字代表事件,所以我想根據該特定事件更改背景顏色: EX。有人就會被殺死,那是特定Listview項目將具有紅色背景顏色

如何實現這一目標?

這是我的Java代碼:

  //STARTING SETUP 
     ArrayList<String> arrayListMother; 
     ArrayAdapter<String> arrayAdapterMother; 
     ListView listViewMother; 

     int counter; 
     boolean introDone = false; 

     String[] night = {"Welcome!","This is Lupus In Tabula!","You're gonna experience the Hasbro Game in a total new way, have fun!"}; 
     String[] day = {"Thank you!","Great","Let's do it!"}; 
     String[] dead = {"Thank you!","Great","Let's do it!"}; 
     String[] saved = {"Thank you!","Great","Let's do it!"}; 

     Button buttonGo; 
     //ENDING SETUP 


     //CREATE - CREATE// 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_game); 
      setup(); 

     } 
     //CREATE - CREATE// 


    public void setup(){ 

    arrayListMother = new ArrayList<String>(); 
    arrayAdapterMother = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayListMother); 
    listViewMother = (ListView) findViewById(R.id.list_mother); 
    listViewMother.setAdapter(arrayAdapterMother); 
    buttonGo = (Button)findViewById(R.id.buttonGo); 
} 

這是我的XML代碼:

<?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:orientation="vertical"> 

<ListView 
    android:id="@+id/list_mother" 
    android:text="@string/go" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:textAppearance="?attr/textAppearanceListItem" 
    android:divider="@null" 
    android:layout_weight="7"/> 

<Button 
    android:id="@+id/buttonGo" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:text="@string/go" 
    android:textColor="@color/white" 
    android:background="@color/colorPrimary" 
    android:enabled="true" 
    android:onClick="play" /> 
</LinearLayout> 
+0

您可以用具有setter和getter模型類的顏色,與位置(列表項的指數) – Raghunandan

+0

請問爲例@Raghunandan – FET

+0

,如果你發佈你會得到一個更好的解決方案做到這一點很容易的代碼第一。至少你嘗試過的相關部分 – Raghunandan

回答

2

覆蓋getViewArrayAdapter。基於條件變化的顏色或做需要什麼。您已經提供了參數ArrayAdapter構造函數。因此沒有必要再次誇大觀點。獲取相同的內容並執行所需的操作。類似地,getItem(position)將數據存放在specidifed的位置。

http://developer.android.com/reference/android/widget/ArrayAdapter.html#getItem(int)

listViewMother.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayListMother) { 
@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View row = super.getView(position, convertView, parent); 

    if(getItem(position).equals("Somebody gets killed")) 
    { 
     // do something change color 
     row.setBackgroundColor (Color.RED); // some color 
    } 
    else 
    { 
     // default state 
     row.setBackgroundColor (Color.WHITE); // default coloe 
    } 
    return row; 
    } 
}); 
+0

嘿,我將如何自動獲得我要添加的新項目當前位置的整數?我創建了一個redEvent()方法,該方法將參數作爲參數傳遞給消息的String,然後它應該將該項目的背景變成紅色,我如何獲得該項目的位置或重點?謝謝 – FET

+0

您可以在getView中獲取該項目的當前位置。看到第一個參數。如果你正在添加新的項目,你可以添加到列表中。 getItem(Position)將得到該位置處的項目,這是一個字符串,並將其與SomeBody進行比較,如果等於背景將變爲紅色,則會被殺死。 – Raghunandan

+0

是的,事實是我想通過比較字符串而不是通過比較字符串來變紅。當我添加一個方法說「redEvent()」。所以問題是,在那個適配器之外,我如何獲得我在同一時間創建的新項目的位置? – FET

4

使用此代碼:

listViewMother.getChildAt(position).setBackgroundColor(
      Color.parseColor("#00743D")); 
1

在您的適配器類,你有約束力的觀點,你可以檢查特定的條件,並設置顏色以下way.This方式,你可以設置文本或背景的顏色動態。希望這可以幫助。 我在這裏使用回收瀏覽器,它在騎在onBindViewHolder上。你的適配器應該有綁定視圖的方法。

@Override 
public void onBindViewHolder(ViewHolder holder, int position) { 
    Person person = personList.get(position); 

    holder.username.setText(person.getName()); 
    holder.arriveTime.setText(DateFormat.format("hh:mm a",(person.getArriveTime() * 1000))); 


    if(person.getName().equals("NoVisitor")) 
    { 
     holder.username.setTextColor(Color.GRAY); 
     holder.arriveTime.setTextColor(Color.GRAY); 
     holder.leaveTime.setTextColor(Color.GRAY); 
    } 
    else 
    { 
     holder.username.setTextColor(Color.BLACK); 
     holder.arriveTime.setTextColor(Color.BLACK); 
     holder.leaveTime.setTextColor(Color.BLACK); 
    } 
} 
相關問題