RemoteViews嵌入ListView复杂布局

来源:互联网 发布:深圳市软件外包公司 编辑:程序博客网 时间:2024/06/01 18:37

主要函数

public void setRemoteAdapter (int appWidgetId, int viewId, Intent intent)

当在widgets中使用集合(比如说ListView, StackView等等),在单独的一个条目中设置PendingIntents是非常浪费的,并且是不被允许的。然而一个单独的PendingIntents模板可以设置在集合里,参见setPendingIntentTemplate(int, PendingIntent),并且一个给定条目的单独的on-click的动作可以通过在条目上设置fillInIntent来区别。然后这个fillInIntent就和PendingIntent的实例结合在一起决定最终的被点击执行的intent。规定如下:在PendingIntent实例里,左边为空白的任何区域,但由fillInIntent提供的,将被重写,同时PendingIntent的结果将被使用。然后PendingIntent将被在fillInIntent中设置的有联系的字段填充。

public void setOnClickFillInIntent (int viewId, Intent fillInIntent)


当在widgets中使用集合(比如说ListView, StackView等等)时,在单独的一个条目中设置PendingIntent代价是很昂贵的,因此也是不被允许的。这个方法应该在集合中设置一个单独的PendingIntent模板,然后每一个单独的条目都可以通过setOnClickFillInIntent(int, Intent)来区别他们的点击事件。

public void setPendingIntentTemplate (int viewId, PendingIntent pendingIntentTemplate)


1.布局文件

在Mantifest中:

        

[java] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.test.appwidget"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk android:minSdkVersion="15" />  
  8.   
  9.     <application  
  10.         android:icon="@drawable/ic_launcher"  
  11.         android:label="@string/app_name" >  
  12.           
  13.         <receiver android:name=".CustomWidget">  
  14.             <intent-filter >  
  15.                 <action android:name="android.appwidget.action.APPWIDGET_UPDATE"></action>  
  16.             </intent-filter>  
  17.             <meta-data   
  18.                 android:name="android.appwidget.provider"  
  19.                 android:resource="@xml/custom_widget">  
  20.             </meta-data>  
  21.         </receiver>  
  22.           
  23.            <service   
  24.             android:name=".UpdateService"  
  25.             android:permission="android.permission.BIND_REMOTEVIEWS"  
  26.             android:exported="false" >  
  27.         </service>  
  28.     </application>  
  29.   
  30. </manifest>  

在res/xml/下建立custom_widget.xml文件

    

[java] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <appwidget-provider   
  3.     xmlns:android = "http://schemas.android.com/apk/res/android"  
  4.     android:minWidth="280dp"  
  5.     android:minHeight="280dp"  
  6.     android:initialLayout="@layout/widget_layout"  
  7.     android:updatePeriodMillis="0">   
  8.     <!-- sdk1.5之后updatePeriodMillis已失效,置为0,循环执行自行在代码中实现 -->  
  9. </appwidget-provider>  

在res/layout/下建立widget_layout和widget_list_item布局文件

   widget_layout.xml

[java] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <FrameLayout   
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:background="#000000">  
  7.       
  8.     <ListView   
  9.         android:id="@+id/widget_list"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:scrollbars="none"  
  13.         android:divider="@drawable/widget_list_divider"  
  14.         android:listSelector="@drawable/list_bg_selector"   
  15.         android:layout_marginTop="4dp"  
  16.         android:layout_marginBottom="4dp"  
  17.         android:layout_marginLeft = "5dp"  
  18.         android:cacheColorHint="#00000000"/>   
  19.       
  20. </FrameLayout>  

widget_list_item布局文件:

[java] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <LinearLayout   
  3.     xmlns:android = "http://schemas.android.com/apk/res/android"  
  4.     android:id = "@+id/widget_list_item_layout"  
  5.     android:layout_width = "fill_parent"  
  6.     android:layout_height = "fill_parent"  
  7.     android:background="#00000000"  
  8.     android:orientation="vertical">   
  9.     <TextView   
  10.         android:id = "@+id/widget_list_item_tv"  
  11.         android:layout_width = "fill_parent"  
  12.         android:layout_height = "wrap_content"  
  13.         android:layout_marginTop = "10dip"  
  14.         android:layout_marginRight="10dip"  
  15.         android:layout_marginLeft = "20dp"  
  16.         android:lineSpacingExtra = "4dip"  
  17.         android:textSize="20sp"  
  18.         android:textColor="#FFFFF0"/>  
  19. </LinearLayout>  

CustomWidget.java

[java] view plaincopy
  1. package com.test.appwidget;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import android.app.PendingIntent;  
  7. import android.appwidget.AppWidgetManager;  
  8. import android.appwidget.AppWidgetProvider;  
  9. import android.content.ComponentName;  
  10. import android.content.Context;  
  11. import android.content.Intent;  
  12. import android.widget.RemoteViews;  
  13.   
  14. public class CustomWidget extends AppWidgetProvider{  
  15.   
  16.     private static List<String> sList;  
  17.   
  18.     static{  
  19.         sList = new ArrayList<String>();  
  20.         sList.add("第一条新闻");  
  21.         sList.add("第二条新闻");  
  22.         sList.add("第三条新闻");  
  23.         sList.add("第四条新闻");  
  24.         sList.add("第五条新闻");  
  25.         sList.add("第六条新闻");  
  26.     }  
  27.   
  28.     private ComponentName thisWidget;  
  29.     private RemoteViews remoteViews;  
  30.   
  31.     @Override  
  32.     public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){  
  33.   
  34.         thisWidget = new ComponentName(context, CustomWidget.class);  
  35.         remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);  
  36.   
  37.         Intent intent = new Intent(context, UpdateService.class);  
  38.         intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[0]);  
  39.         //设置适配器  
  40.         remoteViews.setRemoteAdapter(R.id.widget_list, intent);  
  41.   
  42.         Intent intent2 = new Intent();  
  43.         //TODO  
  44.         //intent2.setComponent(new ComponentName("包名", "类名"));  
  45.         PendingIntent pendingIntentTemplate = PendingIntent.getActivity(context, 1, intent2, PendingIntent.FLAG_UPDATE_CURRENT);  
  46.           
  47.         //拼接PendingIntent  
  48.         remoteViews.setPendingIntentTemplate(R.id.widget_list, pendingIntentTemplate);  
  49.   
  50.         //更新RemoteViews  
  51.         appWidgetManager.updateAppWidget(thisWidget, remoteViews);  
  52.           
  53.         AppWidgetManager manager = AppWidgetManager.getInstance(context);  
  54.         manager.notifyAppWidgetViewDataChanged(appWidgetIds[0], R.id.widget_list);  
  55.     }  
  56.   
  57.     @Override  
  58.     public void onDisabled(Context context) {  
  59.         super.onDisabled(context);  
  60.     }  
  61.   
  62.   
  63.     public static List<String> getList(){  
  64.         return sList;  
  65.     }  
  66.   
  67. }  

UpdateService.java

[java] view plaincopy
  1. package com.test.appwidget;  
  2.   
  3. import java.util.List;  
  4.   
  5. import android.content.Context;  
  6. import android.content.Intent;  
  7. import android.os.IBinder;  
  8. import android.os.Looper;  
  9. import android.widget.RemoteViews;  
  10. import android.widget.RemoteViewsService;  
  11.   
  12. public class UpdateService extends RemoteViewsService {  
  13.   
  14.     @Override  
  15.     public void onStart(Intent intent, int startId){  
  16.         super.onCreate();  
  17.     }  
  18.     @Override  
  19.     public IBinder onBind(Intent intent) {  
  20.         return super.onBind(intent);  
  21.     }  
  22.     @Override  
  23.     public RemoteViewsFactory onGetViewFactory(Intent intent) {  
  24.         return new ListRemoteViewsFactory(this.getApplicationContext(), intent);  
  25.     }  
  26.   
  27.     class ListRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory{  
  28.   
  29.         private final Context mContext;  
  30.         private final List<String> mList;  
  31.   
  32.         public ListRemoteViewsFactory(Context context, Intent intent){  
  33.             mContext = context;  
  34.             mList = CustomWidget.getList();  
  35.   
  36.             if(Looper.myLooper() == null){  
  37.                 Looper.prepare();  
  38.             }  
  39.         }  
  40.   
  41.         @Override  
  42.         public void onCreate() {  
  43.         }  
  44.   
  45.         @Override  
  46.         public void onDataSetChanged() {  
  47.         }  
  48.   
  49.         @Override  
  50.         public void onDestroy() {  
  51.             mList.clear();  
  52.         }  
  53.   
  54.         @Override  
  55.         public int getCount() {  
  56.             return mList.size();  
  57.         }  
  58.   
  59.         @Override  
  60.         public RemoteViews getViewAt(int position) {  
  61.             if(position<0 || position>=mList.size())  
  62.                 return null;  
  63.             String content = mList.get(position);  
  64.             final RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.widget_list_item);  
  65.   
  66.             Intent intent = new Intent();  
  67.             //TODO  
  68.             //intent.setComponent(new ComponentName("包名", "类名"));  
  69.             //与CustomWidget中remoteViews.setPendingIntentTemplate配对使用  
  70.             rv.setOnClickFillInIntent(R.id.widget_list_item_layout, intent);  
  71.   
  72.             rv.setTextViewText(R.id.widget_list_item_tv, content);  
  73.   
  74.             return rv;  
  75.         }  
  76.   
  77.         @Override  
  78.         public RemoteViews getLoadingView() {  
  79.             return null;  
  80.         }  
  81.   
  82.         @Override  
  83.         public int getViewTypeCount() {  
  84.             return 1;  
  85.         }  
  86.   
  87.         @Override  
  88.         public long getItemId(int position) {  
  89.             return position;  
  90.         }  
  91.   
  92.         @Override  
  93.         public boolean hasStableIds() {  
  94.             return true;  
  95.         }  
  96.     }  
  97. }  
https://github.com/lions88/RemoteViews