【问题标题】:Android ListView widgets are sometimes unresponsive to clicksAndroid ListView 小部件有时对点击无响应
【发布时间】:2014-04-24 22:18:07
【问题描述】:

我已经实现了一个带有RemoteViewsService 的小部件,其中包含一个ListView 的项目,其中所有项目都可以点击以启动服务。简而言之,我在AppWidgetProvider.onUpdate() 中使用setPendingIntentTemplate,在RemoteViewsService.RemoteViewsFactory() 中使用setOnClickFillInIntent

在大多数情况下,我得到了预期的行为。但是,当用完一些内存并返回尝试再次单击列表中的项目时有时单击列表中的项目时没有任何反应:服务未启动,并且没有提供触摸反馈。如果我有多个小部件,其中一个列表可能有问题,而其他列表则没有。

有人知道如何解决这个问题吗?

其他一些测试表明:

  • 如果我在有问题的列表中滚动一段时间然后尝试再次单击,它会起作用!我的第一个想法是原因是 RemoteViewsFactory.getView 被调用并因此更新了待处理的意图,但是查看日志我可以看到没有输入这个方法。
  • 我还登录了RemoteViewsService.RemoteViewsFactory.onDestroy,看看问题的原因是否是它已被删除,但事实并非如此。
  • 如果我设法调用 AppWidgetManager.updateAppWidget(以便 AppWidgetProvider.onUpdate 将运行)问题消失(我从我的主应用程序中调用 updateAppWidget)。

这三个观察结果似乎表明问题出在 AppWidgetManager 而不是 RemoteViewsFactory 中。

AppWidgetProvider.onUpdate:

@Override
public void onUpdate(Context iContext, AppWidgetManager iWidgetMgr,
        int[] iWidgetIds){
    Log.d(DbgU.getAppTag(), DbgU.getMethodName());

    //Going through all widgets placed (could be more than one)
    for(int i = 0; i < iWidgetIds.length; i++){
        //Setting up the remote view service
        Intent tmpRVServiceIntent = new Intent(iContext,
                RemoteViewsServiceC.class);
        tmpRVServiceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                iWidgetIds[i]);
        tmpRVServiceIntent.setData(Uri.parse(tmpRVServiceIntent.toUri(
                Intent.URI_INTENT_SCHEME)));

        //Setting up the remote views
        RemoteViews tmpRemoteViews = new RemoteViews(iContext.getPackageName(),
                R.layout.widget);
        tmpRemoteViews.setRemoteAdapter(R.id.widget_listview,
                tmpRVServiceIntent);
        tmpRemoteViews.setEmptyView(R.id.widget_listview,
                R.id.widget_empty_view);

        //Setting up the pending intent template (the id will be filled
        //in later in RemoteViewsFactoryC.getViewAt())
        Intent tmpTemplateIntent = new Intent(iContext,
                LauncherServiceC.class);

        tmpTemplateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                iWidgetIds[i]);
        PendingIntent tmpPendingIntent = PendingIntent.getService(iContext,
                0, tmpTemplateIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        tmpRemoteViews.setPendingIntentTemplate(R.id.widget_listview,
                tmpPendingIntent);

        //Applying the update for the views
        iWidgetMgr.updateAppWidget(iWidgetIds[i], tmpRemoteViews);
    }
}

RemoteViewsService.RemoteViewsFactory.getViewAt:

@Override
public RemoteViews getViewAt(int inPosition) {
    Log.v(DbgU.getAppTag(), DbgU.getMethodName()
            + ", inPosition = " + inPosition);

    //Moving the cursor to the current position
    mItemCursor.moveToPosition(inPosition);

    //Extracting values from the database
    String tmpName = mItemCursor.getString(
            mItemCursor.getColumnIndexOrThrow(ItemTableM.COLUMN_NAME));
    long tmpItemId = mItemCursor.getLong(
            mItemCursor.getColumnIndexOrThrow(ItemTableM.COLUMN_ID));
    Uri tmpItemUri = DatabaseU.getItemUriFromId(tmpItemId);

    //Setting up the remote views object
    RemoteViews retRemoteViews = new RemoteViews(
            mContext.getPackageName(), R.layout.widget_listitem);
    retRemoteViews.setTextViewText(R.id.widget_listitem_textView, tmpName);

    //Adding action URI to the intent template which was set for all the
    //list rows in WidgetProviderC.onUpdate
    Intent tmpFillInIntent = new Intent();
    tmpFillInIntent.setData(tmpItemUri);
    retRemoteViews.setOnClickFillInIntent(R.id.widget_listitem_textView,
            tmpFillInIntent);

    return retRemoteViews;
}

这个问题困扰我很久了,非常感谢大家的帮助

【问题讨论】:

    标签: android android-widget android-pendingintent remoteview android-remoteview


    【解决方案1】:

    我也有同样的问题很久了。

    在搭载 Android 4.0 的 Sony 设备上,如果您滑动到另一个主窗格然后返回到小部件所在的那个,则小部件列表元素将无法点击。 Sony Android 4.1 及以上版本没有此问题,但有时会显示错误的列表元素。 在一些装有 Android 4.1 的三星设备上,我也看到了同样的问题。 Android 4.0模拟器主屏没有这个问题。

    我尝试了很多方法来修复它,也是这样:stackoverflow.com/questions/21891008/a-textview-in-a-listview-s-row-cannot-click-after-setting-descendantfocusabilit

    但是,我无法解决导致我认为这是主屏幕问题的问题。 为了确认这一点,我决定在 Sony Android 4.0 设备上安装“Go Launcher”,一切正常!

    答案:这是一个主屏幕问题,如果不使用某种 hack,很可能无法解决。请告诉我我错了。

    【讨论】:

    • 感谢您提供此信息,我有一台带 ICS 的 Sony Xperia,所以原因一定是您所说的主屏幕问题
    猜你喜欢
    • 2018-02-10
    • 2015-11-24
    • 1970-01-01
    • 2019-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多