【问题标题】:Android problem related OnItemClickListener of ListViewListView的OnItemClickListener相关的Android问题
【发布时间】:2011-08-25 09:26:45
【问题描述】:

我的布局中有一个 ListView。这是列表项的布局。

shopitem.xml

<?xml version="1.0" encoding="utf-8"?>
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android" 
    android:paddingTop="2dip"
    android:paddingBottom="2dip"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:mode="twoLine"> 
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/text1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingTop="6dip"
        android:paddingLeft="44dip"
        android:textStyle="bold" 
        android:lines="1"
        android:textColor="#FFFFFF"/>
    <ImageView
            android:id="@+id/playBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="3dip"
            android:layout_alignRight="@android:id/text1"
            android:src="@drawable/btnplaypreview"/>            
</TwoLineListItem>

现在我想在playBtn OnItemClickListener 中设置一些图像更改。为此,我正在使用以下代码。

ListView shopActivityListView = (ListView) findViewById(R.id.shopActivityListView);
shopActivityListView.setCacheColorHint(Color.TRANSPARENT);
shopActivityListView.setAdapter(new ArrayAdapter<String>(getApplicationContext(), R.layout.shopitem, trackArr[1]));

shopActivityListView.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View vItem, int position, long id) {
    // TODO Auto-generated method stub
    ImageView playBtn = (ImageView) vItem.findViewById(R.id.playBtn);
    playBtn.setImageResource(R.drawable.eq12);
}                
});

但是在 itemclick 上什么都没有发生。我检查了onItemClick 方法正在执行,没有任何异常。但一切都没有改变。我的代码有什么问题?

【问题讨论】:

  • TwoLineListItem 是线性布局扩展?尝试添加android:clickable="true"
  • 我试过了。没有变化。
  • 在 playBtn.setImageResource(R.drawable.eq12); 之后使您的列表视图无效。
  • 我在 playBtn.setImageResource(R.drawable.eq12) 之后添加了 shopActivityListView.invalidate(),但没有变化。
  • 谁能给我正确的答案?

标签: android listview imageview onitemclicklistener


【解决方案1】:

Listview 的任何一项中的图像都不能直接更改,因为它是由 Adapter 控制的。只有适配器的getView() 方法可以设置项目中的任何视图。 因此,在onItemClick 方法中,我们必须设置一个position 特定标志,然后为适配器调用notifyDataSetChanged()。此外,需要一个自定义适配器,我们必须在其中定义 getView() 方法,以便我们根据上述position 特定标志设置图像。

【讨论】:

  • 你能举个例子吗
猜你喜欢
  • 2011-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-28
  • 1970-01-01
相关资源
最近更新 更多