Android ListView小部件有时对点击没有响应



我实现了一个小部件,其中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 4.0的索尼设备上,如果你滑到另一个主窗格,然后再回到小部件所在的那个,那么小部件列表元素将无法点击。索尼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

然而,我无法解决这个问题,这使我相信这是一个主屏幕的问题。为了证实这一点,我决定在索尼Android 4.0设备上安装"Go Launcher",一切正常!

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

最新更新