Android 触按ListView中的ImageView
我有一个包含以下项目的列表视图:Android 触按ListView中的ImageView,android,android-listview,Android,Android 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_widt
<?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 MLayersListener
=新AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView父视图、视图、,
内部位置,长id){
//这里触摸ImageView或TextView?
}
}) 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
}
});
ListView-mList=。。。;
mList.setOnItemClickListener(新的OnItemClickListener()
{
public void onItemClick(AdapterView父对象、视图、整型位置、长id)
{
//位置是项目编号
}
});
}是,职位:)我更新帖子。请看这个。很抱歉,如果使用英语,您将无法获得触摸位置坐标,并且无法判断用户是否单击了图像或文本
ListView mList = ...;
mList.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
// position is the item number
}
});