Android小部件listview句柄点击

Posted

技术标签:

【中文标题】Android小部件listview句柄点击【英文标题】:Android widget listview handle click 【发布时间】:2018-02-10 16:59:37 【问题描述】:

我已经看到有关此问题的相关问题,但我无法弄清楚为什么这对我不起作用。

我希望在单击列表项时调用 Widget Provider 类中的 onReceive() 方法,但没有任何反应。

包括一些 cmets,它们也显示了我一直在尝试的内容。

相关代码:

主应用布局的 ListView 定义如下:

<ListView
        android:id="@+id/events_list"
        style="@android:style/TextAppearance.Small"
        android:layout_
        android:layout_ />  

Row 是使用具有两个嵌套线性布局的 RelativeLayout 定义的,不确定这是否重要。

getViewAt() 是通过视图工厂实现的,函数如下:

@Override
    public RemoteViews getViewAt(int position) 

        if (events_ack == null) 
            Log.e("LIST", "getViewAt (null) events_ack");
            return null;
        

        RemoteViews row = new RemoteViews(context.getPackageName(), R.layout.row);

        row.setTextViewText(R.id.event_name, events_ack[position].name);
        row.setTextViewText(R.id.description, events_ack[position].description);
        row.setTextViewText(R.id.publicTV, (events_ack[position].is_public == 0) ? "Public: False" : "Public: True");
        row.setTextViewText(R.id.timeTV, events_ack[position].time);
        row.setViewVisibility(R.id.price, View.GONE);

        // download and set image BONUS QUESTION- does not work through AsyncTask using future insted (images are downloaded but not shown).

        row.setImageViewBitmap(R.id.image, pic);

        Bundle extras = new Bundle();
        extras.putInt(CasusWidgetProvider.EXTRA_WORD, events_ack[position].id);
        Intent fillInIntent = new Intent();
        fillInIntent.putExtras(extras);
        // Make it possible to distinguish the individual on-click
        // action of a given item
        row.setOnClickFillInIntent(R.id.events_list, fillInIntent); 

        //extras.putInt(CasusWidgetProvider.EXTRA_WORD, events_ack[position].id);
        //i.putExtras(extras);
        //i.setAction(CasusWidgetProvider.ITEM_CLICK);
        //row.setOnClickFillInIntent(R.layout.casus_widget, i); //event_name?

        // ??

        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.casus_widget);
        appWidgetManager.updateAppWidget(appWidgetId, rv);
        //appWidgetManager.updateAppWidget(appWidgetId, row);

        return row;
    

Widget Provider 类有 onUpdate() 方法:

@Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) 

        Log.w("CASUS", "UPDATE function called");


        for (int i = 0; i < appWidgetIds.length; i++) 

            Intent intent = new Intent(context, WidgetService.class);
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
            intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
            RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.casus_widget);
            rv.setRemoteAdapter(appWidgetIds[i], R.id.events_list, intent);

            Intent clickIntent=new Intent(context, WidgetService.class);
            clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
            clickIntent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
            clickIntent.setAction(ITEM_CLICK);

            PendingIntent clickPI = PendingIntent.getActivity(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            rv.setPendingIntentTemplate(R.id.events_list, clickPI);
            appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds[i],  R.id.events_list); //?

            clickIntent = new Intent(context, CasusWidgetProvider.class);
            clickIntent.setAction(UPDATE_LIST);
            clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);


            PendingIntent pendingIntentRefresh = PendingIntent.getBroadcast(context, 0, clickIntent, 0);
            rv.setOnClickPendingIntent(R.id.update, pendingIntentRefresh);

            appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
        

        fetchData(context, appWidgetIds);

        super.onUpdate(context, appWidgetManager, appWidgetIds);

    

【问题讨论】:

【参考方案1】:

我想通了。小部件提供程序类中的 onUpdate() 函数已修改为如下所示:

@Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) 
        for (int i = 0; i < appWidgetIds.length; i++) 
            Intent intent = new Intent(context, WidgetService.class);
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
            intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
            RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.casus_widget);
            rv.setRemoteAdapter(appWidgetIds[i], R.id.events_list, intent);

            appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
        

        fetchData(context, appWidgetIds);

        super.onUpdate(context, appWidgetManager, appWidgetIds);

    

在小部件工厂的 getViewAt() 函数中,这一行:

row.setOnClickFillInIntent(R.id.events_list, fillInIntent);  

已修改以匹配正确的 ID:

row.setOnClickFillInIntent(R.id.row_layout, fillInIntent);  

点击处理程序模板设置在执行 HTTP 请求的类中,基本上:

for (int widgetId : allWidgetIds) 
            RemoteViews remoteViews = new RemoteViews(this.getApplicationContext().getPackageName(), R.layout.casus_widget);


            Intent svcIntent = new Intent(this.getApplicationContext(), WidgetService.class);
            svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
            svcIntent.putExtra("events", events);
            svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
            remoteViews.setRemoteAdapter(widgetId, R.id.events_list, svcIntent);


            Intent clickIntent = new Intent(this.getApplicationContext(), CasusWidgetProvider.class);
            clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, allWidgetIds);
            clickIntent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
            clickIntent.setAction(CasusWidgetProvider.ITEM_CLICK);


            PendingIntent toastPendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            remoteViews.setPendingIntentTemplate(R.id.events_list, toastPendingIntent);

            appWidgetManager.updateAppWidget(widgetId, remoteViews);

        

希望这可能会有所帮助..

【讨论】:

以上是关于Android小部件listview句柄点击的主要内容,如果未能解决你的问题,请参考以下文章

android小部件内ListView中的多个可点击项目

Android小部件ListView项目点击不起作用

小部件onClick中的Android ListView不起作用

如何在android中获取主屏幕小部件列表项的点击事件[重复]

将 headerView 添加到小部件上的小部件中的 listView 调整大小 android

带有 ListView 的 Android 小部件无法正确加载项目