gpt4 book ai didi

android - ViewPager 使用了错误的条目

转载 作者:行者123 更新时间:2023-11-30 03:55:50 27 4
gpt4 key购买 nike

我有以下类(class)来展示一些图像......我已经实现了将图像下载到 SD 卡的功能。如果我通过它们,一切都很好(显示图像 + 标题),但如果我从某个图像开始下载,使用另一个 url(另一个图像的),所以我以某种方式认为我的 ViewPager 没有正确更新或其他东西。

这是我的课:

public class ImagePagerActivity extends BaseActivity {

private ViewPager pager;

LinearLayout buttonBar;
TextView txtTitle;

String urlOfImageToDownload;

public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
private ProgressDialog mProgressDialog;

private static DisplayImageOptions options;

ImageView imageView;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.ac_image_pager);
Bundle bundle = getIntent().getExtras();
String[] imageUrls = bundle.getStringArray(Extra.IMAGES);
String[] imageTitles = bundle.getStringArray(Extra.TITLES);
int pagerPosition = bundle.getInt(Extra.IMAGE_POSITION, 0);

options = new DisplayImageOptions.Builder()
.cacheOnDisc().showImageForEmptyUri(R.drawable.no_image)
.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
.build();

pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(new ImagePagerAdapter(imageUrls, imageTitles));
pager.setCurrentItem(pagerPosition);
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (imageView.getDrawable() == null)
menu.getItem(0).setEnabled(false);
return true;
}

public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 0, 0,
getString(R.string.save_image)).setIcon(android.R.drawable.ic_menu_save);
return true;
}

public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 0:
Random randomGenerator = new Random();
int randomInt = 0;
for (int idx = 1; idx <= 10; ++idx) {
randomInt = randomGenerator.nextInt(100000);
}

imageView.setDrawingCacheEnabled(true);
Bitmap b = imageView.getDrawingCache();
try {
b.compress(CompressFormat.JPEG, 100,
new
FileOutputStream(Environment.getExternalStorageDirectory()
.getPath() + "/DCIM/image" + randomInt + ".jpg"));
Crouton.makeText(ImagePagerActivity.this,
"Bild erfolgreich gespeichert",
Style.INFO)
.show();
} catch (FileNotFoundException e) {
Crouton.makeText(ImagePagerActivity.this,
"Fehler beim speichern von Datei",
Style.ALERT)
.show();
}

startDownload();

return true;
}
return false;
}

private void startDownload() {
String url = urlOfImageToDownload;
Log.e(MainActivity.LOG_TAG, "url=" + urlOfImageToDownload);
new DownloadFileAsync().execute(url);
}

@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_DOWNLOAD_PROGRESS:
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage(getString(R.string.downloading_image));
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(false);
mProgressDialog.show();
return mProgressDialog;
default:
return null;
}
}

@Override
protected void onStop() {
imageLoader.stop();
super.onStop();
}

private class ImagePagerAdapter extends PagerAdapter {

private String[] images;
private String[] titles;
private LayoutInflater inflater;

ImagePagerAdapter(String[] images, String[] imageTitles) {
this.images = images;
this.titles = imageTitles;
inflater = getLayoutInflater();
}

@Override
public void destroyItem(View container, int position, Object object) {
((ViewPager) container).removeView((View) object);
}

@Override
public void finishUpdate(View container) {
}

@Override
public int getCount() {
return images.length;
}

@Override
public Object instantiateItem(View view, int position) {
final FrameLayout imageLayout = (FrameLayout) inflater.inflate(
R.layout.item_pager_image, null);
imageView = (ImageView) imageLayout
.findViewById(R.id.image);
final ProgressBar spinner = (ProgressBar) imageLayout
.findViewById(R.id.loading);
txtTitle = (TextView) imageLayout.findViewById(R.id.txtTitle);

urlOfImageToDownload = images[position];

buttonBar = (LinearLayout) imageLayout.findViewById(R.id.buttonBar);

txtTitle.setText(titles[position]);

imageLoader.displayImage(images[position], imageView, options,
new ImageLoadingListener() {
@Override
public void onLoadingStarted() {
spinner.setVisibility(View.VISIBLE);
}

@Override
public void onLoadingFailed(FailReason failReason) {
switch (failReason) {
case IO_ERROR:
break;
case OUT_OF_MEMORY:
break;
case UNKNOWN:
break;
}

spinner.setVisibility(View.GONE);
imageView.setImageResource(android.R.drawable.ic_delete);
}

@Override
public void onLoadingComplete(Bitmap bm) {
spinner.setVisibility(View.GONE);
}

@Override
public void onLoadingCancelled() {
spinner.setVisibility(View.GONE);
}
});

((ViewPager) view).addView(imageLayout, 0);
return imageLayout;
}

@Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}

@Override
public void restoreState(Parcelable state, ClassLoader loader) {
}

@Override
public Parcelable saveState() {
return null;
}

@Override
public void startUpdate(View container) {
}

}

class DownloadFileAsync extends AsyncTask<String, String, String> {

@SuppressWarnings("deprecation")
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(DIALOG_DOWNLOAD_PROGRESS);
}

@Override
protected String doInBackground(String... aurl) {
int count;

try {

URL url = new URL(aurl[0]);
URLConnection conexion = url.openConnection();
conexion.connect();

int lenghtOfFile = conexion.getContentLength();

Random rand = new Random();
int randomNumber = rand.nextInt(100000);

InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(
"sdcard/nature_" + randomNumber + ".jpg");

byte data[] = new byte[1024];

long total = 0;

while ((count = input.read(data)) != -1) {
total += count;
publishProgress("" + (int) ((total * 100) / lenghtOfFile));
output.write(data, 0, count);
}

output.flush();
output.close();
input.close();
} catch (Exception e) {
}
return null;

}

protected void onProgressUpdate(String... progress) {
mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}

@Override
protected void onPostExecute(String unused) {
dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
Crouton.makeText(ImagePagerActivity.this,
getString(R.string.image_saved),
Style.CONFIRM)
.show();
}
}
}

感谢您的帮助!

最佳答案

问题:

用户第一次在 ViewPager 中查看图像时,将调用该图像的 instantiateItem(..) 并将字段 urlOfImagetoDownload 设置为正确的 URL:

urlOfImageToDownload = images[position];

但是,如果用户现在返回他之前看到的已经实例化的图像,instantiateItem(...)不会被调用并且urlOfImageToDownload 将包含错误的 URL(上一张图片的)。

解决方法:

您可以使用 ViewPager#getCurrentItem() 检索当前图像的索引,然后将该索引与 images[] 一起使用以获得正确的 URL 当用户点击下载时。

关于android - ViewPager 使用了错误的条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13379616/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com