1

我必须在列表视图中更改多个选定项目(文本视图)的颜色。我在这里做的是,当用户从列表视图中选择项目时,颜色应更改为蓝色,当用户取消选择项目时,颜色应更改为默认颜色(此处为黑色)。我已经完成了一些教程并实现了一个小演示。但我不明白,如何处理颜色变化。下面是我的代码...

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ListView
        android:layout_height="wrap_content"
        android:id="@+id/listView2"
        android:layout_width="match_parent">
    </ListView>
</LinearLayout>

列表项.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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

MainActivity.java

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    public View row;
    ListView lview;
    ListViewAdapter lviewAdapter;

    private final static String month[] = {"January","February","March","April","May",
        "June","July","August","September","October","November","December"};



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lview = (ListView) findViewById(R.id.listView2);
        lviewAdapter = new ListViewAdapter(this, month);

        lview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        lview.setAdapter(lviewAdapter);

        lview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub

            }
        });
    }




}

ListViewAdapter.java

public class ListViewAdapter extends BaseAdapter
{
    ArrayList<Boolean> saved = new ArrayList<Boolean>();
    Activity context;
    String title[];
    String description[];

    public ListViewAdapter(Activity context, String[] title) {
        super();
        this.context = context;
        this.title = title;

    }

    public int getCount() {
        // TODO Auto-generated method stub
        return title.length;
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    private class ViewHolder {
        TextView txtViewTitle;

    }




    public View getView(int position, View convertView, ViewGroup parent)
    {
        // TODO Auto-generated method stub
        ViewHolder holder;
        LayoutInflater inflater =  context.getLayoutInflater();

        if (convertView == null)
        {
            convertView = inflater.inflate(R.layout.listitem_row, null);
            holder = new ViewHolder();
            holder.txtViewTitle = (TextView) convertView.findViewById(R.id.textView1);

            convertView.setTag(holder);


        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.txtViewTitle.setText(title[position]);


    return convertView;
    }

}
4

5 回答 5

1
lview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {

                 TextView textView1 = (TextView) arg1.findViewById(R.id.textView1);
                   if(lv.isItemChecked(arg2))
                         textView1.setTextColor(your selected state color);
                   else
                       textView1.setTextColor(your unselected state color);

            }
        });
于 2013-08-13T05:02:27.083 回答
1

只需在 res/drawable 中创建 selector.xml,就像我在下面创建的一样:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" android:color="#fff"/>
    <item android:state_enabled="false" android:color="#cc003300"/>
    <item android:color="#cc003300"></item>

</selector>

然后将其分配给xml中的textview。

 android:textColor="@drawable/selector_btn_textcolor_green"

将您的代码更新为

<TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textColor="@drawable/selector_btn_textcolor_green"
        android:textAppearance="?android:attr/textAppearanceLarge" />
于 2013-08-13T05:03:08.083 回答
0

创建新的布局 xml 文件并从外部库中的 simple_list_item_1.xml 复制整个代码,然后根据您进行更改。

custom_text.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#1ca099"
android:textSize="20dp"
android:padding="5dp"
android:gravity="center_vertical"
 />

然后在activity.java文件中:

 ListView lst=(ListView) findViewById(R.id.listView);
     ArrayAdapter<String> adapter=new ArrayAdapter<String>(getApplicationContext(),R.layout.custom_text,cultural);
        lst.setAdapter(adapter);
于 2015-02-17T11:06:00.617 回答
0

首先,您应该获得 Item 的选定位置,因此您可以通过将选定的 Item 传递给 List onItemClickListener 中的适配器来做到这一点:在您的适配器类中添加:

public void setSelectedPosition(int pos)
{
    selectedPos = pos;
} 

在您的活动中,您调用适配器的此功能:

public void selectPosition(int position) {
    ((YourCustomAdapter)getListView().getAdapter()).setSelectedPosition(position);
}

并在 ListItemClick 的 List 监听器中:

YourList.setOnItemClickListener(new OnItemClickListener()
    {
        @Override public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
        {
             selectPosition(position);

        }
    });

这里 selectedPos 是一个全局变量,它接受您的 Selected Item ,因此您的 Adapter 中有选定的 Item,然后您可以在 getView() 函数中使用它:

if(selectedPos == position)
{
   rowView.setBackgroundResource(R.color.listselect_red);       
}
else
{
   rowView.setBackgroundResource(R.color.listselect_light_grey);
}
于 2013-08-13T05:06:44.603 回答
0

对于列表项,您可以将可绘制对象作为 xml 资源保存在可绘制文件夹中。因为您可以明确地处理不同颜色的按下、聚焦、正常情况。

于 2013-08-13T05:00:32.653 回答