2016-01-10 233 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); 
    } 
} 
相关问题