Android ViewPager显示最后的图像,直到新图像未加载
我的问题:当我第一次打开项目屏幕时,所有图像都将加载到viewpager中,然后当我打开新项目屏幕时,它将显示旧图像,直到新图像未加载为止 因此,我想在打开新项目时清除viewpager上的旧图像 打开“新建项目”屏幕时,如何清除最后的图像 我为在viewpager中显示图像编写了以下代码:Android ViewPager显示最后的图像,直到新图像未加载,android,android-fragments,android-viewpager,android-view,Android,Android Fragments,Android Viewpager,Android View,我的问题:当我第一次打开项目屏幕时,所有图像都将加载到viewpager中,然后当我打开新项目屏幕时,它将显示旧图像,直到新图像未加载为止 因此,我想在打开新项目时清除viewpager上的旧图像 打开“新建项目”屏幕时,如何清除最后的图像 我为在viewpager中显示图像编写了以下代码: class productimages extends AsyncTask<String, String, String> { @Override pro
class productimages extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... arg) {
imageUlrList.clear();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id", arg[0]));
JSONObject json = jParser.makeHttpRequest(url, "GET",
params);
try {
int success;
success = json.getInt(TAG_IMAGESUCCESS);
if (success == 1) {
JSONArray productObj = json.getJSONArray(TAG_PIMAGES);
for (int index = 0; index < productObj.length(); index++) {
JSONObject product = productObj.getJSONObject(index);
imageurl = product.getString(TAG_IMAGEURL);
imageUlrList.add(imageurl);
}
} else {
// product with pid not found
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
try {
mViewPager.setAdapter(null);
mViewSlidingImageAdapter = new ViewSlidingImageAdapter(
getSupportFragmentManager());
mViewPager.setAdapter(mViewSlidingImageAdapter);
mViewSlidingImageAdapter.notifyDataSetChanged();
} catch (Exception e) {
}
}
}
查看幻灯片图像:
public class ViewSlidingImage extends Fragment {
private String postionOfArray = "";
static ViewSlidingImage newInstance(int imageNum, String imagePathName) {
final ViewSlidingImage f = new ViewSlidingImage();
final Bundle args = new Bundle();
args.putString("postionOfArray", "" + imageNum);
f.setArguments(args);
return f;
}
public ViewSlidingImage() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
postionOfArray = getArguments().getString("postionOfArray");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.view_sliding_image, container,
false);
ImageView mImageView = (ImageView) v
.findViewById(R.id.tutorial_imageview);
try {
String imagePath = SingleProductscreen.imageUlrList.get(Integer
.parseInt(postionOfArray));
UrlImageViewHelper.setUrlDrawable(mImageView, imagePath);
} catch (Exception e) {
// TODO: handle exception
}
return v;
}
}
ViewSlidingImage使用imageUrlList中的内容加载ImageView,如下所示:
String imagePath = SingleProductscreen.imageUlrList.get(Integer.parseInt(postionOfArray));
因此,您只需清除onPreExecute中的SingleProductscreen.ImageUrlList。通过这种方式,您可以确保调用productimages异步任务后,SingleProductscreen.ImageUrlList为空,因此不会有任何要加载的图像,它将为空
protected void onPreExecute() {
super.onPreExecute();
SingleProductscreen.imageUlrList.clear();
mViewSlidingImageAdapter = new ViewSlidingImageAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mViewSlidingImageAdapter);
mViewSlidingImageAdapter.notifyDataSetChanged();
}
谢谢你的工作。但我有一个问题是,我想在图像未完全加载时显示正在加载的图像。为此,我编写了“urlmageviewHelper.setUrlDrawablemImageView,imagePath,R.drawable.loadingsymbol;”但这个符号是可见的。有什么解决办法吗?有时显示加载图像,有时不显示图像。有什么解决方案吗?有一个带有可绘图加载符号的ImageView。在onPreExecute集中,它的可见性为true。在onPostExecute集中,它的可见性为false。这样,加载符号将一直显示,直到图像完全加载。但适配器类中已存在Imageview。正如您在上面的代码中所看到的,我不是指适配器,而是指view\u slideing\u image.xml。imageView应该位于您的ViewPager上方。这是我能想到的最简单的方法。
protected void onPreExecute() {
super.onPreExecute();
SingleProductscreen.imageUlrList.clear();
mViewSlidingImageAdapter = new ViewSlidingImageAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mViewSlidingImageAdapter);
mViewSlidingImageAdapter.notifyDataSetChanged();
}