试图抓住Android ListView项目的点击:android:descendantFocusability =“blocksDescendants”无法正常工作

试图抓住Android ListView项目的点击:android:descendantFocusability =“blocksDescendants”无法正常工作,第1张

概述这已在StackOverflow上的许多帖子中得到解决.最推荐的解决方案是设置:android:descendantFocusability="blocksDescendants"在列表项布局的根布局上.我做了以下事情.>在根上设置android:descendantFocusability=“blocksDescendants”列表项目布局.>在listView上调用setDescen

这已在StackOverflow上的许多帖子中得到解决.最推荐的解决方案是设置:

androID:descendantFocusability="blocksDescendants"

在列表项布局的根布局上.我做了以下事情.

>在根上设置androID:descendantFocusability =“blocksDescendants”
列表项目布局.
>在ListVIEw上调用setDescendantFocusability(BLOCK),以及
每个ListVIEwItem.
>在Imagebuttons上设置焦点=禁用

但是,当单击其中一个子Imagebuttons时,我无法触发ListVIEw或ListVIEw项上的任何侦听器.

下面是我用来呈现列表项的“模板”.

<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                                        androID:ID="@+ID/shiftbutton"                    androID:layout_wIDth="30dp"                    androID:layout_height="30dp"                    androID:background="@drawable/ic_action_forward"                    androID:layout_marginleft="10dp"/>            <Imagebutton                                        androID:ID="@+ID/repeatbutton"                    androID:layout_wIDth="30dp"                    androID:layout_height="30dp"                    androID:background="@drawable/ic_action_refresh"                    androID:layout_marginleft="10dp"/>            <Imagebutton                                        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  androID:text="OK"/>        <VIEw />        <button  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  androID:text="day"/>        <VIEw />        <button  androID:text="weekday"/>        <VIEw />        <button  androID:text="week"/>        <VIEw />        <button  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  androID:text="1 week"/>        <VIEw />        <button  androID:text="1 day"/>        <VIEw />        <button  androID:text="1 hour"/>        <VIEw />        <button  androID:text="10 mins"/>    </linearLayout></linearLayout>

我也在代码中设置了这个descendantFocusability,只是为了确定:

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;    }

在我的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>    . . .

单击列表项的任何部分(包括子TextVIEw元素)时,我收到了onClick.但是,当单击任何Imagebuttons时,单击不会被拾取. (顺便说一句,我已经在Imagebuttons中注册了OnClickListeners,并且它们正在被调用.)

已设定:

> ListVIEw上的setonItemClickListener
> ListVIEw上的setonItemSelectedListener
>每个ListVIEw项上的setonClickListener.

但是,如果单击其中一个Imagebutton,则不会调用这些调用.

任何帮助,将不胜感激.

解决方法:

尝试在每个按钮布局中使用此功能

 androID:focusable="false"androID:focusableIntouchMode="false"

并删除你正在为此做的其他事情.
也来自ListvIEw.
同时删除它

androID:descendantFocusability="blocksDescendants"

这也是

parent.setDescendantFocusability(VIEwGroup.FOCUS_BLOCK_DESCENDANTS);        ((VIEwGroup) v).setDescendantFocusability(VIEwGroup.FOCUS_BLOCK_DESCENDANTS);
总结

以上是内存溢出为你收集整理的试图抓住Android ListView项目的点击:android:descendantFocusability =“blocksDescendants”无法正常工作全部内容,希望文章能够帮你解决试图抓住Android ListView项目的点击:android:descendantFocusability =“blocksDescendants”无法正常工作所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1102093.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存