5

Ich habe eine ListView mit benutzerdefinierten Liste Adapter. Es hat OnItemClickListener und OnItemLongClickListner, die früher gut funktionierten. Danach musste ich eine Schaltfläche in das Layout des Listenelements einfügen und der Elementklick und der Longclick-Listener funktionierten nicht mehr. Hier ist mein Beispielcode:ListView lange klicken nicht funktioniert, wenn eine Schaltfläche in der Liste ist

ListView lv=(ListView)findViewbyId(R.id.listview); 
lv.setAdapter(listviewadapter); 
lv.setOnItemLongClickListener(new OnItemLongClickListener() { 
      @Override 
      public boolean onItemLongClick(AdapterView<?> parent, View view,int position, long id) { 
      // My code 
      } 
}); 

Diese verwendet zu funktionieren, bevor die Schaltfläche in dem Layout Listeneintrag hinzufügen:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:padding="2dp" 
    > 
    <TextView 
     android:id="@+id/symbol_name" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="2.5" 
     android:layout_gravity="left" 
     /> 
    <TextView 
     android:id="@+id/ltp" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right" 
     android:layout_weight="1" 
     /> 
    <TextView 
     android:id="@+id/change_in_perc" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right" 
     android:layout_weight="1" 
     /> 

    <TextView 
     android:id="@+id/volume" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right" 
     android:layout_weight="1" 
     /> 

    <ImageButton 
     android:id="@+id/chart" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right" 
     android:layout_weight="0.5" 
     android:src="@drawable/charts" 
     android:contentDescription="Chart Link" 
     /> 
</LinearLayout> 

I Inline-Hörers versuchte Wechsel zur Umsetzung onItemLongClickListener auf Aktivität, aber kein Erfolg bis jetzt. Vielen Dank.

+0

Dies könnte der Fall sein:

dieses

android:descendantFocusability="blocksDescendants" 

auf Ihre Wurzel

http://developer.android.com/reference/android/view/ViewGroup.html#attr_android:descendantFocusability

android So Element hinzufügen. Ich versuche es. Aber wenn das den Fokus nehmen würde, sollte nicht das Klickereignis der Schaltfläche ausgelöst werden, wenn ich auf die Listenansicht klicke? – Sourabh

+2

Das funktioniert .. Bitte fügen Sie dies als Ihre Antwort, so dass ich es akzeptieren kann. – Sourabh

Antwort

13

Ihre Bildschaltfläche nimmt wahrscheinlich den Fokus, wenn Sie auf Listenelement klicken. descendantFocusability

Defines the relationship between the ViewGroup and its descendants when looking for a View to take focus. 

Must be one of the following constant values. 

Constant Value Description 
beforeDescendants 0 The ViewGroup will get focus before any of its descendants. 
afterDescendants 1 The ViewGroup will get focus only if none of its descendants want it. 
blocksDescendants 2 The ViewGroup will block its descendants from receiving focus. 
This corresponds to the global attribute resource symbol descendantFocusability. 
+1

Vielen Dank. Diese Antwort wird akzeptiert, sobald der Stackoverflow dies zulässt. :) – Sourabh

+0

Bitte helfen Sie mir. Ich bin jetzt seit 7 Stunden dran und es ist so eine einfache Frage. http://stackoverflow.com/questions/35108940/why-cant-i-remove-an-item/35109304#35109304 –

0

haben Sie die android: longClickable = "true" in der XML-Datei und die listview.setLongClickable (true); im Code?

+0

Bitte helfen Sie mir. Ich bin jetzt seit 7 Stunden dran und es ist so eine einfache Frage. http://stackoverflow.com/questions/35108940/why-cant-i-remove-an-item/35109304#35109304 –

Verwandte Themen