2017-02-14 4 views
1

나는 내 listview의 모든 항목에 onClick Listener를 추가하고 싶지만 작동을 시도한 것은 없습니다.앱 위젯의 목록보기 항목에 onClick Listener를 연결하는 방법

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

리스트 widget.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> 

는 아무 일이 없다

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

리스트 item.xml :

여기 내 RemoteViewsFactory입니다.

공급자가 목록보기에 직접 걸어 둔다는 의도가 있지만 제대로 스크롤 할 수 없지만 더 이상 스크롤하지 않고 개별 항목에 응답 할 수 없습니다.

답변

1

official doc에서 개별 항목에 동작을 추가 의 섹션 말 :

AppWidgetProvider 클래스 사용에서 설명한대로 일반적으로 setOnClickPendingIntent()를 사용하여 버튼이 액티비티를 시작하게하는 것과 같이 객체의 클릭 비헤이비어를 설정합니다. 그러나이 방법은 개별 컬렉션 항목의 하위 뷰에는 허용되지 않습니다. 예를 들어, 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; 
    } 

또한 setPendingIntentTemplate 컬렉션에 하나의 PendingIntent 템플릿을 설정하는 대신 setOnClickPendingIntent의 사용되어야한다. 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); 
    } 
0

당신은 위치가 목록보기에서 항목의 인덱스 그런 일을 시도 할 수

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

안녕하세요; 어디에서해야합니까? RemoteViewsFactory에는 getListView() 메소드가 없습니다. https://developer.android.com/reference/android/widget/RemoteViewsService.RemoteViewsFactory.html – Wookiee

+0

다음과 같이 할 수 있습니다 :'ListView l = (ListView) findViewById (R.layout.file_that_you_inflate);'그리고 나서'l.setOnItemClickListener (new OnItemClickListener() { @Override public void onItemClick (어댑터보기 arg0,보기 arg1, int 위치, 긴 arg3) { 토스트.makeText (SuggestionActivity.this, ""+ position, Toast.LENGTH_SHORT) .show(); } }); ' – mychemicalro

+0

listView에는 어댑터가 있어야합니다. – mychemicalro

관련 문제