1

我的列表视图包含 2 个 TextViews 和一个 ImageView。我使用了一个适配器来填充 ListView。当我单击一个行项目时,我希望位于单击项目的背景图像进行更改。我正在使用如下所示的 setOnItemClickListener()

lvRMultiple.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

             mp3Url = trackList.get(position).getPreview().toString();
             title = trackList.get(position).getTitle().toString();
             playStreamMusic();



            Toast.makeText(getBaseContext(), "Item Clicked :)", Toast.LENGTH_LONG).show();
        }
    });

我的 getView() 方法如下:

public View getView(int position, View convertView, ViewGroup parent) {
        vi = convertView;

        if (convertView == null) {

            vi = inflater.inflate(R.layout.ecran_multiple_row, null);
            holder = new ViewHolder();

            holder.txIndex = (TextView) vi.findViewById(R.id.txIndex);
            holder.txSTitle = (TextView) vi.findViewById(R.id.txSTitle);
            holder.btOnOFF = (ImageView) vi.findViewById(R.id.btOnOFF);
            vi.setTag(holder);


        } else
            holder = (ViewHolder) vi.getTag();


        return vi;
    }



}

如何更改项目选择的 imageView 背景?

4

2 回答 2

3

您需要使用选择器。它是一个 xml 文件,它引用了多个可绘制对象或颜色,并告诉 Android 何时应该使用它。

是这样的:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_enabled="false"><color android:color="#50b1b1b1" />
</item>
<item android:drawable="@drawable/borderless_background_shape" android:state_enabled="true" android:state_pressed="true"/>
<item android:drawable="@drawable/borderless_background_shape" android:state_enabled="true" android:state_focused="true"/>
<item android:state_enabled="true"><color android:color="@android:color/transparent" />
</item>

</selector>

看看这个以获取更多信息:http: //developer.android.com/guide/topics/resources/drawable-resource.html#StateList

于 2013-06-28T08:49:13.680 回答
2

尝试这个 :

 vRMultiple.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {

         mp3Url = trackList.get(position).getPreview().toString();
         title = trackList.get(position).getTitle().toString();
         playStreamMusic();

         //find your ImageView using the view provided by the event
         ImageView imgview = (ImageView) view.findViewById(R.id.btOnOFF);
         //And change its background here
         imgview.setBackgroundDrawable(....);


         Toast.makeText(getBaseContext(), "Item Clicked :)", Toast.LENGTH_LONG).show();
    }
});   
于 2013-06-28T08:49:41.850 回答