试图赶上Android的ListView项点击:安卓descendantFocusability =" blocksDescendants"不工作 [英] Trying to catch a click on android ListView item: android:descendantFocusability="blocksDescendants" not working

查看:185
本文介绍了试图赶上Android的ListView项点击:安卓descendantFocusability =" blocksDescendants"不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这已得到解决在计算器上的很多帖子。最推荐的解决方案是设置:

This has been addressed in many posts on StackOverflow. The most recommended solution is to set:

android:descendantFocusability="blocksDescendants"

上的列表项目布局的根布局。我已经做了以下几点。

on the root layout of the list item layout. I've done the following.

  • 设置机器人:descendantFocusability =根blocksDescendants 列表项的布局。
  • 在ListView控件调用setDescendantFocusability(块),以及 每个ListViewItem的。
  • 设置重点=在ImageButtons禁用
  • Set android:descendantFocusability="blocksDescendants" on the root list item layout.
  • Called setDescendantFocusability(BLOCK) on the listView, as well as each listViewItem.
  • Set focus=disabled on the ImageButtons

但是点击子ImageButtons之一的时候,我不能让上的ListView或ListView项目的任何听众开火。

However when one of the child ImageButtons is clicked, I can't get any listeners on the ListView or ListView Items to fire.

下面是模板我使用的渲染列表项。

Below is the "template" I am using to render a list item.

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

我也设置这个descendantFocusability在code,只是可以肯定的:

Am also setting this descendantFocusability in the code, just to be sure:

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

和对我的ImageButton的造型我给自己定可成为焦点=假,只是为了好措施:

And on the styling of my ImageButton's I've set focusable=false, just for good measure:

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

我收到的onClick的任何部分列表项的点击,包括孩子的TextView元素时收到的。但是在任何ImageButtons的点击时,点击的没有得到回升。 (顺便说一句我有与ImageButtons登记OnClickListeners和之后就被调用。)

I am getting onClick's received when clicking on any part of the list item, including the child TextView elements. However when clicking on any of the ImageButtons, the click's don't get picked up. (BTW I have got OnClickListeners registered with the ImageButtons and they are getting called.)

设了:

  • 在setOnItemClickListener上的ListView
  • 在setOnItemSelectedListener上的ListView
  • 在setOnClickListener每个ListView项。

但这些被调用如果单击ImageButtons之一。

But none of these get called if one of the ImageButtons is clicked.

任何帮助将是AP preciated。

Any help would be appreciated.

推荐答案

尝试使用这个类的按钮布局在

Try using this inside each button layouts

 android:focusable="false"
android:focusableInTouchMode="false"

和删除哪些你正在做这个其他东西。 另外从列表视图。 同时删除此

and remove other thing which you are doing for this. Also from listview. Also remove this

android:descendantFocusability="blocksDescendants"

这也太

parent.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
        ((ViewGroup) v).setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);

这篇关于试图赶上Android的ListView项点击:安卓descendantFocusability =&QUOT; blocksDescendants&QUOT;不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆