1

我浏览了以下 android 文档:“App Widgets with Collections”来创建一个显示文本列表的小部件。一切正常,它就像我想要的那样工作。

现在,我不想显示文本列表,而是显示存储在 sd 卡中的图像列表。那么如何在我实现的代码中做到这一点。如果有人对此有一些想法,请帮我解决这个问题。

我写的代码:

WidgetProvider.java

public class WidgetProvider extends AppWidgetProvider 
{
    public static String EXTRA_WORD=
            "com.commonsware.android.appwidget.lorem.WORD";

    @Override
    public void onUpdate(Context ctxt, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) 
    {
        for (int i=0; i<appWidgetIds.length; i++) 
        {
            Intent svcIntent=new Intent(ctxt, WidgetService.class);

            svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
            svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));

            RemoteViews widget=new RemoteViews(ctxt.getPackageName(),
                    R.layout.widget);

            widget.setRemoteAdapter(appWidgetIds[i], R.id.words,
                    svcIntent);

            Intent clickIntent=new Intent(ctxt, LoremActivity.class);
            PendingIntent clickPI=PendingIntent
                    .getActivity(ctxt, 0,
                            clickIntent,
                            PendingIntent.FLAG_UPDATE_CURRENT);

            widget.setPendingIntentTemplate(R.id.words, clickPI);

            appWidgetManager.updateAppWidget(appWidgetIds[i], widget);
        }

        super.onUpdate(ctxt, appWidgetManager, appWidgetIds);
    }
}

小部件服务.java

public class WidgetService extends RemoteViewsService 
{
    @Override
    public RemoteViewsFactory onGetViewFactory(Intent intent)
    {
        return(new WidgetDisplay(this.getApplicationContext(),
                intent));
    }
}

小部件显示.java

public class WidgetDisplay implements RemoteViewsService.RemoteViewsFactory 
{
    private static final String[] items={"Red", "Green", "Blue", "Pink", "Orange"};
    private Context ctxt=null;
    private int appWidgetId;

    public WidgetDisplay(Context ctxt, Intent intent) 
    {
        this.ctxt=ctxt;
        appWidgetId=intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);
    }

    @Override
    public void onCreate() 
    {
        // no-op
    }

    @Override
    public void onDestroy() 
    {
        // no-op
    }

    @Override
    public int getCount() 
    {
        return(items.length);
    }

    @Override
    public RemoteViews getViewAt(int position) 
    {
        RemoteViews row=new RemoteViews(ctxt.getPackageName(),
                R.layout.row);

        row.setTextViewText(android.R.id.text1, items[position]);

        Intent i=new Intent();
        Bundle extras=new Bundle();

        extras.putString(WidgetProvider.EXTRA_WORD, items[position]);
        i.putExtras(extras);
        row.setOnClickFillInIntent(android.R.id.text1, i);

        return(row);
    }

    @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);
    }

    @Override
    public void onDataSetChanged() 
    {
        // no-op
    }
}

XML 文件

小部件.xml

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/words"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_marginTop="3dp"
  android:layout_marginLeft="3dp"
  android:background="@drawable/widget_frame"
/>

行.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="center_vertical"
    android:paddingLeft="6dip"
    android:minHeight="?android:attr/listPreferredItemHeight"   
/>

Widget_provider.xml

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
  android:minWidth="146dip"
  android:minHeight="146dip"
  android:updatePeriodMillis="0"
  android:initialLayout="@layout/widget"
  android:autoAdvanceViewId="@+id/words"
  android:previewImage="@drawable/preview"
  android:resizeMode="vertical"
/>

我得到的小部件的屏幕截图

在此处输入图像描述

4

1 回答 1

5

我习惯了这种方式并且工作得很好。

在row.xml 中将textview 更改为 imageview

public WidgetDisplay(Context ctxt, Intent intent) 
    {
        this.ctxt=ctxt;
        appWidgetId=intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);
                setImageinView(this.ctxt);
    }

private void setImageinView(Context context) {
//read sd card and store bitmap in  arrayListBitmap;
}

@Override
public RemoteViews getViewAt(int position) {
remoteView = new RemoteViews(ctxt.getPackageName(), R.layout.row);
    remoteView.setImageViewBitmap(R.id.image_photo1,arrayListBitmap.get(i));
}

很简单,您必须将 sd 卡存储数据读取到 arraylist。

将大小设置为getCount()

现在在您的图像视图中设置图像。

如果有任何疑问,那么欢迎。

于 2013-03-20T13:38:27.727 回答