Woo*_*iee 4 android listview android-appwidget
我喜欢向我的列表视图的每个项目添加一个 onClick 侦听器,但我尝试过的一切都不起作用。
这是我的 RemoteViewsFactory:
public class MyRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
[...]
public RemoteViews getViewAt(int position) {
final RemoteViews remoteView = new RemoteViews(
context.getPackageName(), R.layout.widget_item);
[...]
final Intent activityIntent = new Intent(context, ListActivity.class);
activityIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
activityIntent.putExtra(AppWidgetManager.ACTION_APPWIDGET_PICK, position);
PendingIntent pI = PendingIntent.getActivity(
context, appWidgetId, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteView.setOnClickPendingIntent(R.id.item_frame, pI);
return remoteView;
}
}
Run Code Online (Sandbox Code Playgroud)
列表小部件.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget_frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" >
<include layout="@layout/picture_frame" />
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/picframe"
android:layout_margin="5dp"
android:gravity="center"
android:loopViews="true" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
列表项.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item_frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:layout_centerVertical="true" >
<TextView
android:id="@+id/date"
style="@style/WidgetItemText" />
<TextView
android:id="@+id/name"
style="@style/WidgetItemText" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
如果我单击一个项目,则什么也不会发生。
意图正常工作,如果我将它直接挂在提供者中的列表视图中,但是我无法再滚动并且无法响应单个项目。
的部分添加行为,个别项目的官方文档说:
如使用 AppWidgetProvider 类中所述,您通常使用 setOnClickPendingIntent() 来设置对象的单击行为,例如使按钮启动 Activity。但是这种方法不允许用于单个集合项中的子视图(澄清一下,您可以使用 setOnClickPendingIntent() 在启动应用程序的 Gmail 应用程序小部件中设置全局按钮,例如,但不能在单个列表项上)。相反,要将单击行为添加到集合中的单个项目,请使用 setOnClickFillInIntent()。这需要为您的集合视图设置一个待定的意图模板,然后通过您的 RemoteViewsFactory 为集合中的每个项目设置一个填充意图。
您的 RemoteViewsFactory 必须为集合中的每个项目设置填充意图。这使得区分给定项目的单个点击操作成为可能。然后将填充意图与 PendingIntent 模板相结合,以确定单击项目时将执行的最终意图。
您需要调用setOnClickFillInIntent来区分项目的点击行为。像这样的东西:
public RemoteViews getViewAt(int position) {
final RemoteViews remoteView = new RemoteViews(
context.getPackageName(), R.layout.widget_item);
[...]
// Next, set a fill-intent, which will be used to fill in the pending intent template
// that is set on the collection view in StackWidgetProvider.
Bundle extras = new Bundle();
extras.putInt(YouWidgetProvider.EXTRA_ITEM, position);
Intent fillInIntent = new Intent();
fillInIntent.putExtras(extras);
// Make it possible to distinguish the individual on-click
// action of a given item
remoteView.setOnClickFillInIntent(R.id.item_frame, fillInIntent);
return remoteView;
}
Run Code Online (Sandbox Code Playgroud)
也setPendingIntentTemplate应该使用而不是setOnClickPendingIntent在集合上设置单个PendingIntent模板。在onUpdate与您的子类相对应的方法中设置挂起的意图AppWidgetProvider,如下所示:
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// update each of the app widgets with the remote adapter
for (int i = 0; i < appWidgetIds.length; ++i) {
...
RemoteViews remoteView = new RemoteViews(context.getPackageName(),
R.layout.widget_item);
...
// This section makes it possible for items to have individualized behavior.
// It does this by setting up a pending intent template. Individuals items of a collection
// cannot set up their own pending intents. Instead, the collection as a whole sets
// up a pending intent template, and the individual items set a fillInIntent
// to create unique behavior on an item-by-item basis.
Intent activityIntent = new Intent(context, ListActivity.class);
// Set the action for the intent.
// When the user touches a particular view.
activityIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
activityIntent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i],
activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteView.setPendingIntentTemplate(R.id.stack_view, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds[i], remoteView);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2791 次 |
| 最近记录: |