带有RelativeLayout的ListView在按下时不突出显示背景

时间:2011-03-07 03:36:21

标签: android listview relativelayout

我想要一个元素列表(使用ListView),列表中的每个元素都使用相对布局进行样式设置。目前,项目正在正确显示,但是,由于某些原因,列表视图项目在单击时不会呈绿色亮起。这是为什么?

我已将所有代码删除为最小值,但仍然可以执行此操作。

活动类

package com.test;

import android.app.Activity;

public class My_ListView extends Activity
{
    private ListView_Adapter listViewAdapter;
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_listview);


        // initialise the list-view object
        listViewAdapter = new ListView_Adapter(this);
        listView = (ListView) findViewById(R.id.listview);
        listView.setAdapter(listViewAdapter);


        for (int i=0;i<20;i++)
        {
            listViewAdapter.add("item "+i);
        }

    }
    public void clicked(View v)
    {
        v.setBackgroundColor(0xFF0000FF);
    }
}

列表视图项适配器类

package com.test;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ListView_Adapter extends ArrayAdapter<String>
{
    public ListView_Adapter(Context c)
    {
        super(c, R.layout.my_listview_item);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View row = convertView;
        StationFinder_ListViewItemHolder holder = null;

        if (row == null)
        {
            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            row = inflater.inflate(R.layout.my_listview_item, parent, false);
            holder = new StationFinder_ListViewItemHolder(row);
            row.setTag(holder);
        }
        else
        {
            holder = (StationFinder_ListViewItemHolder) row.getTag();
        }

        holder.populateFrom(getItem(position));

        return row;
    }

    static class StationFinder_ListViewItemHolder
    {
        private TextView destination = null;

        StationFinder_ListViewItemHolder(View row)
        {
            destination = (TextView) row.findViewById(R.id.text1);
        }

        void populateFrom(String locationDistance)
        {
            destination.setText(locationDistance);
        }

    }
}

my_listview.xml

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

    <ListView android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />
</LinearLayout>

my_listview_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:onClick="clicked"
    >
    <TextView android:id="@+id/text1"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        />
</RelativeLayout>

1 个答案:

答案 0 :(得分:2)

我认为您可能需要在设置背景颜色后调用invalidate()

您可能希望通过为ListView设置listSelector和可能drawSelectorOnTop来执行此操作。这样,选择/取消选择和点击将以正常方式处理。

修改 - 此外,由于您使用的是ListView,因此您可能希望通过在ListView上设置OnItemClickListener来收听点击次数。