【问题标题】:Android - Change background color of specific item of ListViewAndroid - 更改 ListView 特定项目的背景颜色
【发布时间】:2016-01-10 14:33:51
【问题描述】:

我正在制作一个游戏,它将使用 Listview 来显示一些文本作为聊天。

其中一些文本代表事件,所以我想根据该特定事件更改它们的背景颜色: 前任。有人被杀,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>

【问题讨论】:

  • 您可以使用具有颜色的 setter 和 getter 以及位置(listitem 的索引)的模型类轻松做到这一点
  • 我可以问一个例子@Raghunandan
  • 如果您先发布代码,您将获得更好的解决方案。至少您尝试过的相关部分
  • 当然,编辑@Raghunandan
  • 不工作@Raghunandan

标签: android listview android-listview


【解决方案1】:

使用此代码:

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

【讨论】:

    【解决方案2】:

    ArrayAdapter 覆盖getView。根据情况更改颜色或执行所需的操作。您已经为 ArrayAdapter 构造函数提供了参数。所以没有必要再次膨胀视图。做同样的事,做需要做的事。同样getItem(position) 获取指定位置的数据。

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

    【讨论】:

    • 嘿,我如何自动获取我要添加的新项目的当前位置的 int?我正在创建一个 redEvent() 方法,该方法将消息的字符串作为参数,然后它应该将该项目的背景变为红色,我如何获得该项目的位置或焦点?谢谢
    • 获取项目在getView中的当前位置。见第一个参数。如果您要添加新项目,您可以添加到列表中。 getItem(Position) 将获取该位置的项目,这是一个字符串,并将其与 SomeBody 进行比较,如果相等,则背景将变为红色
    • 是的,事实是我想通过比较字符串而不是通过比较字符串来将项目变为红色,但是当我使用“redEvent()”方法添加它们时。所以问题是,在那个适配器之外,我如何同时获得我正在创建的新项目的位置?
    • 答案是getview。使用字符串添加新项目并调用 adapter.notifyDatasetchanged() 进行刷新
    • 添加新项目并刷新列表视图。 Getview 将完成这项工作
    【解决方案3】:

    在您绑定视图的适配器类中,您可以检查特定条件并按以下方式设置颜色。这样您可以动态设置文本或背景的颜色。希望这可以帮助。 我在这里使用了超越 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);
         }
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-10
      • 1970-01-01
      • 2011-01-14
      • 1970-01-01
      • 2016-03-13
      • 1970-01-01
      • 2013-06-03
      • 1970-01-01
      相关资源
      最近更新 更多