显示图像列表时应用程序窗口小部件问题

时间:2013-03-20 12:36:21

标签: java android android-widget

我通过以下android文档:“使用集合的应用程序小部件”来创建一个显示文本列表的小部件。一切正常,它的工作就像我希望它一样。

现在,我想要显示存储在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);
    }
}

WidgetService.java

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

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

Widget.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"
/>

Row.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"
/>

我获得的小部件的ScreenShots

enter image description here

1 个答案:

答案 0 :(得分: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()

现在在您的imageview中设置图片。

如果有任何查询,那么欢迎。