2

我有一个ListView,并且我有一个用于在选择/按下时突出显示每一行Drawable的每个项目。ListView我还有一个自定义适配器,我在其中以编程方式设置每行的背景颜色(我想要交替的背景颜色)。这是我添加的一项新功能,在添加代码之前,行会突出显示蓝色,但之后不会突出显示。不知道如何修复它。这是我所拥有的:

ListView 项目布局

<?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="fill_parent" 
    android:orientation="vertical"
    android:paddingLeft="5dip"
    android:paddingRight="5dip"
    android:paddingTop="8dip"
    android:paddingBottom="8dip"
    android:background="@drawable/app_selector"
>    
    <TextView  
        android:id="@+id/text"
        style="@style/ListingTitle"
        android:layout_alignParentTop="true"
        android:paddingBottom="2dip"
    />

</RelativeLayout>

可绘制

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" android:drawable="@color/light_blue" />

    <item android:state_selected="true" android:state_pressed="false" android:drawable="@color/light_blue" />

    <item android:state_activated="true" android:state_selected="false" android:state_pressed="false" android:drawable="@color/light_blue" />

    <item android:state_selected="false" android:state_pressed="false" android:drawable="@android:color/transparent" />

</selector>

分段

public class ListingFragment extends SherlockListFragment
{
    public void onActivityCreated(final Bundle icicle)
    {    
        ListView lv = getListView();

        mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
        mListView.setMultiChoiceModeListener(new MultiChoiceModeListener() {

                @Override
                public void onItemCheckedStateChanged(android.view.ActionMode mode, int position, long id, boolean checked) {
                }

                @Override
                public boolean onActionItemClicked(android.view.ActionMode mode, android.view.MenuItem item) {
                }
        }
    }

     /* ADAPTER */
     private class CustomAdapter extends BaseAdapter 
     {       
        @Override
        public View getView(final int position, View convertView, final ViewGroup parent)
        {
            final ViewHolder holder;
            if (convertView == null)
            {
                holder = new ViewHolder();

                convertView = mInflater.inflate(R.layout.item, parent, false);
            }
            else 
            {
                holder = (ViewHolder)convertView.getTag();
            }

            if (position % 2 == 0)
                convertView.setBackgroundColor(Color.GRAY);
            else
                convertView.setBackgroundColor(Color.TRANSPARENT);

            return(convertView);
        }
    }

}
4

2 回答 2

6

扩展 dum 的答案,您不需要在代码中完成所有这些工作。

可绘制 A

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/light_blue" />
    <item android:state_selected="true" android:state_pressed="false" android:drawable="@color/light_blue" />
    <item android:state_activated="true" android:state_selected="false" android:state_pressed="false" android:drawable="@color/light_blue" />
    <item android:state_selected="false" android:state_pressed="false" android:drawable="@android:color/transparent" />
</selector>

可绘制 B

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/light_blue" />
    <item android:state_selected="true" android:state_pressed="false" android:drawable="@color/light_blue" />
    <item android:state_activated="true" android:state_selected="false" android:state_pressed="false" android:drawable="@color/light_blue" />
    <item android:state_selected="false" android:state_pressed="false" android:drawable="@android:color/gray" />
</selector>

在您的适配器中:

if (position % 2 == 0) {
    convertView.setBackgroundResource(R.drawable.A);
} else {
    convertView.setBackgroundResource(R.drawable.B);
}
于 2013-08-05T02:17:06.383 回答
0

不要用纯色更改可绘制对象,而是首先以编程方式创建一个新的可绘制对象并像在 xml 中一样设置状态,然后为每个状态设置颜色。然后尝试用您以编程方式创建的 drwable 替换您的背景。

于 2013-08-05T01:08:22.690 回答