Volley ImageRequest不会总是用ViewPagerAdapter中的图像替换进度微调器

时间:2017-05-31 04:50:02

标签: android android-viewpager android-volley android-progressbar

我希望能够将图像加载到具有齐射和进度微调器的ViewPager中。

我的问题有时是图像显示,有时进度微调器会永远持续下去。通常,如果加载图像需要不到一秒的时间,它会正确加载并显示图像,否则进度微调器会在需要更长时间后继续运行。

这是我在instantiateItem方法中的PagerAdapter代码:

sdbmCode = function(str){
    var hash = 0;
    for (i = 0; i < str.length; i++) {
        char = str.charCodeAt(i);
        hash = char + (hash << 6) + (hash << 16) - hash;
    }
    return Math.abs(hash);
}

var str1 = 'BomB!';
var str2 = 'Bo mB!';
var str3 = '!!$%#^^@';
var str4 = 'test!!$%#^^@';
var str5 = 'test!!$%#^^@!';
var str6 = '"test!!$%#^^@"';

console.log('hash '+str1+'    -->'+sdbmCode(str1));
console.log('hash '+str1+'    -->'+sdbmCode(str1));
console.log('hash '+str2+'   -->'+sdbmCode(str2));
console.log('hash '+str3+'        -->'+sdbmCode(str3));
console.log('hash '+str4+'    -->'+sdbmCode(str4));
console.log('hash '+str5+'    -->'+sdbmCode(str5));
console.log('hash '+str6+'   -->'+sdbmCode(str6));

感谢任何帮助,谢谢。

1 个答案:

答案 0 :(得分:0)

所以,我一段时间都很努力,而且效果非常好!

  1. 我使用Universal Image Loader加载图像。您可以在此处找到:https://github.com/nostra13/Android-Universal-Image-Loader,并将其添加到项目中,您需要将此行添加到Build.Gradle文件中:

    编译'com.nostra13.universalimageloader:universal-image-loader:1.9.5'

  2. 我使用了Fresco的扩展(通过Facebook)ZoomableDraweeView来保存图像,发现缩放仍然没有用,所以我看得更深入。这对于保持加载的照片仍然非常有用,所以我将在此处发布:https://github.com/ibosong/CommentGallery/tree/master/frescozoomablelib/,您可以将它添加到您的Build.Gradle依赖项中以使用它:

    编译'com.github.ibosong.CommentGallery:frescozoomablelib:1.0.1'

  3. 我发现PhotoViewAttacher非常适合放大和缩小照片:https://github.com/chrisbanes/PhotoView,您可以将它添加到Build.Gradle依赖项中以使用它:

    编译'com.commit451:PhotoView:1.2.4'

    您还需要将其添加到Build.Gradle存储库:

    maven {url'https://jitpack.io'}

  4. 我可以确认这对ViewPager非常有效,实现方法如下:

    PhotoPagerActivity:

    private PhotoPagerAdapter mAdapter;
    private ViewPager mPager;
    private Activity activity;
    private DisplayImageOptions options;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.photo_pager_activity);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    
        // Show the Up button in the action bar.
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    
        activity = this;
    
        options = new DisplayImageOptions.Builder()
                .resetViewBeforeLoading(true)
                .imageScaleType(ImageScaleType.EXACTLY)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .considerExifParams(true)
                .displayer(new FadeInBitmapDisplayer(300))
                .build();
    
        Bundle extras = getIntent().getExtras();
        int position = extras.getInt("pos");
        String title = extras.getString("AlbumTitle");
        setTitle(title);
    
        ArrayList<Photo> photos = PhotoAlbumActivity.getPhotos();
        mPager = (ViewPager) findViewById(R.id.photoPager);
        mAdapter = new PhotoPagerAdapter(photos, activity, options);
        mPager.setAdapter(mAdapter);
        mPager.setCurrentItem(position);
    }
    

    PhotoPagerAdapter:

    private ArrayList<Photo> images;
    private View v;
    private LayoutInflater inflater;
    private ZoomableDraweeView mImageView;
    private Activity activity;
    private ImageLoader imageLoader;
    private DisplayImageOptions options;
    
    PhotoPagerAdapter(ArrayList<Photo> images, Activity activity, DisplayImageOptions options) {
        super();
        this.images = images;
        this.activity = activity;
        ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(activity).build();
        ImageLoader.getInstance().init(configuration);
        imageLoader = ImageLoader.getInstance();
        this.options = options;
    }
    
    @Override
    public Object instantiateItem(final ViewGroup container, int position) {
        inflater = LayoutInflater.from(activity);
        v = inflater.inflate(R.layout.photo_pager_layout, container, false);
    
        String url = images.get(position).getPhotoUrl().replaceAll(activity.getString(R.string.flickr_photo_link_fifth), activity.getString(R.string.flickr_photo_link_sixth));
        mImageView = (ZoomableDraweeView) v.findViewById(R.id.photo);
        PhotoViewAttacher pAttacher;
        pAttacher = new PhotoViewAttacher(mImageView);
        pAttacher.update();
    
    
        imageLoader.displayImage(url, mImageView, options, new SimpleImageLoadingListener() {
            @Override
            public void onLoadingStarted(String imageUri, View view) {
    
            }
    
            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                String message = null;
                switch (failReason.getType()) {
                    case IO_ERROR:
                        message = "Input/Output error";
                        break;
                    case DECODING_ERROR:
                        message = "Image can't be downloaded";
                        break;
                    case NETWORK_DENIED:
                        message = "Downloads are denied";
                        break;
                    case OUT_OF_MEMORY:
                        message = "Out Of Memory error";
                        break;
                    case UNKNOWN:
                        message = "Unknown error";
                        break;
                }
    
                Toast.makeText(v.getContext(), message, Toast.LENGTH_SHORT).show();
            }
    
            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
            }
        });
    
        container.addView(v, 0);
        return v;
    }
    

    和photo_pager_layout:

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="1dp">
    
        <ProgressBar
            android:id="@+id/pbHeaderProgress"
            style="@style/SpinnerAsEditText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"/>
    
        <com.bosong.frescozoomablelib.zoomable.ZoomableDraweeView
            android:id="@+id/photo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"
            android:layout_gravity="center"/>
    
    </FrameLayout>
    

    活动的布局只包含一个viewpager和其他一些不重要的东西。希望这有助于某人!