2012-08-09 137 views
0

的ImageView我的ListView的项目:触摸上的ListView

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/layerItem" 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
    <ImageView 
      android:layout_width="55dip" 
      android:layout_height="fill_parent" 
      android:id="@+id/layerImage"/> 
    <TextView 
      android:id="@+id/layerTitle" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:gravity="center_vertical" 
      android:paddingLeft="6.0dip" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:minHeight="?android:attr/listPreferredItemHeight"/> 
</LinearLayout> 

如何听触摸的ImageView并获得项目数量?

私人AdapterView.OnItemClickListener mLayersListListener =新AdapterView.OnItemClickListener(){

public void onItemClick(AdapterView<?> parent, View view, 
         int position, long id) { 
    //here touch on ImageView or TextView? 
} 

};

+0

与onItemClickListener你不会得到触摸位置的坐标并不能告诉我们,如果用户点击图片或文字 – CSmith 2012-08-09 18:37:06

回答

1

ImageButton可能是比ImageView更好的选择。无论哪种方式:

ImageButton mButton = (ImageButton)findViewById(R.id.layerImage); 
mButton.setTag(new Integer(position)); // position is the item number 
mButton.setOnClickListener (new OnClickListener() { 
public void onClick(View v) 
{ 
    // handle the image click/touch 
    Integer position = (Integer)v.getTag(); 
} 
}); 

“获得项目编号”我假设你的意思是获得列表视图的位置?使用标签对象是传递此信息的一种可能方式。

但是,为什么不在您的列表上使用setOnItemClickListener()?用户可以点击图片或文字,但此处理干净通过列表项的位置:

ListView mList = ...; 
mList.setOnItemClickListener(new OnItemClickListener() 
{ 
public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
{ 
    // position is the item number 
} 
}); 

}

+0

是的,位置:)我更新后。看到这个请求。对不起英文 – 2012-08-09 18:27:54

+0

编辑答案显示setOnItemClickListener – CSmith 2012-08-09 18:36:02