带有ListView自定义项的Android小部件

时间:2017-01-24 09:21:14

标签: android android-layout listview android-widget

我想为我的应用创建一个Widget,它显示从网络加载的几行数据。

我有一个小部件布局,其中包含一个列表视图,一个AppWidgetProvider,一个RemoteViewsService和一个RemoteViewsFactory。该服务已在清单中注册。

问题是我无法使用自定义的ListItem布局。 public RemoteViews getViewAt(int position)中唯一有效的方法是在许多基本教程中使用android.R.layout.simple_list_item_1。但是当我尝试使用自己的布局时,我只为每个条目(默认加载视图)获得一个“正在加载...”条目 这是我想要使用的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:padding="10dp"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="@dimen/cardview_spacing"
            android:layout_marginEnd="@dimen/cardview_spacing"
            android:layout_gravity="center_horizontal"
            android:textSize="@dimen/textTitle"
            android:id="@+id/plan_entry_lesson" />

        <TextView
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="@dimen/cardview_spacing"
            android:layout_marginEnd="@dimen/cardview_spacing"
            android:layout_gravity="center_horizontal"
            android:textSize="@dimen/textTitle"
            android:id="@+id/plan_entry_class" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_gravity="start"
            android:textSize="@dimen/textSmallTitle"
            android:id="@+id/plan_header"
            android:text="@string/plan_header"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/cardview_spacing"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_gravity="start"
            android:id="@+id/plan"
            android:layout_marginLeft="@dimen/cardview_spacing" />


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_gravity="start"
            android:textSize="@dimen/textSmallTitle"
            android:id="@+id/subst_header"
            android:text="@string/subst_header"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/cardview_spacing"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_gravity="start"
            android:id="@+id/subst"
            android:layout_marginLeft="@dimen/cardview_spacing" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_gravity="start"
            android:textSize="@dimen/textSmallTitle"
            android:id="@+id/comment_header"
            android:text="@string/comment_header"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/cardview_spacing"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_gravity="start"
            android:id="@+id/comment"
            android:layout_marginLeft="@dimen/cardview_spacing" />

    </LinearLayout>
</LinearLayout>

由于数据是异步加载的,我建议数据根本不存在或类似,但我的调试输出显示数据可用且我创建的视图不为空。但是,我只能使用android示例列表项。

对此有何建议?

我的getViewTypeCount()此刻返回1!(不是0,这似乎是一个常见问题)

完整性:RemoteViewsFactory:

public class WidgetDataProvider implements    RemoteViewsService.RemoteViewsFactory {
    private static final String TAG = "WidgetDataProvider";
    private Vplan plan;

    private List<VplanEntry> mCollection = new ArrayList<>();
    private Context mContext = null;

    public WidgetDataProvider(Context context, Intent intent) {
        mContext = context;
        plan = new Vplan(mContext, "today");
    }

    @Override
    public void onCreate() {
        initData();
    }

    @Override
    public void onDataSetChanged() {
        initData();
    }

    @Override
    public void onDestroy() {

    }

    @Override
    public int getCount() {
        return mCollection.size();
    }

    @Override
    public RemoteViews getViewAt(int position) {
        Log.d("WIDGET", "getViewAt "  + position);
        VplanEntry entry = mCollection.get(position);
        Log.d("WIDGET", "entry plan: " + entry.getPlan());

        RemoteViews view = new RemoteViews(mContext.getPackageName(),
                android.R.layout.simple_list_item_1);
        String text = entry.getCls() + " " + entry.getLesson() + ":\n"
                + entry.getPlan() +"\n"
                + entry.getSubstitution() + "\n"
                + entry.getComment();
        view.setTextViewText(android.R.id.text1, text);
        //view.setTextViewText(R.id.plan_entry_lesson, entry.getLesson());

        return view;
    }

    @Override
    public RemoteViews getLoadingView() {
        //return new RemoteViews(mContext.getPackageName(),
        //       R.layout.vplan_appwidget_loading);
        return null;
    }

    @Override
    public int getViewTypeCount() {
        return 1;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    private void initData() {
        mCollection.clear();
        if (plan.isLoaded() && plan.upToDate()) {
            mCollection.clear();
            Log.d("WIDGET", "Data present");
            for (VplanEntry entry : plan.getEntries()) {
                mCollection.add(entry);
            }
            Log.d("WIDGET", "Entries: " + mCollection.size());
            AppWidgetManager awm = AppWidgetManager.getInstance(mContext);
            awm.notifyAppWidgetViewDataChanged(awm.getAppWidgetIds(new ComponentName(mContext, WidgetService.class)), R.id.widgetListView);
        } else {
            Log.d("WIDGET", "Loading started");
            plan.load(new SuccessCallback() {
                @Override
                public void callback(boolean success) {
                    Log.d("WIDGET", "Loading finished: " + success);
                    if (success) {
                        initData();
                    }
                }
            });
        }

    }
}

1 个答案:

答案 0 :(得分:0)

问题是我的布局宽度高于最低要求的Widget宽度......

更改宽度解决了问题。