How to prevent Button inside ListItem getting highlight

萝らか妹 提交于 2019-12-18 04:16:12

问题


So I have custom list item with buttons for a ListView. When pressed, the button display alternate drawable to show feedback to user. However when I click on the row, every buttons show pressed state as if I have clicked on them.

How do I keep the button displays its original state instead of state_pressed?

layout/List Item:

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

I have tried to remove state_focused and state_pressed, state_focused. It seems that the button take state_pressed from its parent.


回答1:


I found out that setting android:clickable="true" to the parent view will prevent child views state to be changed.

See this answer.




回答2:


This makes my app work harder, but it solves the problem:

...
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()
    });



回答3:


I have this problem too, I Google it and try it about 3 hours! now I found the solution. just add OnClickListener to child view. and even DO NOTHING in OnClick method. It works on 2.3 emulator and my nexus 5.




回答4:


im my opinion you need to disable the state from the parent as android:duplicateParentState="false"

add this to your Button



来源:https://stackoverflow.com/questions/12280863/how-to-prevent-button-inside-listitem-getting-highlight

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!