如何将onClick侦听器附加到应用程序小部件上的listview项 [英] how attach a onClick Listener to an listview item on an app widget

查看:91
本文介绍了如何将onClick侦听器附加到应用程序小部件上的listview项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢在列表视图的每个项目中添加一个onClick侦听器,但是我没有尝试过.

I like to add a onClick Listener to every item of my listview, but nothing what I have tried work.

这是我的RemoteViewsFactory:

Here is my 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;
    }
}

list-widget.xml

list-widget.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>

list-item.xml

list-item.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>

如果我单击一个项目,则什么也没有发生.

If I click an item, nothing is happen.

如果我将意图直接悬挂在提供程序中并挂在列表视图上,则意图可以正常工作,但随后我将无法滚动并且不对单个项做出响应.

The intent works properly, if I hang it in the provider directly to the listview, but then I can no longer scroll and not respond to individual items.

推荐答案

如使用AppWidgetProvider类中所述,通常使用setOnClickPendingIntent()设置对象的单击行为,例如使按钮启动活动.但是,这种方法不允许用于单个集合项中的子视图(为澄清起见,您可以使用setOnClickPendingIntent()在启动应用程序的Gmail应用程序小部件中设置全局按钮,但不能在单个列表项中设置) ).相反,要向集合中的各个项目添加点击行为,请使用setOnClickFillInIntent().这需要为您的集合视图设置一个待处理的意图模板,然后通过RemoteViewsFactory在集合中的每个项目上设置一个填充意图.

As described in Using the AppWidgetProvider Class, you normally use setOnClickPendingIntent() to set an object's click behavior—such as to cause a button to launch an Activity. But this approach is not allowed for child views in an individual collection item (to clarify, you could use setOnClickPendingIntent() to set up a global button in the Gmail app widget that launches the app, for example, but not on the individual list items). Instead, to add click behavior to individual items in a collection, you use setOnClickFillInIntent(). This entails setting up up a pending intent template for your collection view, and then setting a fill-in intent on each item in the collection via your RemoteViewsFactory.

您的RemoteViewsFactory必须在集合中的每个项目上设置一个填充意图.这样就可以区分给定项目的各个单击动作.然后,将填充意图与PendingIntent模板组合在一起,以确定单击该项目时将要执行的最终意图.

Your RemoteViewsFactory must set a fill-in intent on each item in the collection. This makes it possible to distinguish the individual on-click action of a given item. The fill-in intent is then combined with the PendingIntent template in order to determine the final intent that will be executed when the item is clicked.

您需要调用setOnClickFillInIntent来区分项目的单击行为.像这样:

Your need to call setOnClickFillInIntent to differentiate the item on-click behavior. Something like this:

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;
    }

也应使用setPendingIntentTemplate而不是setOnClickPendingIntent在集合上设置单个PendingIntent模板.在与您的AppWidgetProvider子类相对应的onUpdate方法中设置待处理的意图,如下所示:

Also setPendingIntentTemplate should be use instead of setOnClickPendingIntent to set a single PendingIntent template on the collection. Set the pending intent in onUpdate method corresponding to you subclass of AppWidgetProvider, something like this:

@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);
    }

这篇关于如何将onClick侦听器附加到应用程序小部件上的listview项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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