Я реализую виджет новостей для android. В обновлении я получаю все данные (около 50 статей, в которых есть текст и ссылки). Я буду sh реализовывать левую и правую кнопки для переключения между новостями. Но onReceive я могу вызвать только onUpdate, и onUpdate снова получит все элементы. Как я могу реализовать левый и правый щелчок, не выбирая все данные снова, но используя существующие данные. И еще один вопрос: если я определю глобальную переменную в AppWidgetProvider, она будет собираться сборщиком мусора в какой-то момент или она «живет» вечно с виджетом?
Код:
public class MyWidgetProvider extends AppWidgetProvider {
private static final String RIGHT_CLICKED = "RIGHT_CLICKED";
private static final String LEFT_CLICKED = "LEF_CLICKED";
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (RIGHT_CLICKED.equals(intent.getAction())) {
//Get next content on list
}
if (LEFT_CLICKED.equals(intent.getAction())) {
//Get prev content on list
}
}
@Override
public void onUpdate(final Context context, final AppWidgetManager appWidgetManager, int[] appWidgetIds) {
//Fetching all stories from RSS
RSSFetchCallback rssFetchCallback = new RSSFetchCallback() {
@Override
public void onComplete(ArrayList<Story> stories) {
ComponentName thisWidget = new ComponentName(context, MyWidgetProvider.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
for (int widgetId : allWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.appwidget);
remoteViews.setTextViewText(R.id.textViewContent, stories.get(0).getContent()); //Setting 0 story for example
remoteViews.setTextViewText(R.id.textViewHeadline, stories.get(0).getTitle()); //Setting 0 story for example
remoteViews.setOnClickPendingIntent(R.id.buttonRight, getPendingSelfIntent(context, RIGHT_CLICKED));
remoteViews.setOnClickPendingIntent(R.id.buttonLeft, getPendingSelfIntent(context, LEFT_CLICKED));
appWidgetManager.updateAppWidget(widgetId, remoteViews);
//Loading R.id.imageView with Glide
AppWidgetTarget awt = new AppWidgetTarget(context, R.id.imageView, remoteViews, widgetId) {
@Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
super.onResourceReady(resource, transition);
}
};
RequestOptions options = new RequestOptions().override(300, 300).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher);
Glide.with(context.getApplicationContext())
.asBitmap()
.load(stories.get(0).getImgUrl())//Setting 0 story image for example
.apply(options)
.into(awt);
//End Loading R.id.imageView with Glide
}
}
};
RSSFetcher.getData(rssFetchCallback);
}
protected PendingIntent getPendingSelfIntent(Context context, String action) {
Intent intent = new Intent(context, getClass());
intent.setAction(action);
return PendingIntent.getBroadcast(context, 0, intent, 0);
}
}