无法在AppWidget中的GridView中更新ProgressBar

时间:2014-07-18 06:54:30

标签: android gridview android-appwidget android-remoteview

我正在开发AppWidget for Android。我在AppWidget上有一个GridView,在GridView的每个项目中都有一个ProgressBarr。这个ProgressBar应该直播更新(因为我希望看到它从左到右进行)。不幸的是我的代码"一个线程"正在做但不准确。当我向下滚动或向上滚动时,我可以看到变化,否则一切就像停在小部件上一样。我试图在它正在工作的线程中更新小部件。我可以看到现场进展但我在这里遇到了另一个问题。问题是每个项目都在整个屏幕上展开并逐个迭代并显示ProgressBar上的Progress。我在代码中评论过我已更新小部件并遇到问题。请帮忙吗?我会感激你的。

这是我在主屏幕上拖动我的小部件然后我可以看到我的GridView的屏幕截图..

enter image description here

然后导致updateWidget调用我的小部件看起来像这样。它正在更新进度条,但我不想这样。它应该在progressBarr中更新,如上所示。它不是在那些gridview项目中进行,而是在窗口小部件上展开项目,然后开始迭代窗口小部件上的项目并继续更新。 enter image description here enter image description here

这是两个不同的项目。

我的代码是。

public class ListProvider implements RemoteViewsFactory {
private ArrayList<ListItem> listItemList = new ArrayList<ListItem>();
private Context context = null;
private int appWidgetId;
private int mProgressStatus = 0;
AppWidgetManager appWidgetManager ;
private Handler mHandler = new Handler();


public ListProvider(Context context, Intent intent,
        ArrayList<ListItem> listItems) {
    this.context = context;

    appWidgetManager = AppWidgetManager.getInstance(context);
    appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
            AppWidgetManager.INVALID_APPWIDGET_ID);
    if (listItems != null)
        this.listItemList = (ArrayList<ListItem>) listItems.clone();

}

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

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

/*
 * Similar to getView of Adapter where instead of View return RemoteViews
 * And using the saved ImagePath to show images on ImageView
 */
@Override
public RemoteViews getViewAt(final int position) {
    final RemoteViews remoteView = new RemoteViews(
            context.getPackageName(), R.layout.list_row);
    final ListItem listItem = listItemList.get(position);
    remoteView.setTextViewText(R.id.heading, (listItem.heading).toUpperCase());

    remoteView.setViewVisibility(R.id.ProgressTime, View.VISIBLE);
    remoteView.setProgressBar(R.id.ProgressTime, 100, mProgressStatus, false);


    Thread worker = new Thread(new Runnable() {

        @Override
        public void run() {
            mProgressStatus=0;
            while(mProgressStatus<100){
                mProgressStatus = mProgressStatus+3;
                try {
                    Thread.sleep(2011);
                } catch (InterruptedException e) {

                    e.printStackTrace();
                }
                mHandler.post(new Runnable() {
                    public void run() {
                        remoteView.setProgressBar(R.id.ProgressTime, 100, mProgressStatus, false);

                    }
                });

                /////  I have tried doing this ... this solution did work for me but 
                //    this  is expanding one item on whole widget and showing only 
                //    one item on AppWidget instead of showing all GridView
                //    i know this is not gona work properly for me . i Need something else
                ///  tell me from where i should do this call so i can see all gridview at same time 
                //   and can see live update.

                appWidgetManager.updateAppWidget(appWidgetId, remoteView);
            }



        }
    });
    worker.start();


    Bitmap bitmap = BitmapFactory.decodeFile(listItem.fileUrl);
    if (bitmap != null)
        remoteView.setImageViewBitmap(R.id.imageView, bitmap);

    else
        remoteView.setImageViewResource(R.id.imageView, R.drawable.ic_launcher);
    return remoteView;
}
@Override
public RemoteViews getLoadingView() {
    return null;
}

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

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

@Override
public void onCreate() {

}

@Override
public void onDataSetChanged() {
}

@Override
public void onDestroy() {
}

}

0 个答案:

没有答案