试图抓住对 android ListView 项目的点击:android:descendantFocusability="blocksDescendants" not working
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20697534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trying to catch a click on android ListView item: android:descendantFocusability="blocksDescendants" not working
提问by Ben
This has been addressed in many posts on StackOverflow. The most recommended solution is to set:
StackOverflow 上的许多帖子都解决了这个问题。最推荐的解决方案是设置:
android:descendantFocusability="blocksDescendants"
on the root layout of the list item layout. I've done the following.
在列表项布局的根布局上。我做了以下工作。
- Set
android:descendantFocusability="blocksDescendants"
on the root list item layout. - Called
setDescendantFocusability(BLOCK)
on the listView, as well as each listViewItem. - Set
focus=disabled
on the ImageButtons
- 设置
android:descendantFocusability="blocksDescendants"
在根目录的项目布局。 setDescendantFocusability(BLOCK)
在 listView 以及每个 listViewItem 上调用。focus=disabled
在 ImageButton 上设置
However when one of the child ImageButtons is clicked, I can't get any listeners on the ListView
or ListView
Items to fire.
但是,当单击子 ImageButtons 之一时,我无法触发ListView
或ListView
Items上的任何侦听器。
Below is the "template" I am using to render a list item.
下面是我用来呈现列表项的“模板”。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/reminderItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants"
android:padding="0dp">
<RelativeLayout
android:id="@+id/topPartOfReminder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<LinearLayout
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/textLayout">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:fontFamily="sans-serif-light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22sp"
android:text="10:00"
android:textStyle="normal"
android:id="@+id/textTime"/>
<TextView
android:fontFamily="sans-serif-condensed"
android:textSize="9sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AM"
android:paddingLeft="2dp"
android:textStyle="normal"
android:id="@+id/textAmPm"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text=" - "
android:id="@+id/textDivider"/>
<TextView
android:fontFamily="sans-serif-light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="March 26"
android:id="@+id/textDate"/>
</LinearLayout>
<TextView
android:fontFamily="sans-serif-light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text="Blah blah blah blah"
android:id="@+id/textDescription"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/textLayout"
android:gravity="right">
<ImageButton
style="@style/HorizontalButton"
android:id="@+id/shiftButton"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/ic_action_forward"
android:layout_marginLeft="10dp"/>
<ImageButton
style="@style/HorizontalButton"
android:id="@+id/repeatButton"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/ic_action_refresh"
android:layout_marginLeft="10dp"/>
<ImageButton
style="@style/HorizontalButton"
android:id="@+id/tickButton"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/ic_action_done"
android:layout_marginLeft="10dp"/>
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:id="@+id/tickButtonsView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:padding="0dp"
android:orientation="horizontal"
android:visibility="gone">
<Button style="@style/HorizontalButton" android:text="OK"/>
<View style="@style/VerticalDivider"/>
<Button style="@style/HorizontalButton" android:text="Cancel"/>
</LinearLayout>
<LinearLayout
android:id="@+id/repeatButtonsView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:padding="0dp"
android:orientation="horizontal"
android:visibility="gone">
<Button style="@style/HorizontalButton" android:text="day"/>
<View style="@style/VerticalDivider"/>
<Button style="@style/HorizontalButton" android:text="weekday"/>
<View style="@style/VerticalDivider"/>
<Button style="@style/HorizontalButton" android:text="week"/>
<View style="@style/VerticalDivider"/>
<Button style="@style/HorizontalButton" android:text="month"/>
</LinearLayout>
<LinearLayout
android:id="@+id/shiftButtonsView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:padding="0dp"
android:orientation="horizontal"
android:visibility="gone">
<Button style="@style/HorizontalButton" android:text="1 week"/>
<View style="@style/VerticalDivider"/>
<Button style="@style/HorizontalButton" android:text="1 day"/>
<View style="@style/VerticalDivider"/>
<Button style="@style/HorizontalButton" android:text="1 hour"/>
<View style="@style/VerticalDivider"/>
<Button style="@style/HorizontalButton" android:text="10 mins"/>
</LinearLayout>
</LinearLayout>
Am also setting this descendantFocusability in the code, just to be sure:
我也在代码中设置了这个后代焦点,只是为了确定:
public class ReminderItemAdapter extends CursorAdapter {
. . .
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = mLayoutInflater.inflate(R.layout.reminder_item, parent, false);
parent.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
((ViewGroup) v).setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
return v;
}
And on the styling of my ImageButton's I've set focusable=false, just for good measure:
在我的 ImageButton 的样式上,我设置了 focusable=false,只是为了更好的衡量:
<resources>
<style name="HorizontalButton">
<item name="android:layout_width">0dip</item>
<item name="android:layout_weight">1</item>
<item name="android:layout_height">match_parent</item>
<item name="android:fontFamily">sans-serif</item>
<item name="android:textSize">12sp</item>
<item name="android:background">?android:attr/selectableItemBackground</item>
<item name="android:focusable">false</item>
<item name="android:focusableInTouchMode">false</item>
</style>
. . .
I am getting onClick's received when clicking on any part of the list item, including the child TextView elements. However when clicking on any of the ImageButtons, the click's don't get picked up. (BTW I have got OnClickListeners registered with the ImageButtons and they are getting called.)
单击列表项的任何部分(包括子 TextView 元素)时,我收到了 onClick。然而,当点击任何 ImageButtons 时,点击不会被拾取。(顺便说一句,我已经在 ImageButtons 上注册了 OnClickListeners 并且它们被调用了。)
Have set:
已设置:
- setOnItemClickListener on ListView
- setOnItemSelectedListener on ListView
- setOnClickListener on each ListView Item.
- ListView 上的 setOnItemClickListener
- ListView 上的 setOnItemSelectedListener
- 每个 ListView 项目上的 setOnClickListener。
But none of these get called if one of the ImageButtons is clicked.
但是,如果单击 ImageButtons 之一,这些都不会被调用。
Any help would be appreciated.
任何帮助,将不胜感激。
回答by keshav
Try using this inside each button layouts
尝试在每个按钮布局中使用它
android:focusable="false"
android:focusableInTouchMode="false"
and remove other thing which you are doing for this. Also from listview. Also remove this
并删除您为此所做的其他事情。也来自列表视图。也删除这个
android:descendantFocusability="blocksDescendants"
and this too
这也是
parent.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
((ViewGroup) v).setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
回答by Jawad Zeb
Just put
android:focusable="false"
in the row/item.xml of the listView .
只需放入
android:focusable="false"
listView 的 row/item.xml 即可。
It solve all your problem. if you put an event listener on the listView
.. It will not let you click on whole ListView Row. I mean
它解决了你所有的问题。如果你放一个event listener on the listView
.. 它不会让你点击整个 ListView 行。我的意思是
listView.setOnClickListener(this);
will not be called . because the focus moves to the row/item of the listView
. By adding android:focusable="false"
to the Button or ImageView on which you put an Event . It will lose their focus and the focus is gain by listView.Now you can have listView clickable as well as its descendents rows/item..
listView.setOnClickListener(this);
不会被调用。因为焦点移到row/item of the listView
. 通过添加 android:focusable="false"
到您放置 Event 的 Button 或 ImageView 。它将失去他们的焦点,而焦点是由 listView 获得的。现在你可以拥有listView clickable as well as its descendents rows/item..
In short just put android:focusable="false
in TextView/Button or ImageView
which have event Listeners in the Item/row.xml.
简而言之,只需android:focusable="false
在TextView/Button or ImageView
Item/row.xml 中放入 事件监听器即可。
回答by Richard Fung
Have you tried adding android:clickable="false" for the buttons? I just ran into a similar issue and that fixed it for me, whereas using android:focusable="false" did not.
您是否尝试过为按钮添加 android:clickable="false" ?我刚刚遇到了类似的问题,并为我修复了它,而使用 android:focusable="false" 却没有。
Good luck!
祝你好运!