Android Widget 小組件(四---完結) 使用ListView、GridView、StackView、ViewFlipper展示Widget

來源:互聯網
上載者:User

Android Widget 小組件(四---完結) 使用ListView、GridView、StackView、ViewFlipper展示Widget

官方有話這樣說:

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.不支援這些類的後代

      接下來的樣本說明怎麼樣實現 使用ListView、GridView、StackView、ViewFlipper建立AppWidget

      menifest

                                                                                                

      set_widget_provider.xml


      widget使用的布局檔案 collections_view_widget.xml

                                                  <frameLayout        xmlns:android="http://schemas.android.com/apk/res/android"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:background="#80000000" >        />                                                                                                    </frameLayout>

      集合的資料來源 需要 繼承 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 * 該服務只是用來 建立 集合widget使用的資料來源 * @author stone */public class WidgetSetService extends RemoteViewsService {public WidgetSetService() {}@Overridepublic 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 mWidgetItems = new ArrayList();public WidgetFactory(Context context, Intent intent) {        mContext = context;//        mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,//                AppWidgetManager.INVALID_APPWIDGET_ID);    }@Overridepublic void onCreate() {for (int i = 0; i < mCount; i++) {                mWidgetItems.add("item:" + i + "!");            }}@Overridepublic void onDataSetChanged() {/* * appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.listview); * 使用該通知更新資料來源,會調用onDataSetChanged */System.out.println("----onDataSetChanged----");}@Overridepublic void onDestroy() {mWidgetItems.clear();}@Overridepublic int getCount() {return  mCount;}@Overridepublic 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發送,             * fillInIntent它預設的就有action 是provider中使用 setPendingIntentTemplate 設定的action             */views.setOnClickFillInIntent(android.R.id.text1, fillInIntent);return views;}@Overridepublic RemoteViews getLoadingView() {/* 在更新介面的時候如果耗時就會顯示 正在載入... 的預設字樣,但是你可以更改這個介面 * 如果返回null 顯示預設介面 * 否則 載入自訂的,返回RemoteViews */return null;}@Overridepublic int getViewTypeCount() {return 1;}@Overridepublic long getItemId(int position) {return position;}@Overridepublic 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 設定adapter,處理item點擊 * ViewFlipper 在RemoteViews中缺少支援,暫只能在它的布局檔案中設定 輪換效果 * 對於切換到哪一個子view的item事件不好處理,只能設定一個整體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";@Overridepublic 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("--點擊了item---" + position);System.out.println("");//Toast.makeText(context, "click item:" + position, 0).show();}}}@Overridepublic 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);}@Overridepublic void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId,Bundle newOptions) {super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId,newOptions);}@Overridepublic void onDeleted(Context context, int[] appWidgetIds) {super.onDeleted(context, appWidgetIds);}@Overridepublic void onEnabled(Context context) {super.onEnabled(context);}@Overridepublic void onDisabled(Context context) {super.onDisabled(context);}private void updateWidget(Context context, int visible, int gone1, int gone2, int gone3) {//RemoteViews處理異進程中的ViewRemoteViews 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  的viewIntent intent = new Intent(context, WidgetSetService.class);views.setRemoteAdapter(visible, intent);//設定集合的adapter為intent指定的serviceviews.setEmptyView(visible, R.id.tv_empty);//指定集合view為空白時顯示的viewIntent toIntent = new Intent(CLICK_ITEM_ACTION);PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 200, toIntent, PendingIntent.FLAG_UPDATE_CURRENT);/* * setPendingIntentTemplate 設定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); //更新 執行個體}}}

      啟動並執行周期函數

      點擊stackview的




相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.