我将在主屏幕小部件中设置一个列表视图,以像视频波纹管中的此列表视图一样进行操作。
此视频链接下面的内容是我目前无法显示所有项目的内容,但是找不到在单击列表中的项目时打开链接的方法。
public class WidgetProvider extends AppWidgetProvider {
public static final String EXTA_LINK = "extraLink";
public static final String WIDGET_CLICK = "widgetClick";
/*
* this method is called every 30 mins as specified on widgetinfo.xml
* this method is also called on every phone reboot
*/
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
final int N = appWidgetIds.length;
/*int[] appWidgetIds holds ids of multiple instance of your widget
* meaning you are placing more than one widgets on your homescreen*/
for (int i = 0; i < N; ++i) {
RemoteViews remoteViews = updateWidgetListView(context,
appWidgetIds[i]);
appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
private RemoteViews updateWidgetListView(Context context, int appWidgetId) {
//which layout to show on widget
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
//RemoteViews Service needed to provide adapter for ListView
Intent svcIntent = new Intent(context, WidgetService.class);
Intent clickIntent = new Intent(Intent.ACTION_VIEW);
clickIntent.setAction(WIDGET_CLICK);
PendingIntent clickPendingIntent = PendingIntent.getActivity(context, 0,clickIntent,0);
//passing app widget id to that RemoteViews Service
svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
//setting a unique Uri to the intent
//don't know its purpose to me right now
svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
//setting adapter to listview of the widget
remoteViews.setRemoteAdapter(appWidgetId, R.id.listViewWidget,
svcIntent);
//setting an empty view in case of no data
remoteViews.setEmptyView(R.id.listViewWidget, R.id.empty_view);
remoteViews.setPendingIntentTemplate(R.id.listViewWidget, clickPendingIntent);
return remoteViews;
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() != null & intent.getAction().equals(WIDGET_CLICK)) {
String link = intent.getStringExtra(EXTA_LINK); //get the link
intent.setData(Uri.parse(link));
context.startActivity(intent);
}
super.onReceive(context, intent);
}
}
public class ListProvider implements RemoteViewsFactory {
private ArrayList<News> listItemList = new ArrayList<News>();
private Context context = null;
private int appWidgetId;
public ListProvider(Context context, Intent intent) {
this.context = context;
appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
populateListItem();
}
public RemoteViews getViewAt(int position) {
final RemoteViews remoteView = new RemoteViews(
context.getPackageName(), R.layout.list_row);
News listItem = listItemList.get(position);
remoteView.setTextViewText(R.id.content, listItem.articleName);
Intent fillIntent = new Intent();
fillIntent.putExtra(WidgetProvider.EXTA_LINK,listItem.link);
remoteView.setOnClickFillInIntent(R.id.content,fillIntent);
return remoteView;
}
如果任何人都可以帮助我进行研究,而没有使用onclicklistner的小部件列表视图。