2017-02-27 77 views
0

我可以在通知的內容視圖中放置ListView嗎?如果是這樣,我該怎麼做?Android - 通知內容視圖中的ListView

在另一個應用程序中,我使用RemoteViews.setRemoteAdapter(),RemoteViewsService和RemoteViewsService.RemoteViewsFactory工作,在一個小部件內部有一個ListView。

我試過對通知做同樣的事情,但它不起作用。該應用程序不會崩潰,日誌中沒有錯誤,但ListView未顯示。 RemoteViewsService甚至沒有被調用。

創建通知:

NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 

RemoteViews views = new RemoteViews(getPackageName(), R.layout.notification); 
views.setRemoteAdapter(R.id.lvNotificationList, new Intent(this, ListService.class)); 

builder 
     .setSmallIcon(R.mipmap.ic_launcher) 
     .setOngoing(true) 
     .setContentTitle("Test") 
     .setContent(views) 
     .setCustomBigContentView(views) 
     .setCustomHeadsUpContentView(views) 
     .setCustomContentView(views); 
Notification not = builder.build(); 

通知佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="My list should appear below this" /> 

<ListView 
    android:id="@+id/lvNotificationList" 
    android:layout_width="match_parent" 
    android:layout_height="100dp" /> 

ListService:ListService的清單中

public class ListService extends RemoteViewsService { 
    private static final String TAG = "MyTag"; 

    @Override 
    public RemoteViewsFactory onGetViewFactory(Intent intent) { 
     Log.d(TAG, "onGetViewFactory()"); 
     return new ListProvider(getApplicationContext()); 
    } 
} 

宣言:

<service 
    android:name=".ListService" 
    android:permission="android.permission.BIND_REMOTEVIEWS" /> 

ListProvider:

public class ListProvider implements RemoteViewsService.RemoteViewsFactory { 
    private static final String TAG = "MyTag"; 

    private Context context; 

    public ListProvider(Context context) { 
     this.context = context; 
    } 

    @Override 
    public void onCreate() { 
    } 

    @Override 
    public void onDestroy() { 
    } 

    @Override 
    public int getCount() { 
     return songs.size(); 
    } 

    @Override 
    public RemoteViews getViewAt(int position) { 
     Log.d(TAG, "ListProvider.getViewAt(), position=" + position); 

     RemoteViews ret = new RemoteViews(context.getPackageName(), R.layout.row_notification_list); 
     ... 
     return ret; 
    } 

    @Override 
    public RemoteViews getLoadingView() { 
     return null; 
    } 

    @Override 
    public int getViewTypeCount() { 
     return 1; 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public boolean hasStableIds() { 
     return true; 
    } 

    @Override 
    public void onDataSetChanged() { 
    } 
} 

ListView的上述TextView的顯示,但該列表保持空...

回答

0

看起來我應該有RTFM ...從https://developer.android.com/reference/android/widget/RemoteViews.html

void setRemoteAdapter(int viewId,Intent intent)

Equival調用setRemoteViewsAdapter(Intent)。 只能用於應用小部件。

所以我想回答我的問題:「我可以把通知的內容視圖一個ListView」是沒有 ...

+0

但這真的只是不可能?有任何想法嗎? –

相關問題