Auto flipping view flipper in remote view app widget

谁都会走 提交于 2020-07-10 08:15:56

问题


I have created a view flipper for my app widget that is contains 20 PNGs flipping one after another by tap. But i want to change that to auto flipping. After 1st tap i want to flip all the images automatically and stop in 1 st image again. But i am having hard time to figuring out auto flipping in remote view. My code as folows,

AppWidget.cs

[MetaData("android.appwidget.provider", Resource = "@xml/appwidgetprovider")]
public class AppWidget : AppWidgetProvider
{
    private static String ACTION_CLICK = "com.example.action.CLICK";
    private static String PREF_DISPLAYED_CHILD = "displayed_child_";

    private static int START_NO = 0;
    private static int LAST_NO = 20;
    bool click_event = false;
    
    public override void OnUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
    foreach (int appWidgetId in appWidgetIds) {
            update(context, appWidgetManager, appWidgetId, click_event);
    }
}


    public override void onDeleted(Context context, int[] appWidgetIds) {
        ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(context);
        ISharedPreferencesEditoe editor = prefs.Edit();

        foreach (int appWidgetId in appWidgetIds) {
            editor.remove(PREF_DISPLAYED_CHILD + appWidgetId);
        }

        editor.apply();
    }

    static void update(Context context, AppWidgetManager appWidgetManager, int appWidgetId, bool click_event) {
        String idKey = PREF_DISPLAYED_CHILD + appWidgetId;

        ISharedPreferences sp = PreferenceManager.GetDefaultSharedPreferences(context);
        int displayedChild = sp.getInt(idKey, START_NO);

        RemoteViews rv = new RemoteViews(context.PackageName, Resource.layout.balance_widget);
        Intent clickIntent = new Intent(context, typeof(Appwidget));
        clickIntent.setAction(ACTION_CLICK);
        clickIntent.putExtra(AppWidgetManager.ExtraAppWidgetId, appWidgetId);
        
        PendingIntent clickPending = PendingIntent.getBroadcast(context,appWidgetId,clickIntent,0);
        rv.setOnClickPendingIntent(Resource.id.viewflipper1, clickPending);
        rv.setDisplayedChild(Resource.id.viewflipper1, displayedChild);

        appWidgetManager.updateAppWidget(appWidgetId, rv);

        int newChild = displayedChild +1;
        
        if(displayedChild == START_NO && click_event == false){
            newChild = START_NO;
        } else if(displayedChild  == LAST_NO){
            newChild = START_NO;
        }
        sp.edit().putInt(idKey, newChild).apply();
    }
    
    public override void onReceive(Context context, Intent intent) {
        if (ACTION_CLICK.equals(intent.Action) {
            int appWidgetId = intent.getIntExtra(AppWidgetManager.ExtraAppwidgetId, AppWidgetManager.InvalidAppwidgetId);
            update(context,AppWidgetManager.GetInstance(context),appWidgetId,click_event);
        }
        else {
            click_event =false;
            base.onReceive(context, intent);
        }
    }
}
}

Please help me on this.

来源:https://stackoverflow.com/questions/62748537/auto-flipping-view-flipper-in-remote-view-app-widget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!