android:如何禁用listView中父视图的选定和按下属性

发布于 2024-12-11 02:11:38 字数 1895 浏览 1 评论 0原文

我有一个包含按钮的自定义行的 listView,并使用自定义的cursorAdapter 填充 listView。我为按钮设置了一个选择器,以在按下按钮时更改其背景,并且当我按下按钮时它效果很好。问题是当我按行上的其他任何地方时它也有效。

我尝试将父视图的 .setPressed、.setSelected、.setFocusable、.setFocusableInTouchMode、.setClickable 属性设置为 false,但它不起作用。

我还尝试将按钮的 .setDuplicateParentStateEnabled 设置为 false,但也没有帮助。

如果您能帮助我,我将不胜感激,

提前谢谢

您让我扩展我的问题:我首先有没有按钮的行(这是下面 xml 代码中 id 为“delete_button”的 TextView)。当我单击视图中的另一个按钮时,删除按钮滑入行中,并且另一个 TextView (id:list_title_text) 的背景更改为恒定颜色(因为我不想再单击它)

<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/title_rows_relativelayout"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    <TextView
        android:id="@+id/list_title_text"
        android:layout_alignParentLeft="true"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:textSize="25dp"
        android:textColor="@android:color/white"
        android:layout_alignParentRight="true"
        android:background="@drawable/selector_for_list_rows"
        android:paddingLeft="15dp"
        android:paddingBottom="5dp"
        android:paddingRight="15dp"
        android:gravity="center_vertical"
        android:typeface="normal" />
    </TextView>
    <TextView 
    android:id="@+id/delete_button"
    android:background="@drawable/selector_for_delete_button"
    android:layout_width="wrap_content" 
    android:layout_height="40dp"
    android:textSize="23dp"
    android:textColor="@android:color/white"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:gravity="center"
    android:layout_alignTop="@+id/row_icon_background" 
    android:layout_alignParentRight="true"
    android:layout_marginRight="5dp">
    </TextView>
 </RelativeLayout>

I have a listView with custom rows containing buttons and I populate the listView with a custom cursorAdapter. I have set a selector for the button to change the background of it when pressed and it works great when I press on the button. The problem is it also works when I press anywhere else on the row.

I tried to set the parent view's .setPressed, .setSelected, .setFocusable, .setFocusableInTouchMode, .setClickable attributes to false but it did not work.

I also tried to set button's .setDuplicateParentStateEnabled to false but it did not help either.

If you can help me I will be grateful,

Thank you in advance

Let me extend my question: I have the rows without the button (which is the TextView with id of "delete_button" in xml code below) first. When I click another button in the view, the delete buttons slide into the rows and the background of the other TextView (id:list_title_text) changes to a constant color (because I don't want it to be clickable anymore)

<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/title_rows_relativelayout"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    <TextView
        android:id="@+id/list_title_text"
        android:layout_alignParentLeft="true"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:textSize="25dp"
        android:textColor="@android:color/white"
        android:layout_alignParentRight="true"
        android:background="@drawable/selector_for_list_rows"
        android:paddingLeft="15dp"
        android:paddingBottom="5dp"
        android:paddingRight="15dp"
        android:gravity="center_vertical"
        android:typeface="normal" />
    </TextView>
    <TextView 
    android:id="@+id/delete_button"
    android:background="@drawable/selector_for_delete_button"
    android:layout_width="wrap_content" 
    android:layout_height="40dp"
    android:textSize="23dp"
    android:textColor="@android:color/white"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:gravity="center"
    android:layout_alignTop="@+id/row_icon_background" 
    android:layout_alignParentRight="true"
    android:layout_marginRight="5dp">
    </TextView>
 </RelativeLayout>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

飘然心甜 2024-12-18 02:11:38

选定、按下和激活分别通过 ViewGroup.dispatchSetSelected().dispatchSetPressed().dispatchSetActivated() 在子级上显式设置,无论子项的 DUPLICATE_PARENT_STATE 值如何。使用无操作覆盖父级中的这些方法以禁用这些行为。

默认情况下,焦点和按下分别向下传播到不可聚焦或不可单击的子项。在我的例子中,将适当的子项标记为可聚焦或可点击比 setDuplicateParentState(false) (这已经是 View 类的默认值)更准确。

对我来说,我发现 setDuplicateParentStateEnabled() 是一个转移注意力的东西,并且无需它就能解决我的问题。

Selected, pressed, and activated are explicitly set on children by ViewGroup.dispatchSetSelected(), .dispatchSetPressed(), and .dispatchSetActivated() respectively, regardless of the child's DUPLICATE_PARENT_STATE value. Override those methods in the parent with no-ops to disable these behaviors.

By default, focus and pressed are propagated down to children that are not focusable or clickable, respectively. In my cases, it was more accurate to mark the appropriate children focusable or clickable, than to setDuplicateParentState(false) (which is already the default for a View class).

For me, I found setDuplicateParentStateEnabled() to be a red herring, and was able to solve my problems without it.

弥繁 2024-12-18 02:11:38

我通过在自定义 ListAdapter 中将按钮的 setFocusable 属性设置为 true 解决了这个问题(在编写问题后,我将 CursorAdapter 更改为 ListAdapter,但我确信它也适用于 CursorAdapter)。现在,仅当我单击按钮时,按钮背景才会发生变化,而当我单击其他任何地方时,不会发生任何情况。

I solved this by setting the button's setFocusable attribute to true inside my custom ListAdapter (I changed my CursorAdapter to ListAdapter after I wrote the question but I am sure it will work with CursorAdapter too). Now the button background changes only when I click on the button and nothing happens when I click anywhere else.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文