OnItemClickListener不响应
根原因:listview(或其他有adapter的view)没有获得焦点 列子: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"//误点一:fill_parent android:orientation="vertical" android:descendantFocusability="blocksDescendants"//误点二 > 正确应该是 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > 切记: adapter的xml的主layout(RelativeLayout)不能用fill_parent 原因有二。 一: 原因:在adapter的xml中设置了 android:descendantFocusability="blocksDescendants" 注释:下面的item就是ListView的item android:beforeFocusability viewgroup在子项处理之前获得焦点 android:afterFocusability viewGroup在子项处理之后获得焦点 android:blocksFocusability viewGroup阻止子项获得焦点(以此子项的子项就能获得焦点) 二:在layout_......设置了fill_parent, 三 Android 长按setOnItemLongClickListener 注意细节 原理是fill_parent原来就是要求本layout全屏,但listview(或其他有adapter的view)限制item的高宽。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 gridview.setOnItemLongClickListener( new OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub Log.e( "setOnItemLongClickListener" , "setOnItemLongClickListener" ); return true ; } }); gridview.setOnItemClickListener( new OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { Log.e( "setOnItemClickListener" , "setOnItemClickListener" ); } }); 1.如果返回false那么onItemClick仍然会被调用。而且是先调用onItemLongClick,然后调用onItemClick。 如果返回true那么click就会被吃掉,onItemClick就不会再被调用了。 2.监听onItemClick以及onItemLongClick影响弹出菜单吗? onItemClick不影响;onItemLongClick如果返回true那么就会吃掉click事件,导致菜单不能弹出。 3.如何让包含button的item也能弹出菜单,回调onItemClick以及onItemLongClick的监听器呢? 需要设置Button属性:android:focusable="false" android:focusable="false"android:longClickable="true" android:longClickable="true"否则无法收到onItemLongClick 本文转自lilin9105 51CTO博客,原文链接:http://blog.51cto.com/7071976/1216753,如需转载请自行联系原作者