2013-04-17 3 views
0

Прошу прощения за мой английский, я французский!Android listview onItemClickListener не работает

У меня есть список, и я хочу отобразить другое действие при щелчке по элементу. Но onItemClickListener, похоже, не работает ... Я искал в Google, но ничего не нашел.

Вот мой код:

XML-ListView (под заказ):

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/activity_advert_list_listview" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:background="@color/background" > 

<RelativeLayout 
    android:id="@+id/advertBackground" 
    android:layout_width="339dp" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_marginBottom="7.5dp" 
    android:layout_marginTop="7.5dp" 
    android:background="@drawable/background_advert_list" > 

    <RelativeLayout 
     android:id="@+id/advertImageLayout" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="8dp" > 

     <RelativeLayout 
      android:id="@+id/advertBackgroundLayout" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@color/grey" > 

      <ImageView 
       android:id="@+id/advertImage" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" /> 
     </RelativeLayout> 

     <RelativeLayout 
      android:id="@+id/advertPriceLayout" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/advertImage" 
      android:layout_marginTop="-30.5dp" 
      android:background="@color/white_50" > 

      <TextView 
       android:id="@+id/advertPriceText" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerHorizontal="true" 
       android:hint="@string/Price" 
       android:paddingBottom="5dp" 
       android:paddingLeft="7dp" 
       android:paddingRight="7dp" 
       android:paddingTop="3dp" 
       android:textSize="14dp" 
       android:textStyle="bold" /> 
     </RelativeLayout> 
    </RelativeLayout> 

    <RelativeLayout 
     android:id="@+id/advertInfosLayout" 
     android:layout_width="305dp" 
     android:layout_height="47dp" 
     android:layout_below="@+id/advertImageLayout" 
     android:layout_centerHorizontal="true" 
     android:layout_marginBottom="10dp" 
     android:layout_marginTop="10dp" > 

     <RelativeLayout 
      android:id="@+id/advertInfosLayoutLayout" 
      android:layout_width="225dp" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/advertImageLayout" > 

      <TextView 
       android:id="@+id/advertTitleText" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:hint="@string/Product_name" 
       android:textColor="@color/grey_blue" 
       android:textSize="15sp" 
       android:textStyle="bold" /> 

      <TextView 
       android:id="@+id/advertSizeText" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/advertTitleText" 
       android:hint="@string/Product_infos" 
       android:textColor="@color/b4b5b6" 
       android:textSize="15sp" /> 
     </RelativeLayout> 

     <RelativeLayout 
      android:id="@+id/advertBookmarkLayout" 
      android:layout_width="74dp" 
      android:layout_height="45dp" 
      android:layout_toRightOf="@+id/advertInfosLayoutLayout" > 

      <Button 
       android:id="@+id/advertBookmarkIcon" 
       android:layout_width="26dp" 
       android:layout_height="26dp" 
       android:layout_centerVertical="true" 
       android:background="@drawable/bookmark" /> 
     </RelativeLayout> 

     <RelativeLayout 
      android:id="@+id/advertDistanceLayout" 
      android:layout_width="50dp" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" > 

      <Button 
       android:id="@+id/advertDistanceIcon" 
       android:layout_width="26dp" 
       android:layout_height="26dp" 
       android:layout_centerHorizontal="true" 
       android:background="@drawable/distance" /> 

      <TextView 
       android:id="@+id/advertDistanceText" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/advertDistanceIcon" 
       android:layout_centerHorizontal="true" 
       android:text="13 km" 
       android:textColor="@color/grey_midark" 
       android:textSize="11sp" /> 
     </RelativeLayout> 
    </RelativeLayout> 
</RelativeLayout> 

код Java:

list.setOnItemClickListener(new OnItemClickListener() { 

    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     // Start new detail activity 
     Intent intent = new Intent(activity, AdvertDetailActivity.class); 
     startActivity(intent); 

     Log.w("AdvertList", "Item clicked"); 
    } 
}); 

У вас есть идея ? Спасибо большое!

+1

Не работает в этом смысле ..? Что случилось..? А что такое деятельность? – Pragnani

+0

Попробуйте добавить 'android: clickable =" true "' в XML. – Cornholio

+0

PLS сообщение больше код. – sujith

ответ

4

Если вы хотите добавить кнопки/флажки в ListView строки, вы должны добавить это к ним в XML:

android:focusable="false" 

Почему? Поскольку у вас есть 2 зоны кликов, кнопка и сама строка, поэтому вам нужно поставить свою строку в качестве основной сенсорной зоны.

Попробуйте это, чтобы узнать, работает ли оно.

С другой стороны, у вас слишком много ViewGroups (RelativeLayouts) в вашем xml, что сделает процесс рисования медленнее. Достаточно иметь только один (и, возможно, некоторые LinearLayouts, если они вам действительно нужны).

+0

Нет, это не сработает ... И для вашего другого решения я не могу этого сделать ...:/ –

+0

Я использую @Shyish, это много viewGroups действительно ужасно для устройства для рендеринга, и если вы более чем на 50 строк, ваше приложение может потерпеть крах из-за нехватки памяти. Попробуйте использовать ViewHierarchy Tool, чтобы уменьшить его. – Ankit

Смежные вопросы