2012-09-05 10 views
6

Tengo un elemento de lista personalizado con botones para un ListView. Cuando se presiona, el botón muestra alternas dibujables para mostrar comentarios al usuario. Sin embargo, cuando hago clic en la fila, todos los botones muestran el estado presionado como si hubiera hecho clic en ellos.Cómo evitar el botón dentro de ListItem para resaltar

¿Cómo puedo mantener el botón muestra su estado original en lugar de state_pressed?

diseño/Lista de elementos:

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

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

he intentado quitar state_focused y state_pressed, state_focused. Parece que el botón toma state_pressed de su padre.

+0

¿Alguna suerte para encontrar una solución? – Sunkas

Respuesta

0

im mi opinión es necesario deshabilitar el estado de los padres como android:duplicateParentState="false"

agregar esto a su Button

+0

Lo que hice;). Es ImageButton por cierto. – RobGThai

+0

también en sus diseños –

+5

Probé, sin embargo, no hay diferencia. – RobGThai

1

Esto hace que mi trabajo aplicación más difícil, pero que resuelve el problema:

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

podría explicar esta solución? –

4

Descubrí que establecer android:clickable="true" en la vista principal impedirá que se modifique el estado de vistas secundarias.

Ver this answer.

1

¡También tengo este problema, lo busco en Google y lo intento alrededor de 3 horas! ahora encontré la solución. simplemente agregue OnClickListener a la vista infantil. e incluso NO HACER NADA en el método OnClick. Funciona en 2.3 emulador y mi nexo 5.

Cuestiones relacionadas