简体   繁体   English

Android - 更改ListView特定项目的背景颜色

[英]Android - Change background color of specific item of ListView

I'm making a game which will use a Listview to display some text as a chat. 我正在制作一款游戏,它将使用Listview将一些文字显示为聊天。

Some of these texts represents events , so I want to change their background color according to that specific event: EX. 其中一些文本代表事件 ,因此我想根据特定事件更改其背景颜色:EX。 Somebody gets killed, that specific Listview 's item will have a red background color 有人被杀, 特定Listview的项目将有红色背景颜色

How do I achieve this? 我该如何实现这一目标?

This is my JAVA code: 这是我的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);
}   

And this is my XML code: 这是我的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>

use this code: 使用此代码:

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

Override getView for ArrayAdapter . 覆盖getView for ArrayAdapter Based on the condition change color or do what is required. 根据条件变化颜色或做必要的事情。 You have already provided params to ArrayAdapter constructor. 您已经为ArrayAdapter构造函数提供了参数。 So there is no need to inflate the view again. 所以不需要再次膨胀视图。 Get the same and do what is required. 得到相同的,并做所需的事情。 Similarly getItem(position) get's the data at a specidifed position. 类似地, getItem(position)获取指定位置的数据。

http://developer.android.com/reference/android/widget/ArrayAdapter.html#getItem(int) 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;
  }
});

In your adapter class where you are binding the view, you can check for the particular condition and set the color in following way.This way you can set the color of the text or background dynamically. 在绑定视图的适配器类中,可以检查特定条件并按以下方式设置颜色。这样,您可以动态设置文本或背景的颜色。 Hope this helps. 希望这可以帮助。 I am using recycler viewer here which over rides onBindViewHolder. 我在这里使用的是Recycler viewer,它超越了onBindViewHolder。 Your adapter should have the method which binds view. 您的适配器应具有绑定视图的方法。

@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);
     }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM