Android Widget 小部件(四---完結(jié)) 使用ListView遂蛀、GridView谭跨、StackView、ViewFlipper展示W(wǎng)idget
分類: Android
2014-08-10 23:13
2596人閱讀
評(píng)論(1)
官方有話這樣說:
A RemoteViews object (and, consequently, an App Widget) can support the following layout classes:
FrameLayout
LinearLayout
RelativeLayout
And the following widget classes:
AnalogClock
Button
Chronometer
ImageButton
ImageView
ProgressBar
TextView
ViewFlipper
ListView
GridView
StackView
AdapterViewFlipper
Descendants of these classes are not supported.不支持這些類的后代
接下來的示例說明怎么樣實(shí)現(xiàn) 使用ListView李滴、GridView螃宙、StackView、ViewFlipper創(chuàng)建AppWidget
menifest
- <receiver?android:name="com.stone.receiver.WidgetSetProvider">??
- ?????????????<intent-filter>??
- ?????????????????<action?android:name="android.appwidget.action.APPWIDGET_UPDATE"/>??
- ?????????????????<action?android:name="com.stone.action.clickset"/>??
- ?????????????????<action?android:name="com.stone.action.clickset.item"/>??
- ?????????????</intent-filter>??
- ?????????????<meta-data?android:name="android.appwidget.provider"??
- ?????????????????android:resource="@xml/set_widget_provider"/>??
- </receiver>??
- <receiver?android:name="com.stone.receiver.WidgetSetProvider">??
- ?????????????<intent-filter>??
- ?????????????????<action?android:name="android.appwidget.action.APPWIDGET_UPDATE"/>??
- ?????????????????<action?android:name="com.stone.action.clickset"/>??
- ?????????????????<action?android:name="com.stone.action.clickset.item"/>??
- ?????????????</intent-filter>??
- ?????????????<meta-data?android:name="android.appwidget.provider"??
- ?????????????????android:resource="@xml/set_widget_provider"/>??
- </receiver>??
res/xml/set_widget_provider.xml
- <?xml?version="1.0"?encoding="utf-8"?>??
- <appwidget-provider?xmlns:android="http://schemas.android.com/apk/res/android"??
- ????android:minWidth="250dp"??
- ????android:minHeight="180dp"??
- ????android:updatePeriodMillis="5000"??
- ????android:previewImage="@drawable/ic_launcher"??
- ????android:initialLayout="@layout/collections_view_widget"??
- ????android:resizeMode="horizontal|vertical"??
- ????android:autoAdvanceViewId="@id/viewflipper"??>??
- ????<!--???
- ????????計(jì)算size的公式:?(70*n)?-30??n為部件所需的大小(占幾格)???當(dāng)前的就是??4x4??
- ????????minResizeWidth??
- ????????minResizeHeight???能被調(diào)整的最小寬高悬嗓,若大于minWidth?minHeight?則忽略??
- ????????label???選擇部件時(shí)看到標(biāo)簽??
- ????????icon????選擇部件時(shí)看到圖標(biāo)??
- ????????updatePeriodMillis??更新時(shí)間間隔??
- ????????previewImage????選擇部件時(shí)?展示的圖像??3.0以上使用??
- ????????initialLayout???布局文件??
- ????????resizeMode??????調(diào)整size模式??
- ????????configure???????如果需要在啟動(dòng)前先啟動(dòng)一個(gè)Activity進(jìn)行設(shè)置污呼,在這里給出Activity的完整類名??
- ????????autoAdvanceViewId=@id/xx????與集合部件一起使用,指定該集合item自動(dòng)推進(jìn)?暫只發(fā)現(xiàn)對(duì)stackview有效包竹,會(huì)自動(dòng)一段時(shí)間推進(jìn)到下一個(gè)??
- ??????????
- ????????集合部件:3.0后才有燕酷。set?view:ListView、GridView周瞎、StackView苗缩、AdapterViewFlipper??
- ????????ViewFlipper?為非集合部件??
- ?????-->??
- </appwidget-provider>??
- <?xml?version="1.0"?encoding="utf-8"?>??
- <appwidget-provider?xmlns:android="http://schemas.android.com/apk/res/android"??
- ????android:minWidth="250dp"??
- ????android:minHeight="180dp"??
- ????android:updatePeriodMillis="5000"??
- ????android:previewImage="@drawable/ic_launcher"??
- ????android:initialLayout="@layout/collections_view_widget"??
- ????android:resizeMode="horizontal|vertical"??
- ????android:autoAdvanceViewId="@id/viewflipper"??>??
- ????<!--???
- ????????計(jì)算size的公式:?(70*n)?-30??n為部件所需的大小(占幾格)???當(dāng)前的就是??4x4??
- ????????minResizeWidth??
- ????????minResizeHeight???能被調(diào)整的最小寬高,若大于minWidth?minHeight?則忽略??
- ????????label???選擇部件時(shí)看到標(biāo)簽??
- ????????icon????選擇部件時(shí)看到圖標(biāo)??
- ????????updatePeriodMillis??更新時(shí)間間隔??
- ????????previewImage????選擇部件時(shí)?展示的圖像??3.0以上使用??
- ????????initialLayout???布局文件??
- ????????resizeMode??????調(diào)整size模式??
- ????????configure???????如果需要在啟動(dòng)前先啟動(dòng)一個(gè)Activity進(jìn)行設(shè)置声诸,在這里給出Activity的完整類名??
- ????????autoAdvanceViewId=@id/xx????與集合部件一起使用酱讶,指定該集合item自動(dòng)推進(jìn)?暫只發(fā)現(xiàn)對(duì)stackview有效,會(huì)自動(dòng)一段時(shí)間推進(jìn)到下一個(gè)??
- ??????????
- ????????集合部件:3.0后才有彼乌。set?view:ListView泻肯、GridView渊迁、StackView、AdapterViewFlipper??
- ????????ViewFlipper?為非集合部件??
- ?????-->??
- </appwidget-provider>??
layout/collections_view_widget.xml
- <?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"?>??
- ??
- ????<LinearLayout??
- ????????android:layout_width="fill_parent"??
- ????????android:layout_height="wrap_content"?>??
- ??
- ????????<Button??
- ????????????android:id="@+id/btn_listview"??
- ????????????android:layout_width="0dp"??
- ????????????android:layout_height="wrap_content"??
- ????????????android:layout_weight="1"??
- ????????????android:text="listview"?/>??
- ??
- ????????<Button??
- ????????????android:id="@+id/btn_gridview"??
- ????????????android:layout_width="0dp"??
- ????????????android:layout_height="wrap_content"??
- ????????????android:layout_weight="1"??
- ????????????android:text="gridview"?/>??
- ??
- ????????<Button??
- ????????????android:id="@+id/btn_stackview"??
- ????????????android:layout_width="0dp"??
- ????????????android:layout_height="wrap_content"??
- ????????????android:layout_weight="1"??
- ????????????android:text="stackview"?/>??
- ??
- ????????<Button??
- ????????????android:id="@+id/btn_viewflipper"??
- ????????????android:layout_width="0dp"??
- ????????????android:layout_height="wrap_content"??
- ????????????android:layout_weight="1"??
- ????????????android:text="viewflipper"?/>??
- ????</LinearLayout>??
- ??
- ????<FrameLayout??
- ????????xmlns:android="http://schemas.android.com/apk/res/android"??
- ????????android:layout_width="match_parent"??
- ????????android:layout_height="match_parent"??
- ????????android:background="#80000000"?>??
- ????????/>??
- ??
- ????????<ListView??
- ????????????android:id="@+id/listview"??
- ????????????android:layout_width="fill_parent"??
- ????????????android:layout_height="fill_parent"?/>??
- ??
- ????????<GridView??
- ????????????android:id="@+id/gridview"??
- ????????????android:layout_width="fill_parent"??
- ????????????android:layout_height="fill_parent"??
- ????????????android:numColumns="2"??
- ????????????android:visibility="gone"?/>??
- ??
- ????????<StackView??
- ????????????android:id="@+id/stackview"??
- ????????????android:layout_width="fill_parent"??
- ????????????android:layout_height="fill_parent"??
- ????????????android:visibility="gone"?/>??
- ??
- ????????<ViewFlipper??
- ????????????android:id="@+id/viewflipper"??
- ????????????android:layout_width="fill_parent"??
- ????????????android:layout_height="fill_parent"??
- ????????????android:autoStart="true"??
- ????????????android:flipInterval="2000"??
- ????????????android:visibility="gone"?>??
- <!--???
- autoStart=true??<==>??startFlipping()??
- flipInterval=2000?<==>?How?long?to?wait?before?flipping?to?the?next?view??
- ?-->??
- ????????????<ImageView??
- ????????????????android:id="@+id/iv1"??
- ????????????????android:layout_width="fill_parent"??
- ????????????????android:layout_height="fill_parent"??
- ????????????????android:background="@drawable/a11"/>??
- ????????????<ImageView??
- ????????????????android:id="@+id/iv2"??
- ????????????????android:layout_width="fill_parent"??
- ????????????????android:layout_height="fill_parent"??
- ????????????????android:background="@drawable/a2"/>??
- ????????????<ImageView??
- ????????????????android:id="@+id/iv3"??
- ????????????????android:layout_width="fill_parent"??
- ????????????????android:layout_height="fill_parent"??
- ????????????????android:background="@drawable/a3"?/>??
- ????????????<ImageView??
- ????????????????android:id="@+id/iv4"??
- ????????????????android:layout_width="fill_parent"??
- ????????????????android:layout_height="fill_parent"??
- ????????????????android:background="@drawable/a4"?/>??
- ????????</ViewFlipper>??
- ??
- ????????<TextView??
- ????????????android:id="@+id/tv_empty"??
- ????????????android:layout_width="fill_parent"??
- ????????????android:layout_height="fill_parent"??
- ????????????android:gravity="center"??
- ????????????android:text="Empty?List"??
- ????????????android:visibility="gone"?/>??
- ????</FrameLayout>??
- ??
- </LinearLayout>??
- <?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"?>??
- ??
- ????<LinearLayout??
- ????????android:layout_width="fill_parent"??
- ????????android:layout_height="wrap_content"?>??
- ??
- ????????<Button??
- ????????????android:id="@+id/btn_listview"??
- ????????????android:layout_width="0dp"??
- ????????????android:layout_height="wrap_content"??
- ????????????android:layout_weight="1"??
- ????????????android:text="listview"?/>??
- ??
- ????????<Button??
- ????????????android:id="@+id/btn_gridview"??
- ????????????android:layout_width="0dp"??
- ????????????android:layout_height="wrap_content"??
- ????????????android:layout_weight="1"??
- ????????????android:text="gridview"?/>??
- ??
- ????????<Button??
- ????????????android:id="@+id/btn_stackview"??
- ????????????android:layout_width="0dp"??
- ????????????android:layout_height="wrap_content"??
- ????????????android:layout_weight="1"??
- ????????????android:text="stackview"?/>??
- ??
- ????????<Button??
- ????????????android:id="@+id/btn_viewflipper"??
- ????????????android:layout_width="0dp"??
- ????????????android:layout_height="wrap_content"??
- ????????????android:layout_weight="1"??
- ????????????android:text="viewflipper"?/>??
- ????</LinearLayout>??
- ??
- ????<FrameLayout??
- ????????xmlns:android="http://schemas.android.com/apk/res/android"??
- ????????android:layout_width="match_parent"??
- ????????android:layout_height="match_parent"??
- ????????android:background="#80000000"?>??
- ????????/>??
- ??
- ????????<ListView??
- ????????????android:id="@+id/listview"??
- ????????????android:layout_width="fill_parent"??
- ????????????android:layout_height="fill_parent"?/>??
- ??
- ????????<GridView??
- ????????????android:id="@+id/gridview"??
- ????????????android:layout_width="fill_parent"??
- ????????????android:layout_height="fill_parent"??
- ????????????android:numColumns="2"??
- ????????????android:visibility="gone"?/>??
- ??
- ????????<StackView??
- ????????????android:id="@+id/stackview"??
- ????????????android:layout_width="fill_parent"??
- ????????????android:layout_height="fill_parent"??
- ????????????android:visibility="gone"?/>??
- ??
- ????????<ViewFlipper??
- ????????????android:id="@+id/viewflipper"??
- ????????????android:layout_width="fill_parent"??
- ????????????android:layout_height="fill_parent"??
- ????????????android:autoStart="true"??
- ????????????android:flipInterval="2000"??
- ????????????android:visibility="gone"?>??
- <!--???
- autoStart=true??<==>??startFlipping()??
- flipInterval=2000?<==>?How?long?to?wait?before?flipping?to?the?next?view??
- ?-->??
- ????????????<ImageView??
- ????????????????android:id="@+id/iv1"??
- ????????????????android:layout_width="fill_parent"??
- ????????????????android:layout_height="fill_parent"??
- ????????????????android:background="@drawable/a11"/>??
- ????????????<ImageView??
- ????????????????android:id="@+id/iv2"??
- ????????????????android:layout_width="fill_parent"??
- ????????????????android:layout_height="fill_parent"??
- ????????????????android:background="@drawable/a2"/>??
- ????????????<ImageView??
- ????????????????android:id="@+id/iv3"??
- ????????????????android:layout_width="fill_parent"??
- ????????????????android:layout_height="fill_parent"??
- ????????????????android:background="@drawable/a3"?/>??
- ????????????<ImageView??
- ????????????????android:id="@+id/iv4"??
- ????????????????android:layout_width="fill_parent"??
- ????????????????android:layout_height="fill_parent"??
- ????????????????android:background="@drawable/a4"?/>??
- ????????</ViewFlipper>??
- ??
- ????????<TextView??
- ????????????android:id="@+id/tv_empty"??
- ????????????android:layout_width="fill_parent"??
- ????????????android:layout_height="fill_parent"??
- ????????????android:gravity="center"??
- ????????????android:text="Empty?List"??
- ????????????android:visibility="gone"?/>??
- ????</FrameLayout>??
- ??
- </LinearLayout>??
集合的數(shù)據(jù)源 需要 繼承?RemoteViewsService
- package?com.stone.service;??
- ??
- import?java.util.ArrayList;??
- import?java.util.List;??
- ??
- import?android.app.PendingIntent;??
- import?android.content.Context;??
- import?android.content.Intent;??
- import?android.os.Bundle;??
- import?android.widget.RemoteViews;??
- import?android.widget.RemoteViewsService;??
- ??
- import?com.stone.R;??
- import?com.stone.receiver.WidgetSetProvider;??
- ??
- /**?
- ?*?繼承自RemoteViewsService?必須重寫onGetViewFactory?
- ?*?該服務(wù)只是用來?創(chuàng)建?集合widget使用的數(shù)據(jù)源?
- ?*?@author?stone?
- ?*/??
- public?class?WidgetSetService?extends?RemoteViewsService?{??
- ??????
- ????public?WidgetSetService()?{??
- ??????????
- ????}??
- ??????
- ????@Override??
- ????public?RemoteViewsFactory?onGetViewFactory(Intent?intent)?{??
- ????????return?new?WidgetFactory(this.getApplicationContext(),?intent);??
- ????}??
- ??????
- ??????
- ????public?class?WidgetFactory?implements?RemoteViewsService.RemoteViewsFactory?{??
- ????????private?static?final?int?mCount?=?10;??
- ????????private?Context?mContext;??
- ????????private?List<String>?mWidgetItems?=?new?ArrayList<String>();??
- ??????????
- ??????????
- ????????public?WidgetFactory(Context?context,?Intent?intent)?{??
- ????????????mContext?=?context;??
- //??????????mAppWidgetId?=?intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,??
- //??????????????????AppWidgetManager.INVALID_APPWIDGET_ID);??
- ????????}??
- ??????????
- ????????@Override??
- ????????public?void?onCreate()?{??
- ????????????for?(int?i?=?0;?i?<?mCount;?i++)?{??
- ????????????????mWidgetItems.add("item:"?+?i?+?"!");??
- ????????????}??
- ????????}??
- ??
- ????????@Override??
- ????????public?void?onDataSetChanged()?{??
- ????????????/*?
- ?????????????*?appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds,?R.id.listview);?
- ?????????????*?使用該通知更新數(shù)據(jù)源灶挟,會(huì)調(diào)用onDataSetChanged?
- ?????????????*/??
- ????????????System.out.println("----onDataSetChanged----");??
- ????????}??
- ??
- ????????@Override??
- ????????public?void?onDestroy()?{??
- ????????????mWidgetItems.clear();??
- ????????}??
- ??
- ????????@Override??
- ????????public?int?getCount()?{??
- ????????????return??mCount;??
- ????????}??
- ??
- ????????@Override??
- ????????public?RemoteViews?getViewAt(int?position)?{??
- ????????????RemoteViews?views?=?new?RemoteViews(mContext.getPackageName(),?android.R.layout.simple_list_item_1);??
- ????????????views.setTextViewText(android.R.id.text1,?"item:"?+?position);??
- ????????????System.out.println("RemoteViewsService----getViewAt"?+?position);??
- ??????????????
- ??????????????
- ????????????Bundle?extras?=?new?Bundle();??
- ????????????extras.putInt(WidgetSetProvider.EXTRA_ITEM,?position);??
- ????????????Intent?fillInIntent?=?new?Intent();??
- ????????????fillInIntent.putExtras(extras);??
- ????????????/*?
- ?????????????*?android.R.layout.simple_list_item_1?---?id?---?text1?
- ?????????????*?listview的item?click:將fillInIntent發(fā)送琉朽,?
- ?????????????*?fillInIntent它默認(rèn)的就有action?是provider中使用?setPendingIntentTemplate?設(shè)置的action?
- ?????????????*/??
- ????????????views.setOnClickFillInIntent(android.R.id.text1,?fillInIntent);??
- ??????????????
- ????????????return?views;??
- ????????}??
- ??
- ????????@Override??
- ????????public?RemoteViews?getLoadingView()?{??
- ????????????/*?在更新界面的時(shí)候如果耗時(shí)就會(huì)顯示?正在加載...?的默認(rèn)字樣,但是你可以更改這個(gè)界面?
- ?????????????*?如果返回null?顯示默認(rèn)界面?
- ?????????????*?否則?加載自定義的稚铣,返回RemoteViews?
- ?????????????*/??
- ????????????return?null;??
- ????????}??
- ??
- ????????@Override??
- ????????public?int?getViewTypeCount()?{??
- ????????????return?1;??
- ????????}??
- ??
- ????????@Override??
- ????????public?long?getItemId(int?position)?{??
- ????????????return?position;??
- ????????}??
- ??
- ????????@Override??
- ????????public?boolean?hasStableIds()?{??
- ????????????return?false;??
- ????????}??
- ??????????
- ????}??
- ??
- }??
- package?com.stone.service;??
- ??
- import?java.util.ArrayList;??
- import?java.util.List;??
- ??
- import?android.app.PendingIntent;??
- import?android.content.Context;??
- import?android.content.Intent;??
- import?android.os.Bundle;??
- import?android.widget.RemoteViews;??
- import?android.widget.RemoteViewsService;??
- ??
- import?com.stone.R;??
- import?com.stone.receiver.WidgetSetProvider;??
- ??
- /**?
- ?*?繼承自RemoteViewsService?必須重寫onGetViewFactory?
- ?*?該服務(wù)只是用來?創(chuàng)建?集合widget使用的數(shù)據(jù)源?
- ?*?@author?stone?
- ?*/??
- public?class?WidgetSetService?extends?RemoteViewsService?{??
- ??????
- ????public?WidgetSetService()?{??
- ??????????
- ????}??
- ??????
- ????@Override??
- ????public?RemoteViewsFactory?onGetViewFactory(Intent?intent)?{??
- ????????return?new?WidgetFactory(this.getApplicationContext(),?intent);??
- ????}??
- ??????
- ??????
- ????public?class?WidgetFactory?implements?RemoteViewsService.RemoteViewsFactory?{??
- ????????private?static?final?int?mCount?=?10;??
- ????????private?Context?mContext;??
- ????????private?List<String>?mWidgetItems?=?new?ArrayList<String>();??
- ??????????
- ??????????
- ????????public?WidgetFactory(Context?context,?Intent?intent)?{??
- ????????????mContext?=?context;??
- //??????????mAppWidgetId?=?intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,??
- //??????????????????AppWidgetManager.INVALID_APPWIDGET_ID);??
- ????????}??
- ??????????
- ????????@Override??
- ????????public?void?onCreate()?{??
- ????????????for?(int?i?=?0;?i?<?mCount;?i++)?{??
- ????????????????mWidgetItems.add("item:"?+?i?+?"!");??
- ????????????}??
- ????????}??
- ??
- ????????@Override??
- ????????public?void?onDataSetChanged()?{??
- ????????????/*?
- ?????????????*?appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds,?R.id.listview);?
- ?????????????*?使用該通知更新數(shù)據(jù)源箱叁,會(huì)調(diào)用onDataSetChanged?
- ?????????????*/??
- ????????????System.out.println("----onDataSetChanged----");??
- ????????}??
- ??
- ????????@Override??
- ????????public?void?onDestroy()?{??
- ????????????mWidgetItems.clear();??
- ????????}??
- ??
- ????????@Override??
- ????????public?int?getCount()?{??
- ????????????return??mCount;??
- ????????}??
- ??
- ????????@Override??
- ????????public?RemoteViews?getViewAt(int?position)?{??
- ????????????RemoteViews?views?=?new?RemoteViews(mContext.getPackageName(),?android.R.layout.simple_list_item_1);??
- ????????????views.setTextViewText(android.R.id.text1,?"item:"?+?position);??
- ????????????System.out.println("RemoteViewsService----getViewAt"?+?position);??
- ??????????????
- ??????????????
- ????????????Bundle?extras?=?new?Bundle();??
- ????????????extras.putInt(WidgetSetProvider.EXTRA_ITEM,?position);??
- ????????????Intent?fillInIntent?=?new?Intent();??
- ????????????fillInIntent.putExtras(extras);??
- ????????????/*?
- ?????????????*?android.R.layout.simple_list_item_1?---?id?---?text1?
- ?????????????*?listview的item?click:將fillInIntent發(fā)送,?
- ?????????????*?fillInIntent它默認(rèn)的就有action?是provider中使用?setPendingIntentTemplate?設(shè)置的action?
- ?????????????*/??
- ????????????views.setOnClickFillInIntent(android.R.id.text1,?fillInIntent);??
- ??????????????
- ????????????return?views;??
- ????????}??
- ??
- ????????@Override??
- ????????public?RemoteViews?getLoadingView()?{??
- ????????????/*?在更新界面的時(shí)候如果耗時(shí)就會(huì)顯示?正在加載...?的默認(rèn)字樣惕医,但是你可以更改這個(gè)界面?
- ?????????????*?如果返回null?顯示默認(rèn)界面?
- ?????????????*?否則?加載自定義的耕漱,返回RemoteViews?
- ?????????????*/??
- ????????????return?null;??
- ????????}??
- ??
- ????????@Override??
- ????????public?int?getViewTypeCount()?{??
- ????????????return?1;??
- ????????}??
- ??
- ????????@Override??
- ????????public?long?getItemId(int?position)?{??
- ????????????return?position;??
- ????????}??
- ??
- ????????@Override??
- ????????public?boolean?hasStableIds()?{??
- ????????????return?false;??
- ????????}??
- ??????????
- ????}??
- ??
- }??
widgetprovider
- package?com.stone.receiver;??
- ??
- import?com.stone.R;??
- import?com.stone.service.WidgetSetService;??
- ??
- 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.os.Bundle;??
- import?android.text.TextUtils;??
- import?android.text.format.DateUtils;??
- import?android.view.View;??
- import?android.widget.RemoteViews;??
- import?android.widget.Toast;??
- import?android.widget.ViewFlipper;??
- ??
- /**?
- ?*?使用了集合展示AppWidget?
- ?*?ListView、GridView抬伺、StackView?設(shè)置adapter螟够,處理item點(diǎn)擊?
- ?*?ViewFlipper?在RemoteViews中缺少支持,暫只能在它的布局文件中設(shè)置?輪換效果?
- ?*??????????????對(duì)于切換到哪一個(gè)子view的item事件不好處理沛简,只能設(shè)置一個(gè)整體setPendingIntent?
- ?*?@author?stone?
- ?*/??
- public?class?WidgetSetProvider?extends?AppWidgetProvider?{??
- ????public?final?static?String?CLICK_ACTION?=?"com.stone.action.clickset";??
- ????public?final?static?String?CLICK_ITEM_ACTION?=?"com.stone.action.clickset.item";??
- ????public?final?static?String?EXTRA_ITEM?=?"extra_item";??
- ??????
- ????@Override??
- ????public?void?onReceive(Context?context,?Intent?intent)?{??
- ????????super.onReceive(context,?intent);??
- ????????System.out.println(intent.getAction());??
- ????????if?(TextUtils.equals(CLICK_ACTION,?intent.getAction()))?{??
- ????????????int?extraType?=?intent.getIntExtra("view_tag",?0);??
- ????????????if?(extraType?>?0)?{??
- ????????????????System.out.println("extra:::"?+?extraType);??
- ??????????????????
- ????????????????switch?(extraType)?{??
- ????????????????case?1:??
- ????????????????????updateWidget(context,?R.id.listview,?R.id.gridview,?R.id.stackview,?R.id.viewflipper);??
- ????????????????????break;??
- ????????????????case?2:??
- ????????????????????updateWidget(context,?R.id.gridview,?R.id.listview,?R.id.stackview,?R.id.viewflipper);??
- ????????????????????break;??
- ????????????????case?3:??
- ????????????????????updateWidget(context,?R.id.stackview,?R.id.gridview,?R.id.listview,?R.id.viewflipper);??
- ????????????????????break;??
- ????????????????case?4:??
- ????????????????????updateWidget(context,?R.id.viewflipper,?R.id.gridview,?R.id.stackview,?R.id.listview);??
- ????????????????????break;??
- ??????
- ????????????????default:??
- ????????????????????break;??
- ????????????????}??
- ????????????}???
- ????????}?else?if?(TextUtils.equals(CLICK_ITEM_ACTION,?intent.getAction()))?{??
- ????????????Bundle?extras?=?intent.getExtras();??
- ????????????int?position?=?extras.getInt(WidgetSetProvider.EXTRA_ITEM,?-1);??
- ????????????if?(position?!=?-1)?{??
- ????????????????System.out.println("--點(diǎn)擊了item---"?+?position);??
- ????????????????System.out.println("");??
- //??????????????Toast.makeText(context,?"click?item:"?+?position,?0).show();??
- ????????????}??
- ????????}??
- ????}??
- ??
- ????@Override??
- ????public?void?onUpdate(Context?context,?AppWidgetManager?appWidgetManager,?int[]?appWidgetIds)?{??
- ????????RemoteViews?views?=?new?RemoteViews(context.getPackageName(),?R.layout.collections_view_widget);??
- ??????????
- ????????Intent?intent1?=?new?Intent(CLICK_ACTION);??
- ????????intent1.putExtra("view_tag",?1);??
- ????????PendingIntent?pendingIntent1?=?PendingIntent.getBroadcast(context,?101,?intent1,?0);??
- ????????views.setOnClickPendingIntent(R.id.btn_listview,?pendingIntent1);??
- ??????????
- ????????Intent?intent2?=?new?Intent(CLICK_ACTION);??
- ????????intent2.putExtra("view_tag",?2);??
- ????????PendingIntent?pendingIntent2?=?PendingIntent.getBroadcast(context,?102,?intent2,?0);??
- ????????views.setOnClickPendingIntent(R.id.btn_gridview,?pendingIntent2);??
- ??????????
- ????????Intent?intent3?=?new?Intent(CLICK_ACTION);??
- ????????intent3.putExtra("view_tag",?3);??
- ????????PendingIntent?pendingIntent3?=?PendingIntent.getBroadcast(context,?103,?intent3,?0);??
- ????????views.setOnClickPendingIntent(R.id.btn_stackview,?pendingIntent3);??
- ??????????
- ????????Intent?intent4?=?new?Intent(CLICK_ACTION);??
- ????????intent4.putExtra("view_tag",?4);??
- ????????PendingIntent?pendingIntent4?=?PendingIntent.getBroadcast(context,?104,?intent4,?0);??
- ????????views.setOnClickPendingIntent(R.id.btn_viewflipper,?pendingIntent4);??
- ??????????
- ????????appWidgetManager.updateAppWidget(appWidgetIds,?views);??
- ??????????
- ????????System.out.println("setwidget?update");??
- ????????super.onUpdate(context,?appWidgetManager,?appWidgetIds);??
- ????}??
- ??
- ????@Override??
- ????public?void?onAppWidgetOptionsChanged(Context?context,???
- ????????????AppWidgetManager?appWidgetManager,?int?appWidgetId,??
- ????????????Bundle?newOptions)?{??
- ????????super.onAppWidgetOptionsChanged(context,?appWidgetManager,?appWidgetId,??
- ????????????????newOptions);??
- ????}??
- ??
- ????@Override??
- ????public?void?onDeleted(Context?context,?int[]?appWidgetIds)?{??
- ????????super.onDeleted(context,?appWidgetIds);??
- ????}??
- ??
- ????@Override??
- ????public?void?onEnabled(Context?context)?{??
- ????????super.onEnabled(context);??
- ????}??
- ??
- ????@Override??
- ????public?void?onDisabled(Context?context)?{??
- ????????super.onDisabled(context);??
- ????}??
- ??????
- ????private?void?updateWidget(Context?context,?int?visible,?int?gone1,?int?gone2,?int?gone3)?{??
- ????????//RemoteViews處理異進(jìn)程中的View??
- ????????RemoteViews?views?=?new?RemoteViews(context.getPackageName(),?R.layout.collections_view_widget);??
- ??????????
- ????????views.setViewVisibility(visible,?View.VISIBLE);??
- ????????views.setViewVisibility(gone1,?View.GONE);??
- ????????views.setViewVisibility(gone2,?View.GONE);??
- ????????views.setViewVisibility(gone3,?View.GONE);??
- ??????????
- ????????if?(visible?!=?R.id.viewflipper)?{//viewflipper?不是?繼承自AbsListView??or??AdapterViewAnimator??的view??
- ????????????Intent?intent?=?new?Intent(context,?WidgetSetService.class);??
- ????????????views.setRemoteAdapter(visible,?intent);//設(shè)置集合的adapter為intent指定的service??
- ????????????views.setEmptyView(visible,?R.id.tv_empty);//指定集合view為空時(shí)顯示的view??
- ??????????????
- ????????????Intent?toIntent?=?new?Intent(CLICK_ITEM_ACTION);??
- ????????????PendingIntent?pendingIntent?=?PendingIntent.getBroadcast(context,?200,?toIntent,?PendingIntent.FLAG_UPDATE_CURRENT);??
- ????????????/*?
- ?????????????*?setPendingIntentTemplate?設(shè)置pendingIntent?模板?
- ?????????????*?setOnClickFillInIntent???可以將fillInIntent?添加到pendingIntent中?
- ?????????????*/??
- ????????????views.setPendingIntentTemplate(visible,?pendingIntent);??
- ??????????????
- ????????}?else?if?(visible?==?R.id.viewflipper)?{??
- //??????????views.setPendingIntentTemplate(R.id.viewflipper,?pendingIntentTemplate);??
- ????????}??
- ??????????
- ????????AppWidgetManager?am?=?AppWidgetManager.getInstance(context);??
- ????????int[]?appWidgetIds?=?am.getAppWidgetIds(new?ComponentName(context,?WidgetSetProvider.class));??
- ????????for?(int?i?=?0;?i?<?appWidgetIds.length;?i++)?{??
- ????????????am.updateAppWidget(appWidgetIds[i],?views);?//更新?實(shí)例??
- ????????}??
- ??????????
- ????}??
- ??????
- }??
- package?com.stone.receiver;??
- ??
- import?com.stone.R;??
- import?com.stone.service.WidgetSetService;??
- ??
- 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.os.Bundle;??
- import?android.text.TextUtils;??
- import?android.text.format.DateUtils;??
- import?android.view.View;??
- import?android.widget.RemoteViews;??
- import?android.widget.Toast;??
- import?android.widget.ViewFlipper;??
- ??
- /**?
- ?*?使用了集合展示AppWidget?
- ?*?ListView齐鲤、GridView、StackView?設(shè)置adapter椒楣,處理item點(diǎn)擊?
- ?*?ViewFlipper?在RemoteViews中缺少支持给郊,暫只能在它的布局文件中設(shè)置?輪換效果?
- ?*??????????????對(duì)于切換到哪一個(gè)子view的item事件不好處理,只能設(shè)置一個(gè)整體setPendingIntent?
- ?*?@author?stone?
- ?*/??
- public?class?WidgetSetProvider?extends?AppWidgetProvider?{??
- ????public?final?static?String?CLICK_ACTION?=?"com.stone.action.clickset";??
- ????public?final?static?String?CLICK_ITEM_ACTION?=?"com.stone.action.clickset.item";??
- ????public?final?static?String?EXTRA_ITEM?=?"extra_item";??
- ??????
- ????@Override??
- ????public?void?onReceive(Context?context,?Intent?intent)?{??
- ????????super.onReceive(context,?intent);??
- ????????System.out.println(intent.getAction());??
- ????????if?(TextUtils.equals(CLICK_ACTION,?intent.getAction()))?{??
- ????????????int?extraType?=?intent.getIntExtra("view_tag",?0);??
- ????????????if?(extraType?>?0)?{??
- ????????????????System.out.println("extra:::"?+?extraType);??
- ??????????????????
- ????????????????switch?(extraType)?{??
- ????????????????case?1:??
- ????????????????????updateWidget(context,?R.id.listview,?R.id.gridview,?R.id.stackview,?R.id.viewflipper);??
- ????????????????????break;??
- ????????????????case?2:??
- ????????????????????updateWidget(context,?R.id.gridview,?R.id.listview,?R.id.stackview,?R.id.viewflipper);??
- ????????????????????break;??
- ????????????????case?3:??
- ????????????????????updateWidget(context,?R.id.stackview,?R.id.gridview,?R.id.listview,?R.id.viewflipper);??
- ????????????????????break;??
- ????????????????case?4:??
- ????????????????????updateWidget(context,?R.id.viewflipper,?R.id.gridview,?R.id.stackview,?R.id.listview);??
- ????????????????????break;??
- ??????
- ????????????????default:??
- ????????????????????break;??
- ????????????????}??
- ????????????}???
- ????????}?else?if?(TextUtils.equals(CLICK_ITEM_ACTION,?intent.getAction()))?{??
- ????????????Bundle?extras?=?intent.getExtras();??
- ????????????int?position?=?extras.getInt(WidgetSetProvider.EXTRA_ITEM,?-1);??
- ????????????if?(position?!=?-1)?{??
- ????????????????System.out.println("--點(diǎn)擊了item---"?+?position);??
- ????????????????System.out.println("");??
- //??????????????Toast.makeText(context,?"click?item:"?+?position,?0).show();??
- ????????????}??
- ????????}??
- ????}??
- ??
- ????@Override??
- ????public?void?onUpdate(Context?context,?AppWidgetManager?appWidgetManager,?int[]?appWidgetIds)?{??
- ????????RemoteViews?views?=?new?RemoteViews(context.getPackageName(),?R.layout.collections_view_widget);??
- ??????????
- ????????Intent?intent1?=?new?Intent(CLICK_ACTION);??
- ????????intent1.putExtra("view_tag",?1);??
- ????????PendingIntent?pendingIntent1?=?PendingIntent.getBroadcast(context,?101,?intent1,?0);??
- ????????views.setOnClickPendingIntent(R.id.btn_listview,?pendingIntent1);??
- ??????????
- ????????Intent?intent2?=?new?Intent(CLICK_ACTION);??
- ????????intent2.putExtra("view_tag",?2);??
- ????????PendingIntent?pendingIntent2?=?PendingIntent.getBroadcast(context,?102,?intent2,?0);??
- ????????views.setOnClickPendingIntent(R.id.btn_gridview,?pendingIntent2);??
- ??????????
- ????????Intent?intent3?=?new?Intent(CLICK_ACTION);??
- ????????intent3.putExtra("view_tag",?3);??
- ????????PendingIntent?pendingIntent3?=?PendingIntent.getBroadcast(context,?103,?intent3,?0);??
- ????????views.setOnClickPendingIntent(R.id.btn_stackview,?pendingIntent3);??
- ??????????
- ????????Intent?intent4?=?new?Intent(CLICK_ACTION);??
- ????????intent4.putExtra("view_tag",?4);??
- ????????PendingIntent?pendingIntent4?=?PendingIntent.getBroadcast(context,?104,?intent4,?0);??
- ????????views.setOnClickPendingIntent(R.id.btn_viewflipper,?pendingIntent4);??
- ??????????
- ????????appWidgetManager.updateAppWidget(appWidgetIds,?views);??
- ??????????
- ????????System.out.println("setwidget?update");??
- ????????super.onUpdate(context,?appWidgetManager,?appWidgetIds);??
- ????}??
- ??
- ????@Override??
- ????public?void?onAppWidgetOptionsChanged(Context?context,???
- ????????????AppWidgetManager?appWidgetManager,?int?appWidgetId,??
- ????????????Bundle?newOptions)?{??
- ????????super.onAppWidgetOptionsChanged(context,?appWidgetManager,?appWidgetId,??
- ????????????????newOptions);??
- ????}??
- ??
- ????@Override??
- ????public?void?onDeleted(Context?context,?int[]?appWidgetIds)?{??
- ????????super.onDeleted(context,?appWidgetIds);??
- ????}??
- ??
- ????@Override??
- ????public?void?onEnabled(Context?context)?{??
- ????????super.onEnabled(context);??
- ????}??
- ??
- ????@Override??
- ????public?void?onDisabled(Context?context)?{??
- ????????super.onDisabled(context);??
- ????}??
- ??????
- ????private?void?updateWidget(Context?context,?int?visible,?int?gone1,?int?gone2,?int?gone3)?{??
- ????????//RemoteViews處理異進(jìn)程中的View??
- ????????RemoteViews?views?=?new?RemoteViews(context.getPackageName(),?R.layout.collections_view_widget);??
- ??????????
- ????????views.setViewVisibility(visible,?View.VISIBLE);??
- ????????views.setViewVisibility(gone1,?View.GONE);??
- ????????views.setViewVisibility(gone2,?View.GONE);??
- ????????views.setViewVisibility(gone3,?View.GONE);??
- ??????????
- ????????if?(visible?!=?R.id.viewflipper)?{//viewflipper?不是?繼承自AbsListView??or??AdapterViewAnimator??的view??
- ????????????Intent?intent?=?new?Intent(context,?WidgetSetService.class);??
- ????????????views.setRemoteAdapter(visible,?intent);//設(shè)置集合的adapter為intent指定的service??
- ????????????views.setEmptyView(visible,?R.id.tv_empty);//指定集合view為空時(shí)顯示的view??
- ??????????????
- ????????????Intent?toIntent?=?new?Intent(CLICK_ITEM_ACTION);??
- ????????????PendingIntent?pendingIntent?=?PendingIntent.getBroadcast(context,?200,?toIntent,?PendingIntent.FLAG_UPDATE_CURRENT);??
- ????????????/*?
- ?????????????*?setPendingIntentTemplate?設(shè)置pendingIntent?模板?
- ?????????????*?setOnClickFillInIntent???可以將fillInIntent?添加到pendingIntent中?
- ?????????????*/??
- ????????????views.setPendingIntentTemplate(visible,?pendingIntent);??
- ??????????????
- ????????}?else?if?(visible?==?R.id.viewflipper)?{??
- //??????????views.setPendingIntentTemplate(R.id.viewflipper,?pendingIntentTemplate);??
- ????????}??
- ??????????
- ????????AppWidgetManager?am?=?AppWidgetManager.getInstance(context);??
- ????????int[]?appWidgetIds?=?am.getAppWidgetIds(new?ComponentName(context,?WidgetSetProvider.class));??
- ????????for?(int?i?=?0;?i?<?appWidgetIds.length;?i++)?{??
- ????????????am.updateAppWidget(appWidgetIds[i],?views);?//更新?實(shí)例??
- ????????}??
- ??????????
- ????}??
- ??????
- }??
運(yùn)行的周期函數(shù)
點(diǎn)擊stackview的效果圖