2017-02-14 1 views
1

J'aime ajouter un onClick Listener à chaque élément de ma listview, mais rien de ce que j'ai essayé de travailler.comment attacher un onClick Listener à un élément listview sur un widget d'application

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

liste-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> 

liste-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> 

Si je clique sur un élément, rien ne se produit.

L'intention fonctionne correctement, si je l'accrocher dans le fournisseur directement à la liste, mais je ne peux plus faire défiler et ne pas répondre aux éléments individuels.

Répondre

1

La section de comportement Ajout aux éléments individuels dans le official doc disent:

Comme décrit dans Utilisation de la classe AppWidgetProvider, vous utilisez normalement setOnClickPendingIntent() pour définir le comportement de clic d'un objet, par exemple pour provoquer le lancement d'une activité par un bouton. Mais cette approche n'est pas autorisée pour les vues enfant dans un élément de collection individuel (pour clarifier, vous pouvez utiliser setOnClickPendingIntent() pour configurer un bouton global dans le widget de l'application Gmail qui lance l'application, par exemple, mais pas sur les éléments individuels). Au lieu de cela, pour ajouter un comportement de clic aux éléments individuels d'une collection, vous utilisez setOnClickFillInIntent(). Cela implique la configuration d'un modèle d'intention en attente pour votre vue de collection, puis la définition d'une intention de remplacement pour chaque élément de la collection via votre RemoteViewsFactory.

Votre RemoteViewsFactory doit définir un objectif de remplissage pour chaque élément de la collection. Cela permet de distinguer l'action individuelle sur un clic d'un élément donné. L'intention de remplissage est ensuite combinée avec le modèle PendingIntent afin de déterminer l'intention finale qui sera exécutée lorsque l'utilisateur clique sur l'élément.

Votre besoin d'appeler setOnClickFillInIntent pour différencier le comportement du clic sur l'élément. Quelque chose comme ceci:

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

également setPendingIntentTemplate devrait être utilisé au lieu de setOnClickPendingIntent pour définir un modèle unique PendingIntent sur la collection. Définir l'intention dans l'attente onUpdate méthode pour vous correspondant de la sous-classe AppWidgetProvider, quelque chose comme ceci:

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

Vous pouvez essayer quelque chose comme ça, où la position est l'index de votre article dans le listview

ListView lv = getListView(); 
setContentView(lv); 
lv.setOnItemClickListener(new OnItemClickListener() 
{ 
@Override 
public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3) 
{ 
    Toast.makeText(SuggestionActivity.this, "" + position, Toast.LENGTH_SHORT).show(); 
} 
}); 
+0

Salut; où devrais-je faire cela? RemoteViewsFactory n'a pas de méthode getListView(). https://developer.android.com/reference/android/widget/RemoteViewsService.RemoteViewsFactory.html – Wookiee

+0

Vous pouvez faire: 'ListView l = (ListView) findViewById (R.layout.file_that_you_inflate);' et ensuite 'l.setOnItemClickListener (nouvelle OnItemClickListener() { @Override publique vide onItemClick (AdapterView arg0, Vue arg1, position int, long arg3) { Toast.makeText (SuggestionActivity.this, "" + position, Toast.LENGTH_SHORT) .show(); } }); ' – mychemicalro

+0

Pour la listView, vous devez également avoir un adaptateur. – mychemicalro