2014-01-17 103 views
0

我有一个ListView包含RelativeLayout -Items与Text,Buttons和东西。我想对用户点击按钮作出反应,但由于某种原因,onItemClick未被解雇Android - Listview忽略TouchEvent

您是否有任何想法可能导致这种情况?

storeItems = (ListView) findViewById(R.id.st_storeitems); 
storeItems.setOnItemClickListener(new OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) 
    { 
     Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_LONG).show();  
     if(v.getId() == R.id.csi_buy) { 
      // Some Code 
     } 
    } 
}); 

回答

3

您可以添加

android:descendantFocusability="blocksDescendants" 

到根/顶面布置图上你的ListView的行或

添加以下属性

android:focusable="false" 
android:focusableInTouchMode="false" 

的按钮,在ListView行项目。这是阻止onItemClick被调用的原因。你也应该避免类似的事情

if (v.getId() == R.id.csi_buy) 

onItemClick,因为返回的观点是行,而不是你所点击的项目。要管理视图的点击,您应该使用View.OnClickListener

+0

也可以使用android:descendantFocusability =“blocksDescendants”到xml中的根元素 – Raghunandan

+0

@Raghunandan我从来没有使用它。任何副作用? – Blackbelt

+1

不认为有任何副作用。 http://developer.android.com/reference/android/view/ViewGroup.html#attr_android:descendantFocusability – Raghunandan