问题
I have a Widget that needs to be populated with dynamic content.
Specifically I'd like it to contain a ViewFlipper whose Views are updated at runtime.
I'm not clear on how to access the ViewFlipper from the onUpdate method of my AppWidgetProvider
(assuming this is the correct place to affect change on the Widget and all the examples I'm finding on-line deal with static layouts that have all their elements defined in the xml).
So...
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
//in pseudo code I'm looking for something that lets me do this...
**(ViewFlipper) vf = remoteViews.findViewById(R.id.myFlipper);**
//EDIT:
//based on commonsware's suggestion... I'm here...
ImageView stationView = (ImageView)((Activity)context).getLayoutInflater().inflate(R.layout.widget_station, null);
//how what?
pushWidgetUpdate(context, remoteViews);
}
However, RemoteViews
doesn't have such a method, so, I'm perplexed as to how to handle this. I see according to the docs that Widgets support ViewFlippers... but do they not support populating them dynamically?
回答1:
Specifically I'd like it to contain a ViewFlipper whose Views are updated at runtime
There are two possible interpretations of this request.
One is that you want the ViewFlipper
to hold a fixed number of "pages", and each "page" always has the same widgets, but you want to populate the contents of those widgets. In that case, you populate them the same way that you do anything else in an app widget: by referring to those specific widgets, ignoring the ViewFlipper
.
Or, you want a ViewFlipper
that has varying number of pages at different times, or where the pages have varying layouts. In that case, you can try addView()
for each page, specifying the ViewFlipper
id as the first parameter. Or, try AdapterViewFlipper
, and see if setRemoteAdapter()
will cause the app widget to reload its contents from the RemoteViewsService
.
来源:https://stackoverflow.com/questions/22582260/how-do-i-access-a-viewflipper-in-a-widget-to-populate-its-content-dynamically