2012-09-05 41 views
6

所以我有一个ListView的按钮的自定义列表项。当按下时,按钮显示可替代绘制以向用户显示反馈。然而,当我点击该行时,每个按钮都显示按下状态,就好像我点击了它们一样。如何防止ListItem中的按钮变得突出显示

如何让按钮显示其原始状态而不是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> 

绘制/ 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_focused。看起来这个按钮从它的父母身上取得了state_pressed。

+0

任何运气找到解决方案? – Sunkas

回答

0

IM我看来,你需要从父禁用状态android:duplicateParentState="false"

添加到您的Button

+0

我做了什么;)。顺便说它是ImageButton。 – RobGThai

+0

也在你的布局 –

+5

试过了,虽然没有区别。 – RobGThai

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

你能解释一下这个解决方案吗? –

4

我发现设置android:clickable="true"到父视图将防止子视图状态改变。

请参阅this answer

1

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