2016-07-26 143 views
1

我正在开发具有ListView的应用程序。在此ListView中,当我触摸一个项目时,此项目的背景颜色会发生变化,以显示该项目已被选中。Android ListView取消选择项目

Howerver,布局有一个EditText太:

<ListView 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:id="@+id/pratosListView" 
      android:layout_marginBottom="@dimen/activity_vertical_margin" 
      android:layout_weight="0.8"/> 

    <EditText 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:inputType="number" 
      android:id="@+id/quantidadeEditText" 
      android:layout_marginRight="@dimen/activity_horizontal_margin" 
       style="@style/EditText"/> 

,当我接触到的EditText ListView中选择的项目是未选择的,换句话说,背景色回原来的颜色。我不希望发生这种情况。

这是我的选择(list_item_selector.xml):

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_selected="true" android:drawable="@drawable/item_list_selected"/> 
    <item android:state_pressed="true" android:drawable="@drawable/item_list_pressed"/> 
    <item android:state_focused="true" android:drawable="@drawable/item_list_focused"/> 
    <item android:drawable="@drawable/item_list_normal"/> 
</selector> 

这里我设置的项目(item_lista_pedidos.xml)背景:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:padding="@dimen/activity_all_magim" 
    android:orientation="horizontal" 
    android:weightSum="5" 
    android:baselineAligned="false" 
    android:background="@drawable/list_item_selector"> 
..... 
</LinearLayout> 

这里是点击执行聆听者:

@Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     // salva a posicao do item selecionado 
     lastPositionSelected = position; 
     // salva o objeto relativo ao item selecionado 
     servicoSelected = servicos.get(position); 
     view.setSelected(true); 
    } 

有人有一些想法,以避免在我碰触项目时被取消选择在EditText中?

+0

请考虑接受答案作为正确答案,如果它帮助你 – SoroushA

+1

嗨,谢谢你的回复!但没有帮助。我用另一种方式解决了这个问题我在list_item_selector.xml中将属性“state_focused”更改为“state_activated”,并删除了“view.setSelected(true);”这一行。来自onItemClick函数。但是,无论如何,谢谢。 –

回答

1

如前所述here,如果你想检测外触摸只能在外面的EditText的,你可以在包含视图检测触摸事件,然后,给你有你的EditText视图:

Rect editTextRect = new Rect(); 
myEditText.getHitRect(editTextRect); 

if (!editTextRect.contains((int)event.getX(), (int)event.getY())) { 
    //touch was outside edittext 
} 

或者你加一个触摸监听器的EditText和容器,并在一个EditText中返回false,这样它将被拦截并不会转发给父级。因此,您在父级侦听器中检测到的所有触摸都不属于EditText。

+0

嗨,谢谢你的回复!但没有帮助。我用另一种方式解决了这个问题我在list_item_selector.xml中将属性“state_focused”更改为“state_activated”,并删除了“view.setSelected(true);”这一行。来自onItemClick函数。但是,无论如何,谢谢。 –