我有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的触摸并获取项目编号?
private AdapterView.OnItemClickListener mLayersListListener = new AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//here touch on ImageView or TextView?
}
};
答案 0 :(得分: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();
}
});
通过&#34;获取项目编号&#34;我假设你的意思是获得列表视图位置?使用标记对象是传递此信息的一种可能方式。
但为什么不在列表中使用setOnItemClickListener()?用户可以单击图像或文本,但此处理程序干净地传递列表项的位置:
ListView mList = ...;
mList.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
// position is the item number
}
});
}