我正在为 Android 开发 AppWidget。我在 AppWidget 上有一个 GridView,在 GridView 的每个项目中我都有一个 ProgressBarr。这个 ProgressBar 应该实时更新(因为我想看到它从左到右进行)。不幸的是,我的代码“线程”正在这样做,但并不准确。当我向下滚动或向上滚动时,我可以看到变化,否则一切就像在小部件上停止一样。我试图在它正在工作的线程中更新小部件。我可以看到实时进度,但我在这里遇到了另一个问题。问题是每个项目都在整个屏幕上展开并逐个迭代并在 ProgressBar 上显示进度。我在代码中评论了我更新小部件并遇到问题的地方。请任何帮助?我会感谢你的。
这是我在主屏幕上拖动小部件时的屏幕截图,然后我可以看到我的 GridView..
然后那个 updateWidget 调用的原因是我的小部件看起来像这样。它正在更新进度条,但我不想这样。如上所示,它应该在 progressBarr 中更新。而不是在那些 gridview 项目中做它正在扩展小部件上的项目然后开始迭代小部件上的项目并继续更新。
这是两个不同的项目。
我的代码是。
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() {
}
}