from http://blog.csdn.net/liliang497/article/details/8308313

主要函数

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中:

  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文件

  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

  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布局文件:

  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

  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. public static List<String> getList(){
  63. return sList;
  64. }
  65.  
  66. }

UpdateService.java

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

RemoteViews嵌入ListView复杂布局的更多相关文章

  1. Android ListView多布局

    使用listview多布局会出现一点问题: 由于多个item布局给单一的item布局是不一样的,使用起来,contentview的复用会出现问题. 避免出现问题的有这几个方法: 1.重写 getVie ...

  2. 2018-5-28-win10-uwp-动态修改ListView元素布局

    title author date CreateTime categories win10 uwp 动态修改ListView元素布局 lindexi 2018-05-28 15:15:54 +0800 ...

  3. Android 解决ScrollView嵌入ListView | GridView | ScrollView显示问题

    一.ScrollView中嵌套ListView ScrollView和ListView都是滚动结构,很明显如果在ScrollView中加入ListView,可以预见性的知道,肯定会有显示/滚动的问题, ...

  4. Android中ListView错位布局实现(无聊向)

    由于某些原因,需要个错位的页面,在网上找不到好的例子,试着动手写了写. 不考虑配色的完成图如下: 首先考虑的是,listview每一行左右都有可能缩进. 先假设一行的布局就是ImageView,Tex ...

  5. ScrollView中嵌入ListView,GridView冲突的解决(让ListView全显示出来)

    ScrollView中嵌入原生ListView或GridView,会出现ListView,GridView显示不全的问题. 解决方法:重新构造一个ListView或GridView,重写OnMeasu ...

  6. 实现顶部轮播,下部listview经典布局的两种方式

    开头: 在做android开发的时候,我们经常会遇到这样的布局,上面是一个图片轮播图,下面是一些列表的项目.很多新闻app,视频类app都采用这样的布局.起初的时候 由于没有很多参考,我自己想到了一种 ...

  7. Android ListView多布局讲解

    Listview优化是一个老生常谈的事情了,其优化的方面也有很多种,例如,布局重用.在getView()中减少逻辑计算.减少在页面滑动的时候加在图片,而是在页面停止滚动的时候再加在图片.而今天要介绍的 ...

  8. Android修行之路------ListView自定义布局

    主布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android= ...

  9. flutter控件之ListView滚动布局

    ListView即滚动列表控件,能将子控件组成可滚动的列表.当你需要排列的子控件超出容器大小,就需要用到滚动块. import 'package:flutter/material.dart'; cla ...

随机推荐

  1. Best Premium Private Proxy Service | Lime Proxies

    Best Premium Private Proxy Service | Lime Proxies undefined

  2. objective-c保护属性

    #import <Foundation/Foundation.h> @interface ClassVirable : NSObject{ NSInteger year;//保护树形 } ...

  3. zoj 1671 Walking Ant【简单bfs】

    Walking Ant Time Limit: 2 Seconds      Memory Limit: 65536 KB Ants are quite diligent. They sometime ...

  4. 11个让你吃惊的Linux终端命令

  5. 常用的js效验

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. Java keyword具体解释

    訪问控制修饰符号 1)        private 私有的 private keyword是訪问控制修饰符,能够应用于类.方法或字段(在类中声明的变量). 仅仅能在声明 private(内部)类.方 ...

  7. Ruby学习笔记(二)

    1.block 代码块 do...end 或 {} 构成一个代码块,就像常见的 .each后面跟的代码块. my_nums = [1,2,3] my_double_nums = my_nums.col ...

  8. Delphi TFindDialog TReplaceDialog对话框在Memo中的使用

    Delphi TFindDialog TReplaceDialog对话框的使用 下载地址1: http://download.csdn.net/detail/teststudio/6408383   ...

  9. cs模式与bs模式

     关于CS(Client-Server)模式和BS(Browser-Server)模式的水很深,盆地自己也认为对此了解不够透彻,但作为手机客户端设计,如果不对CS.BS做一定程度的了解,是很容易出现一 ...

  10. hdu-5009-Paint Pearls-dp

    由题意我们能够知道,花费最多为n. 所以单次最多涂掉sqrt(n)种颜色. dp[i]:涂到第i个位置.之前的花费最少为多少. biao[i][j]:在第i个位置,往前涂j-1种颜色,涂到哪个位置. ...