AppWidget AdapterViewFlipper无法处理项目点击

时间:2015-07-07 13:44:33

标签: android android-widget

我使用AdapterViewFlipper创建了简单的appwidget。显示数据和翻转操作正常工作,但如果我想处理项目点击操作,这不起作用。但是,如果我在xml布局中更改ListView上的AdapterViewFlipper,则操作单击项可以正常工作。

带有小部件提供程序的类

public class KidChildrenAdaperViewFlipperWidget extends AppWidgetProvider {

public static final String WIDGET_ACTION = "WIDGET_ACTION";
public static final String ACTION_NEXT = ".ACTION_NEXT";
public static final String ACTION_PREVIOUS = "ACTION_PREVIOUS";
private static final String ACTION = "ACTION";

public static final String TAG = "KidChildrenListWidget";

@Override
public void onReceive(Context context, Intent intent) {

    String action = intent.getAction();
    Bundle extras = intent.getExtras();
    String actionButton = (String) (extras == null ? null : extras.get(ACTION));

    if (action.equals(KidChildrenAdaperViewFlipperWidget.WIDGET_ACTION)){
        Toast.makeText(context, "Kliknięto: ", Toast.LENGTH_SHORT).show();
    }

    if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action) && actionButton != null) {
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_view_flipper_children);
        switch (actionButton) {
            case ACTION_NEXT: {
                remoteViews.showNext(R.id.advKidWidgetChildren);
            }
            break;

            case ACTION_PREVIOUS: {
                remoteViews.showPrevious(R.id.advKidWidgetChildren);
            }
            break;
        }

        int widgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, 0);
        AppWidgetManager.getInstance(context).updateAppWidget(widgetId, remoteViews);
    }else {
        super.onReceive(context, intent);
    }
}

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

    for (int i = 0; i < appWidgetIds.length; i++) {

        Intent widgetIntent = new Intent(context, WidgetChildAdapterViewFlipperService.class);
        widgetIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
        widgetIntent.setData(Uri.parse(widgetIntent.toUri(Intent.URI_INTENT_SCHEME)));

        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_view_flipper_children);

        remoteViews.setRemoteAdapter(R.id.advKidWidgetChildren, widgetIntent);

        remoteViews.setEmptyView(R.id.advKidWidgetChildren, R.id.tvKidWidgetEmptyView);


        Intent activityIntent = new Intent(context, KidChildrenAdaperViewFlipperWidget.class);
        // Set the action for the intent.
        // When the user touches a particular view, it will have the effect of
        // broadcasting WIDGET_ACTION.
        activityIntent.setAction(WIDGET_ACTION);
        activityIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
        widgetIntent.setData(Uri.parse(widgetIntent.toUri(Intent.URI_INTENT_SCHEME)));
        PendingIntent toastPendingIntent = PendingIntent.getBroadcast(context, 0, activityIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setPendingIntentTemplate(R.id.advKidWidgetChildren, toastPendingIntent);



        Intent previousIntent = new Intent(context, KidChildrenAdaperViewFlipperWidget.class);
        previousIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        previousIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
        previousIntent.putExtra(ACTION, ACTION_PREVIOUS);
        PendingIntent previousPendingIntent = PendingIntent.getBroadcast(context, 1, previousIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.ivWVFCPrevious, previousPendingIntent);


        Intent nextIntent = new Intent(context, KidChildrenAdaperViewFlipperWidget.class);
        nextIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        nextIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
        nextIntent.putExtra(ACTION, ACTION_NEXT);
        PendingIntent nextPendingIntent = PendingIntent.getBroadcast(context, 2, nextIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.ivWVFCNext, nextPendingIntent);

        appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);

    }

    super.onUpdate(context, appWidgetManager, appWidgetIds);
    Log.d(TAG, "Zakutalizowano widget");
}

}

使用小部件服务的类

public class WidgetChildAdapterViewFlipperService extends RemoteViewsService {

@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
    return new WidgetFactoryADV(this.getApplicationContext(), intent);
}

public class WidgetFactoryADV implements RemoteViewsFactory {


    private Context context;
    private int appWidgetId;

    private KidDatabaseManager kidDatabaseManager;
    private List<ChildObject> childObjectList;

    public WidgetFactoryADV(Context context, Intent intent ) {
        this.context = context;
        appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);

    }

    @Override
    public void onCreate() {

        kidDatabaseManager = new KidDatabaseManager(context);
        childObjectList = kidDatabaseManager.getChildren(((ApplicationController) context.getApplicationContext()).getUser(context).getId());
    }

    @Override
    public void onDataSetChanged() {
        childObjectList = kidDatabaseManager.getChildren(((ApplicationController) context.getApplicationContext()).getUser(context).getId());

        for (int i=0; i<childObjectList.size(); i++){
            getViewAt(i);
        }
    }

    @Override
    public void onDestroy() {
    }

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

    @Override
    public RemoteViews getViewAt(int i) {

        ChildObject childObject = childObjectList.get(i);

        RemoteViews widgetRow = new RemoteViews(context.getPackageName(), R.layout.widget_children_adv_item);

        widgetRow.setTextViewText(R.id.tvWidgetItemChildName, childObject.getName());

        String sdf = new SimpleDateFormat("yy.MM.dd HH:mm:ss").format(new Date(childObject.getPositionCreatedAt() * 1000));
        widgetRow.setTextViewText(R.id.tvWidgetItemChildPositionTime, Html.fromHtml(String.format(getString(R.string.last_position),
                sdf)));

        Address address = Utils.getAddress(context, childObject.getLatitude(), childObject.getLongitude());
        if (address != null) {
            String thoroughfare = address.getThoroughfare() == null ? "" : address.getThoroughfare();
            String localiyty = address.getLocality() == null ? "" : (", " + address.getLocality());
            String featureName = address.getFeatureName() == null ? "" : (" " + address.getFeatureName());


            widgetRow.setTextViewText(R.id.tvWidgetItemAddress, thoroughfare + featureName + localiyty);
        }

        widgetRow.setTextViewText(R.id.tvWidgetItemAccuracy, ChildUtil.getAccuracyIndicator(context, childObject.getAccuracy()));

        ChildUtil.setActivityIconForRemoteView(context, widgetRow, childObject.getActivity());


        widgetRow.setTextViewText(R.id.tvWidgetItemBattery, childObject.getBattery() + " %");

        Intent widgetIntent = new Intent();
        Bundle widgetBundle = new Bundle();

        widgetBundle.putInt("index", i);
        widgetIntent.putExtras(widgetBundle);

        widgetRow.setOnClickFillInIntent(R.id.rlWidgetADVItem, widgetIntent);


        return widgetRow;
    }

    @Override
    public RemoteViews getLoadingView() {

        return null;
    }

    @Override
    public int getViewTypeCount() {
        return childObjectList.size();
    }

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

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

}

我花了很多时间尝试解决这个问题,但我不知道,为什么这不起作用

感谢您提出任何可以帮助我解决此问题的建议

0 个答案:

没有答案