1

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

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

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

其他一些测试表明:

  • 如果我在有问题的列表中滚动一段时间,然后尝试再次单击,它可以工作!我的第一个想法是原因是调用了 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;
}

我已经有这个问题很长时间了,非常感谢任何帮助

4

1 回答 1

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,很可能无法解决。请告诉我我错了。

于 2014-04-14T22:39:11.847 回答