ViewPager 显示最后的图像,直到没有加载新图像
ViewPager shows the last images until the new images is not loading
我的 problem:At 第一次打开项目屏幕时,所有图像都将加载到 viewpager 之后,当我打开新项目屏幕时,它会显示旧图像,直到新图像未加载。
所以我想在打开新项目时清除 viewpager 上的旧图像。
如何在打开新项目屏幕时清除最后的图像?
我写了下面的代码来在 viewpager 中显示图像:
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) {
}
}
}
我的适配器class:
class ViewSlidingImageAdapter extends FragmentStatePagerAdapter {
public ViewSlidingImageAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return imageUlrList.size();
}
@Override
public Fragment getItem(int position) {
return ViewSlidingImage.newInstance(position, product_url);
}
}
ViewSlidingImage:
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.imageUlrList
。这样你就可以确保一旦你调用 productimages
asynctask,SingleProductscreen.imageUlrList
是空的,因此不会有任何图像加载,它将是空白的。
protected void onPreExecute() {
super.onPreExecute();
SingleProductscreen.imageUlrList.clear();
mViewSlidingImageAdapter = new ViewSlidingImageAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mViewSlidingImageAdapter);
mViewSlidingImageAdapter.notifyDataSetChanged();
}
我的 problem:At 第一次打开项目屏幕时,所有图像都将加载到 viewpager 之后,当我打开新项目屏幕时,它会显示旧图像,直到新图像未加载。
所以我想在打开新项目时清除 viewpager 上的旧图像。
如何在打开新项目屏幕时清除最后的图像?
我写了下面的代码来在 viewpager 中显示图像:
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) {
}
}
}
我的适配器class:
class ViewSlidingImageAdapter extends FragmentStatePagerAdapter {
public ViewSlidingImageAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return imageUlrList.size();
}
@Override
public Fragment getItem(int position) {
return ViewSlidingImage.newInstance(position, product_url);
}
}
ViewSlidingImage:
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.imageUlrList
。这样你就可以确保一旦你调用 productimages
asynctask,SingleProductscreen.imageUlrList
是空的,因此不会有任何图像加载,它将是空白的。
protected void onPreExecute() {
super.onPreExecute();
SingleProductscreen.imageUlrList.clear();
mViewSlidingImageAdapter = new ViewSlidingImageAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mViewSlidingImageAdapter);
mViewSlidingImageAdapter.notifyDataSetChanged();
}