android - 使用 setOnClickFillInIntent 和 setPendingIntentTemplate 时,不会发生点击事件

标签 android android-layout android-listview

我正在开发一个 Android 主屏幕应用小部件,它使用自定义行布局填充 ListView 。 ListView 已正确填充,但当我单击任何项​​目时,没有任何反应。

这里是一些相关的部分

来自 AppWidgetManager 的 onUpdate()

...
final Intent contactIntent = new Intent(context, ContactService.class);
contactIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
contactIntent.setData(Uri.parse(contactIntent.toUri(Intent.URI_INTENT_SCHEME)));
view.setRemoteAdapter(widgetId, R.id.listview, contactIntent);
//works fine up to here, populates ListView

//Set up pendingIntentTemplate
final Intent contactClick = new Intent(context,ContactDial.class);
contactClick.setData(Uri.parse(contactClick.toUri(Intent.URI_INTENT_SCHEME)));
final PendingIntent contactClickPI = PendingIntent.getBroadcast(context,
     0,contactClick,PendingIntent.FLAG_UPDATE_CURRENT);
view.setPendingIntentTemplate(R.id.listview, contactClickPI);
Log.d("TEMPLATE", "set template");
...

来自 RemoteViewsFactory 的 getViewAt(int position)

final Intent i = new Intent();
final Bundle extra = new Bundle();
extra.putString("number", phoneNumber);

i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtras(extra);
row.setOnClickFillInIntent(R.id.contact_row,i);
Log.d("FACTORY", "Set Intent Template");

日志显示代码已正确执行,但出于某种原因它似乎没有记录我的点击。我在应该将 Intent 发送到的类的 onCreate 中放置了一个 Log.d,但它从未发生过。 以下是我的布局的重要部分。

主要布局:

<LinearLayout
        android:id="@+id/layout_two"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:descendantFocusability="blocksDescendants" >


        <ListView
            android:id="@+id/listview"
            android:layout_width="match_parent"
            android:layout_height="260dp" />

        <!-- Some other views -->

    </LinearLayout>

自定义行:

<LinearLayout android:id="@+id/contact_row"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:background="@xml/buttoncolor_selector"
    android:paddingBottom="2dp"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:paddingLeft="10dip" >

    <ImageView
        android:id="@+id/contact_photo"
        android:layout_width="@android:dimen/notification_large_icon_width"
        android:layout_height="match_parent"
        android:layout_marginRight="5dip"
        android:src="@drawable/ic_action_person   />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"  >
        <TextView
            android:id="@+id/contact_name"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:textSize="18sp"
            android:gravity="center_vertical"
            android:singleLine="true"
            android:ellipsize="marquee"
            android:textIsSelectable="false"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:clickable="false"/>
        <TextView
            android:id="@+id/contact_number"
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:singleLine="true"
            android:ellipsize="marquee"
            android:textIsSelectable="false"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:clickable="false"  />
    </LinearLayout>

</LinearLayout>

我几乎查看了在 Internet 上可以找到的每个示例,但没有一个解决方案有任何改变。 请帮忙。

最佳答案

在您的自定义行中,尝试将 android:id="@+id/contact_row" 向下移动到一个 subview (即使它是一个“无用”的 View 组,只是围绕着其他所有内容)。或者,用 FrameLayout 围绕您的顶级 LinearLayout。我没有运气将 setOnClickFillInIntent() 设置为像那样在最顶层的 View 组上定位 ID。

像这样,

<FrameLayout  xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

    <LinearLayout android:id="@+id/contact_row"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:background="@xml/buttoncolor_selector"
    android:paddingBottom="2dp"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:paddingLeft="10dip" >

    <ImageView
        android:id="@+id/contact_photo"
        android:layout_width="@android:dimen/notification_large_icon_width"
        android:layout_height="match_parent"
        android:layout_marginRight="5dip"
        android:src="@drawable/ic_action_person"   />
........

根据您的需要,如果您重新安排您的布局,您也许可以吃蛋糕并拥有它,并且拥有一个无用的 View 组。

关于android - 使用 setOnClickFillInIntent 和 setPendingIntentTemplate 时,不会发生点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24944022/

相关文章:

Android Listview 包含布局点击列表项

android - ListActivity的onListItemClick不起作用

android - 自动测试需要用户认证的Android功能

java - 如何在 android.support.design.chip.Chip 中设置文本颜色?

android - 使用 LinearLayouts 将高度一分为三

android - Android 布局有什么方法可以知道它的 child 什么时候在画画吗?

android - 在Google Play商店中上传具有不同签名和相同包名的apk

android - 用多行填充微调项

android - 包括布局不工作另一个布局

Android:如果从数据库中删除超过 1 行,则具有多项选择 ListView 的应用程序会中断