Dies wurde in vielen Posts auf StackOverflow angesprochen. Die empfohlene Lösung ist:Versuchen, einen Klick auf Android-ListView-Element zu fangen: Android: demcendantFocusability = "blocksDescendants" funktioniert nicht
android:descendantFocusability="blocksDescendants"
im Root-Layout des Listenlayouts. Ich habe Folgendes getan.
- Set
android:descendantFocusability="blocksDescendants"
auf der Wurzel Listenelement-Layout. - Genannt
setDescendantFocusability(BLOCK)
auf der ListView, sowie jedes listViewItem. - Set
focus=disabled
auf den ImageButtons
jedoch, wenn einer der Kind ImageButtons geklickt wird, kann ich nicht alle Zuhörer auf den ListView
oder ListView
Artikel zu feuern.
Unten ist die "Vorlage", die ich verwende, um ein Listenelement zu rendern.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/reminderItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants"
android:padding="0dp">
<RelativeLayout
android:id="@+id/topPartOfReminder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<LinearLayout
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/textLayout">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:fontFamily="sans-serif-light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22sp"
android:text="10:00"
android:textStyle="normal"
android:id="@+id/textTime"/>
<TextView
android:fontFamily="sans-serif-condensed"
android:textSize="9sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AM"
android:paddingLeft="2dp"
android:textStyle="normal"
android:id="@+id/textAmPm"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text=" - "
android:id="@+id/textDivider"/>
<TextView
android:fontFamily="sans-serif-light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="March 26"
android:id="@+id/textDate"/>
</LinearLayout>
<TextView
android:fontFamily="sans-serif-light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text="Blah blah blah blah"
android:id="@+id/textDescription"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/textLayout"
android:gravity="right">
<ImageButton
style="@style/HorizontalButton"
android:id="@+id/shiftButton"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/ic_action_forward"
android:layout_marginLeft="10dp"/>
<ImageButton
style="@style/HorizontalButton"
android:id="@+id/repeatButton"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/ic_action_refresh"
android:layout_marginLeft="10dp"/>
<ImageButton
style="@style/HorizontalButton"
android:id="@+id/tickButton"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/ic_action_done"
android:layout_marginLeft="10dp"/>
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:id="@+id/tickButtonsView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:padding="0dp"
android:orientation="horizontal"
android:visibility="gone">
<Button style="@style/HorizontalButton" android:text="OK"/>
<View style="@style/VerticalDivider"/>
<Button style="@style/HorizontalButton" android:text="Cancel"/>
</LinearLayout>
<LinearLayout
android:id="@+id/repeatButtonsView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:padding="0dp"
android:orientation="horizontal"
android:visibility="gone">
<Button style="@style/HorizontalButton" android:text="day"/>
<View style="@style/VerticalDivider"/>
<Button style="@style/HorizontalButton" android:text="weekday"/>
<View style="@style/VerticalDivider"/>
<Button style="@style/HorizontalButton" android:text="week"/>
<View style="@style/VerticalDivider"/>
<Button style="@style/HorizontalButton" android:text="month"/>
</LinearLayout>
<LinearLayout
android:id="@+id/shiftButtonsView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:padding="0dp"
android:orientation="horizontal"
android:visibility="gone">
<Button style="@style/HorizontalButton" android:text="1 week"/>
<View style="@style/VerticalDivider"/>
<Button style="@style/HorizontalButton" android:text="1 day"/>
<View style="@style/VerticalDivider"/>
<Button style="@style/HorizontalButton" android:text="1 hour"/>
<View style="@style/VerticalDivider"/>
<Button style="@style/HorizontalButton" android:text="10 mins"/>
</LinearLayout>
</LinearLayout>
Am auch diese descendantFocusability in der Code-Einstellung, nur um sicher zu sein:
public class ReminderItemAdapter extends CursorAdapter {
. . .
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = mLayoutInflater.inflate(R.layout.reminder_item, parent, false);
parent.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
((ViewGroup) v).setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
return v;
}
Und auf dem Styling meiner Image die habe ich fokussierbarem = false, nur für eine gute Maßnahme gesetzt:
<resources>
<style name="HorizontalButton">
<item name="android:layout_width">0dip</item>
<item name="android:layout_weight">1</item>
<item name="android:layout_height">match_parent</item>
<item name="android:fontFamily">sans-serif</item>
<item name="android:textSize">12sp</item>
<item name="android:background">?android:attr/selectableItemBackground</item>
<item name="android:focusable">false</item>
<item name="android:focusableInTouchMode">false</item>
</style>
. . .
Ich bekomme onClick erhalten, wenn Sie auf einen Teil des Listenelements klicken, einschließlich der untergeordneten TextView-Elemente. Wenn Sie jedoch auf eines der ImageButtons klicken, werden die Klicks nicht aufgenommen. (BTW habe ich OnClickListeners mit den ImageButtons registriert bekam, und sie werden immer genannt.)
gesetzt haben:
- setOnItemClickListener auf Listview
- setOnItemSelectedListener auf Listview
- setOnClickListener auf jedem Listview-Element.
Aber keiner von diesen wird aufgerufen, wenn einer der ImageButtons geklickt wird.
Jede Hilfe wäre willkommen.
Wenn Sie "Schaltflächenlayouts" sagen, meinen Sie die tatsächlichen ImageButton-Elemente oder die Layouts, die diese Schaltflächen enthalten? Prost. – Ben
jedes Bild Tasten – keshav
Hat genau wie Sie beschrieben. Das Verhalten bleibt gleich. Der Listener für den eigentlichen ImageButton wird aufgerufen. Aber keiner der Listener für die ListView- oder ListView-Elemente wird aufgerufen. – Ben