如何防止ListItem内部的按钮被高亮显示

6

我有一个带有按钮的自定义列表项,用于ListView。当按下按钮时,该按钮显示替代图形以向用户提供反馈。但是,当我单击行时,每个按钮都显示为已按下状态,就好像我已经单击了它们。

我如何使按钮保持其原始状态而不是state_pressed状态?

布局/列表项:

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="horizontal"
    android:paddingBottom="10dp"
    android:paddingTop="10dp"
    android:descendantFocusability="blocksDescendants" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:gravity="center_vertical|left" >

        <TextView
            android:id="@+id/txtMain"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceLarge"
            style="@style/PrimaryText" />

        <TextView
            android:id="@+id/txtSub"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceLarge"
            style="@style/SecondaryText" />
    </LinearLayout>

    <ImageButton
        android:id="@+id/imbResponse"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:focusable="false"
        android:duplicateParentState="false"
        android:src="@drawable/response_btn" 
        android:contentDescription="@string/response"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="5dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="5dp" />
</LinearLayout> 

drawable/response_btn.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_focused="true" android:drawable="@drawable/res_m" />
    <item android:state_pressed="true" android:drawable="@drawable/res_m" />
    <item android:state_focused="false" android:state_pressed="false" android:drawable="@drawable/res_alt_m" />
</selector>

我尝试移除了state_focused和state_pressed,但似乎按钮从其父级继承了state_pressed状态。

4个回答

4
我发现将android:clickable="true"设置到父视图会防止子视图状态的改变。
参见这个答案

1
这会让我的应用程序更加努力工作,但它解决了问题:
...
mImageIcon.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                v.setBackgroundResource(R.drawable.my-background);
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                /*
                 * You can use another background rather than 0.
                 */
                v.setBackgroundResource(0);
                break;
            }

            return false;
        }// onTouch()
    });

你能解释一下这个解决方案吗? - Menna-Allah Sami

1

我也遇到了这个问题,我谷歌了大约3个小时并尝试了一些方法!现在我找到了解决方案。只需为子视图添加OnClickListener即可,在OnClick方法中甚至不用做任何事情。它适用于2.3模拟器和我的Nexus 5。


0

我认为你需要禁用父级的状态,方法是在 android:duplicateParentState="false" 中添加到你的 Button 中。


我已经做了 ;). 顺便说一下,这是一个ImageButton。 - RobGThai
5
尝试了,但没有任何区别。 - RobGThai

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接