如何使Android Widget StackView安排其显示顺序

时间:2018-10-13 13:48:34

标签: android

我有一个Android Widget,它使用StackView来显示数据列表。我希望能够先让StackView显示特定列表(不一定是列表的排列方式),然后再滑动到其他列表。

我有两个使StackView成为可能的类; AppWidgetProvider类和StackWidgetServices类,但是我不知道如何实现此目的。请给我一些指导。 StackWidgetServices类充当适配器。

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

class StackRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
private final ImageDownloader imageDownloader = new ImageDownloader();
private List<Buzz> mBuzzes = new ArrayList<Buzz>();
private Context mContext;
private int mAppWidgetId;

public StackRemoteViewsFactory(Context context, Intent intent) {
    mContext = context;
    mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
}

public void onCreate() {
}

public void onDestroy() {
    mBuzzes.clear();
}

public int getCount() {
    return mBuzzes.size();
}

public RemoteViews getViewAt(int position) {
    RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.stackwidget_item);

    if (position <= getCount()) {
        Buzz buzz = mBuzzes.get(position);

        if (buzz.picture != null) {
            try {
                Bitmap picture = imageDownloader.downloadBitmap(buzz.picture, 100, 100, 70);
                rv.setImageViewBitmap(R.id.stackWidgetItemPicture, picture);
            }
            catch(Exception e) {
                Logging.e("Error reading picture file", e);
            }
        }

        if (!buzz.username.isEmpty()) {
            rv.setTextViewText(R.id.stackWidgetItemUsername, buzz.username);
        }
        rv.setTextViewText(R.id.stackWidgetItemContent, Html.fromHtml(buzz.content));

        // store the buzz ID in the extras so the main activity can use it
        Bundle extras = new Bundle();
        extras.putString(HoneybuzzListActivity.EXTRA_ID, buzz.id);
        Intent fillInIntent = new Intent();
        fillInIntent.putExtras(extras);
        rv.setOnClickFillInIntent(R.id.stackWidgetItem, fillInIntent);
    }

    return rv;
}

public RemoteViews getLoadingView() {
    return null;
}

public int getViewTypeCount() {
    return 1;
}

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

public boolean hasStableIds() {
    return true;
}

public void onDataSetChanged() {
    mBuzzes = Buzz.getBuzzes(HoneybuzzApplication.buzz, mContext);
}
}

1 个答案:

答案 0 :(得分:1)

我按照@pskink的建议修改了getViewAt();

public RemoteViews getViewAt(int position) {
    RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.stackwidget_item);

ArrayList<Integer> list = new ArrayList<>();
list.add(2);
list.add(1);
list.add(0);
list.add(3);

if (position <= getCount()) {
// this is where the issue get solved.
    Buzz buzz = mBuzzes.get(list.get(position));

    if (buzz.picture != null) {
        try {
            Bitmap picture = imageDownloader.downloadBitmap(buzz.picture, 100, 100, 70);
            rv.setImageViewBitmap(R.id.stackWidgetItemPicture, picture);
        }
        catch(Exception e) {
            Logging.e("Error reading picture file", e);
        }
    }

    if (!buzz.username.isEmpty()) {
        rv.setTextViewText(R.id.stackWidgetItemUsername, buzz.username);
    }
    rv.setTextViewText(R.id.stackWidgetItemContent, Html.fromHtml(buzz.content));

    // store the buzz ID in the extras so the main activity can use it
    Bundle extras = new Bundle();
    extras.putString(HoneybuzzListActivity.EXTRA_ID, buzz.id);
    Intent fillInIntent = new Intent();
    fillInIntent.putExtras(extras);
    rv.setOnClickFillInIntent(R.id.stackWidgetItem, fillInIntent);
}

return rv;
}