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

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.appwidget"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk android:minSdkVersion="15" /> <application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" > <receiver android:name=".CustomWidget">
<intent-filter >
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"></action>
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/custom_widget">
</meta-data>
</receiver> <service
android:name=".UpdateService"
android:permission="android.permission.BIND_REMOTEVIEWS"
android:exported="false" >
</service>
</application> </manifest>

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

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

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

widget_layout.xml

<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"> <ListView
android:id="@+id/widget_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none"
android:divider="@drawable/widget_list_divider"
android:listSelector="@drawable/list_bg_selector"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:layout_marginLeft = "5dp"
android:cacheColorHint="#00000000"/> </FrameLayout>

widget_list_item布局文件:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
android:id = "@+id/widget_list_item_layout"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:background="#00000000"
android:orientation="vertical">
<TextView
android:id = "@+id/widget_list_item_tv"
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:layout_marginTop = "10dip"
android:layout_marginRight="10dip"
android:layout_marginLeft = "20dp"
android:lineSpacingExtra = "4dip"
android:textSize="20sp"
android:textColor="#FFFFF0"/>
</LinearLayout>

CustomWidget.java

package com.test.appwidget;

import java.util.ArrayList;
import java.util.List; import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews; public class CustomWidget extends AppWidgetProvider{ private static List<String> sList; static{
sList = new ArrayList<String>();
sList.add("第一条新闻");
sList.add("第二条新闻");
sList.add("第三条新闻");
sList.add("第四条新闻");
sList.add("第五条新闻");
sList.add("第六条新闻");
} private ComponentName thisWidget;
private RemoteViews remoteViews; @Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){ thisWidget = new ComponentName(context, CustomWidget.class);
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout); Intent intent = new Intent(context, UpdateService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[0]);
//设置适配器
remoteViews.setRemoteAdapter(R.id.widget_list, intent); Intent intent2 = new Intent();
//TODO
//intent2.setComponent(new ComponentName("包名", "类名"));
PendingIntent pendingIntentTemplate = PendingIntent.getActivity(context, 1, intent2, PendingIntent.FLAG_UPDATE_CURRENT); //拼接PendingIntent
remoteViews.setPendingIntentTemplate(R.id.widget_list, pendingIntentTemplate); //更新RemoteViews
appWidgetManager.updateAppWidget(thisWidget, remoteViews); AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.notifyAppWidgetViewDataChanged(appWidgetIds[0], R.id.widget_list);
} @Override
public void onDisabled(Context context) {
super.onDisabled(context);
} public static List<String> getList(){
return sList;
} }

UpdateService.java

package com.test.appwidget;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.os.Looper;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService; public class UpdateService extends RemoteViewsService { @Override
public void onStart(Intent intent, int startId){
super.onCreate();
}
@Override
public IBinder onBind(Intent intent) {
return super.onBind(intent);
}
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
return new ListRemoteViewsFactory(this.getApplicationContext(), intent);
} class ListRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory{ private final Context mContext;
private final List<String> mList; public ListRemoteViewsFactory(Context context, Intent intent){
mContext = context;
mList = CustomWidget.getList(); if(Looper.myLooper() == null){
Looper.prepare();
}
} @Override
public void onCreate() {
} @Override
public void onDataSetChanged() {
} @Override
public void onDestroy() {
mList.clear();
} @Override
public int getCount() {
return mList.size();
} @Override
public RemoteViews getViewAt(int position) {
if(position<0 || position>=mList.size())
return null;
String content = mList.get(position);
final RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.widget_list_item); Intent intent = new Intent();
//TODO
//intent.setComponent(new ComponentName("包名", "类名"));
//与CustomWidget中remoteViews.setPendingIntentTemplate配对使用
rv.setOnClickFillInIntent(R.id.widget_list_item_layout, intent); rv.setTextViewText(R.id.widget_list_item_tv, content); return rv;
} @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;
}
}
}

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. Subversion/Git/ReviewBoard工作流程

    根据My (work)Git Workflow进行修改,在 Windows下进行测试,http://mojodna.net/2009/02/24/my-work-git-workflow.html 目 ...

  2. thinkphp 比对过去时间距离现在时间多少的问题

    <?php import('ORG.Util.Date');// 导入日期类 $Date = new Date();//实例化类 $time_diff = $Date->timeDiff( ...

  3. photoshopcs5 win7安装报错的解决

    因为之前安装了绿色中文破解版的PhotoShop CS5,虽然卸载了,但是注册表还可能残留了其它信息,导致在安装Adobe PhotoShop CS5英文版时一直显示 (Exit Code: 7 ER ...

  4. 实用技巧:简单而有用的nohup命令介绍(转)

    简单而有用的nohup命令在UNIX/LINUX中,普通进程用&符号放到后台运行,如果启动该程序的控制台logout,则该进程随即终止. 要实现守护进程,一种方法是按守护进程的规则去编程(本站 ...

  5. lenky的个人站点 ----LINUX 内核进程

    http://www.lenky.info/archives/category/nix%E6%8A%80%E6%9C%AF/%E5%86%85%E6%A0%B8%E6%8A%80%E6%9C%AF

  6. Qt 学习之路 :文件

    文件操作是应用程序必不可少的部分.Qt 作为一个通用开发库,提供了跨平台的文件操作能力.从本章开始,我们来了解下 Qt 的文件以及输入输出的功能,也就是 I/O 系统. Qt 通过QIODevice提 ...

  7. iOS-iPad开发之popoverController使用介绍

    iOS-iPad开发之popoverController使用介绍 iOS开发UI篇-popoverController使用注意 iOS SDK:自定义Popover(弹出窗口) 实现的简单例子: // ...

  8. iOS开发之支付宝集成

    项目中要用到支付功能,需要支付宝,微信,银联三大支付,所以打算总结一下,写两篇文章,方便以后的查阅, 大家在做的时候也能稍微参考下,用到的地方避免再次被坑.这是第二篇支付宝集成,第一篇银联支付在这里. ...

  9. 11.2 afternoon

    #include<iostream> #include<cstdio> #define ll long long using namespace std; ll n,ans; ...

  10. Gprinter Android SDK V1.0 使用说明

    佳博打印机代理商淘宝店https://shop107172033.taobao.com/index.htm?spm=2013.1.w5002-9520741823.2.Sqz8Pf 在此店购买的打印机 ...