如何从默认更改的ListView所选项目颜色为红色,而无需使用选择 [英] How to change ListView Selected items color from default to red without using selector

查看:115
本文介绍了如何从默认更改的ListView所选项目颜色为红色,而无需使用选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个App板球。在我的目的是选择球员为特定的团队ListView中。在这里,我可以能够从列表中选择多个玩家。我使用简单的适配器与多个选择列表视图。

I am developing an App for cricket. In that my aim is to select players for the particular team in ListView. Here I can able to select multiple players from the list. I am using simple Adapter with multiple choice Listview.

          adapter=new ArrayAdapter<String>(this,R.layout.custom_list_view,R.id.textView_color,playersName);
    lvview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

和我使用checkedTextView进行多选。下面是我用CheckedTextView custom_list_view

And I am using checkedTextView for multiple selection. Below is my custom_list_view with CheckedTextView

       <CheckedTextView
   android:id="@+id/textView_color"
   android:layout_width="fill_parent"
   android:layout_height="?android:attr/listPreferredItemHeight"
   android:textAppearance="?android:attr/textAppearanceLarge"
   android:gravity="center_vertical"
   android:checkMark="?android:attr/listChoiceIndicatorMultiple"
   android:paddingLeft="6dip"
   android:paddingRight="6dip"
   android:textColor="#FFffFF"
   />

和我现在的问题是,我想改变列表视图的颜色,当用户从列表中选择特定的播放器。它喜欢展示哪些球员被选中的用户。从非选定的球员我改变所选播放器的颜色为红色区分。

And now my problem is I want to change the color of listview when user select the particular player from the list. Its like to show the user which players are selected. To Differentiate from the unselected player I am changing the color of selected player to red.

    lvview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View v, int position,long id) {
            // TODO Auto-generated method stub
            SparseBooleanArray checked = lvview.getCheckedItemPositions();      

            if(checked.get(position))
            {
                //chkTextView.setTextColor(Color.GREEN);

                counter_selected++;
                selectedCounterText.setText("" + counter_selected);                 
            }
            else
            {
                counter_selected--;
                selectedCounterText.setText("" + counter_selected);
            }
        }
    }); 

如何选择的球员从默认颜色改变颜色为红色。我在努力做到这一点。请帮我找出来..

How to change the color of selected player from default color to Red. I am struggling to do that.. Please help me to find it out..

推荐答案

得到了通过使用下面的code答案

Got Answer by using the below code

在mainActivity适配器类

In mainActivity adapter class

   adapter=new ArrayAdapter<String>(getApplicationContext(), R.layout.text_view,R.id.textView1,players);

    lvview.setAdapter(adapter);

main.xml中看起来像下面

main.xml look like below

<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="8.5"
    android:cacheColorHint="#00000000" 
     />

和我的自定义布局场像下面

And my custom layout field is like below

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="23dp"
    android:text="TextView"
    android:textColor="#ffffff" />

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView1"
    android:layout_alignBottom="@+id/textView1"
    android:layout_alignParentRight="true"
    android:focusable="false"
  android:focusableInTouchMode="false"
    />

而在MainActivity onitem点击监听器的ListView我所谓的自定义布局视图和code以下

And in MainActivity onitem click listener for ListView I called the custom layout view and the code is given below

            lvview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> l, View v, int position,
                long id) {
            // TODO Auto-generated method stub
            SparseBooleanArray checked = lvview.getCheckedItemPositions();  
            checkedText=(CheckBox) v.findViewById(R.id.checkBox1);
            checkedList=(TextView) v.findViewById(R.id.textView1);
            if(checkedText.isChecked()==false)
            {
                counter_selected++;
                checkedText.setChecked(true);
                checkedList.setTextColor(Color.RED);
                selectedCounterText.setText("" + counter_selected);                 

            }
            else
            {
                counter_selected--;
                checkedText.setChecked(false);
                 checkedList.setTextColor(Color.WHITE);
                 selectedCounterText.setText("" + counter_selected);        
            }
        }
    });

和它解决我的问题。

这篇关于如何从默认更改的ListView所选项目颜色为红色,而无需使用选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆