2012-08-09 135 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