Android AppWidget Listview更改选定的图像按钮onClick [英] Android AppWidget Listview change selected imagebutton onClick

查看:102
本文介绍了Android AppWidget Listview更改选定的图像按钮onClick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个主屏幕小部件:

很容易更新例如activity中徽标旁边的两个buttons,因为我从AppWidgetProvider中的onUpdate方法获得了它们的appWidgetId,但是列表中的imagebuttons是在RemoteViewsFactory类中创建的getViewAt()方法.

It's easy to update for example two buttons next to the logo from activity, because I have their appWidgetIds from onUpdate method from AppWidgetProvider, but imagebuttons in the list are created in RemoteViewsFactory class in getViewAt() method.

@Override
    public RemoteViews getViewAt(int position) {
        RemoteViews row = new RemoteViews(ctxt.getPackageName(), R.layout.list_row_widget);
        row.setTextViewText(R.id.tvDescription, items.get(position).name);

        Intent i = new Intent();
        Bundle extras = new Bundle();
        extras.putBoolean(TimeWidgetProvider.ACTION_WORK_START, true);
        extras.putInt(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        i.putExtras(extras);
        row.setOnClickFillInIntent(R.id.btnStartWork, i);

        return (row);
    }

因此它们每个都有相同的ID(R.id.btnStartWork)和相同的appWidgetId,这不是行ID,而是整个窗口小部件ID.

so every of them have same id (R.id.btnStartWork), and same appWidgetId which is not the row id but the whole widget id.

我需要在单击的行中更改imagebutton,但是因为我每次使用时在Activity及其整个窗口小部件中只有一个appwidget ID,

I need to change imagebutton in the row which I have clicked on, but because I've only one appwidget id in Activity and its for whole widget, everytime I use:

AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getBaseContext());
RemoteViews views = new RemoteViews(getBaseContext().getPackageName(), R.layout.widget_layout);
views.setImageViewResource(R.id.btnStartWork, R.drawable.button_active);
appWidgetManager.updateAppWidget(mAppWidgetId, views);

它会更改第一个图像按钮,而不是所选的按钮.

It changes the first imagebutton not the selected one.

我试图从工厂传递remoteviews行,但是它没有用,因为当我打电话时

I have tried to pass remoteviews row from factory, but it didn't work because when I called

appWidgetManager.updateAppWidget(mAppWidgetId, row);

具有整个窗口小部件的ID,窗口小部件消失了,只有选定的行可见.我可以获取项目在列表中的位置,但这没有帮助,因为它不是viewId. 我已经阅读了许多包括以下内容的教程: http://developer.android.com/guide/topics/appwidgets/index.html

with the id of whole widget, widget was gone and only the selected row was visible. I can get the position of item in list, but it's not helpful because it's not a viewId. I have read many tutorial including this: http://developer.android.com/guide/topics/appwidgets/index.html

但是仍然没有找到答案,这是我的第一个小部件.请帮助.

But still haven't found the answer, It's my first widget. Help please.

谢谢;)

推荐答案

实际上,解决方案非常简单,在getViewAt()方法中,将每个imagebutton的位置保存到extras:

Actually the solution was quite simple, in getViewAt() method save the position of every imagebutton to extras:

extras.putInt(TimeWidgetProvider.POSITION, position);

然后在处理方法中将所选项目的索引存储在某处

then in handling method store somewhere the index of selected item

TimeWidgetProvider.selected = extras.getInt(TimeWidgetProvider.POSITION);

然后,使用此:

appWidgetManager.notifyAppWidgetViewDataChanged(mAppWidgetId, R.id.list);

最后,在getViewAt()方法中再次

更改先前存储的索引上的imagebutton.

at last again in the getViewAt() method change imagebutton on index which you have previously stored.

if (position == TimeWidgetProvider.selected) {
    row.setImageViewResource(R.id.btnStartWork, R.drawable.button_active);
} else {
    row.setImageViewResource(R.id.btnStartWork, R.drawable.button_inactive);
}

整个列表会随着所选项目的状态更改而刷新.

The whole list is refreshed with changed states of selected items.

Thx 塔玛斯·穆勒为此解决方案.

Thx Tamás Müller for this solution.

这篇关于Android AppWidget Listview更改选定的图像按钮onClick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆