如何使用Glide在ViewPager中加载多个图像?

时间:2017-10-26 17:19:39

标签: android android-viewpager picasso android-glide

首先,我是Android开发的初学者,真的希望你们可以帮助我。我想将Firebase中的3张图片加载到我的viewpager中,该图片可以滑动。

我设法加载2个图像并将其显示在viewpager上,但是当我尝试使用Glide和Picasso设置第三个图像时,应用程序崩溃并且当我使用时它声明“参数不能为空”当我使用毕加索时,滑行,并且“目标不能为空”。我的图像大小各约为60kb。当我放置一个标签时,它显示已经从Firebase下载了所有图片,但不知何故,第三张图片无法在viewpager上显示并使应用程序崩溃。

以下是我的ViewPagerAdapter。非常感谢你提前。

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import com.bumptech.glide.Glide;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.ValueEventListener;

public class ViewPagerAdapter extends PagerAdapter {

private Context context;
private LayoutInflater layoutInflater;
public static final String TAG_FRAGMENT = "FirebaseImageViewer";
private static final String TAG = "FirebaseImageViewer";
private DatabaseReference bannerReference;
private ImageView[] images = new ImageView[3];

public ViewPagerAdapter(Context context, DatabaseReference bannerReference) 
{
    this.context = context;
    this.bannerReference = bannerReference;

    bannerReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String image =(String)dataSnapshot.child("banner1").getValue();
            String image2 =(String)dataSnapshot.child("banner2").getValue();             
            String image3 =(String)dataSnapshot.child("banner3").getValue();



                       Glide.with(ViewPagerAdapter.this.context)
                       .asBitmap()
                       .load("image").into(images[0]); 

                       Glide.with(ViewPagerAdapter.this.context)
                      .asBitmap()
                      .load("image2")
                      .into(images[1]);  

                       Glide.with(ViewPagerAdapter.this.context)
                      .asBitmap()
                      .load("image3")
                      .into(images[2]);

                                         }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });

}


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

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

@Override
public Object instantiateItem(final ViewGroup container, final int position) {



    images[position] = new ImageView(ViewPagerAdapter.this.context);
    int padding = ViewPagerAdapter.this
            .context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
    images[position].setPadding(padding, padding, padding, padding);

    images[position].setScaleType(ImageView.ScaleType.FIT_XY);


    container.addView(images[position], position);
    return images[position];
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView((ImageView) object);


}

下面是错误日志,

D/FirebaseImageViewer: image loaded 
https://firebasestorage.googleapis.com/v0/b/kahwin2-
6ca62.appspot.com/o/Banner_Contents%2Fkahwinmadeeasy.png?
alt=media&token=df1e45d9-c34b-45a5-b26f-fe4bbfffa7e4

D/FirebaseImageViewer: image loaded 
https://firebasestorage.googleapis.com/v0/b/kahwin2-
6ca62.appspot.com/o/Banner_Contents%2Fbanner2.png?alt=media&token=fa9757fa-
e734-4151-bb52-486d499c192d

D/FirebaseImageViewer: image loaded 
https://firebasestorage.googleapis.com/v0/b/kahwin2-
6ca62.appspot.com/o/Banner_Contents%2Fbanner3.png?alt=media&token=014ee8c4-
a600-4325-9cc5-87d8535805c9

W/Settings: Setting airplane_mode_on has moved from 
android.provider.Settings.System to android.provider.Settings.Global, 
returning read-only value.
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.something.android.kahwinkahwin, PID: 6051
              java.lang.IllegalArgumentException: Target must not be null.
                  at 
com.squareup.picasso.RequestCreator.into(RequestCreator.java:618)
                  at 
com.squareup.picasso.RequestCreator.into(RequestCreator.java:601)
                  at com.something.android.kahwinkahwin.ViewPagerAdapter$1.
onDataChange(ViewPagerAdapter.java:52)
                  at com.google.android.gms.internal.
zzbpx.zza(Unknown Source)
                  at com.google.android.gms.internal.zzbqx.zzZV(Unknown 
Source)
                  at com.google.android.gms.internal.zzbra$1.run(Unknown 
Source)
                  at android.os.Handler.handleCallback(Handler.java:751)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.
main(ActivityThread.java:6334)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.
run(ZygoteInit.java:886)at com.android.internal.os.ZygoteInit.
main(ZygoteInit.java:776)

2 个答案:

答案 0 :(得分:0)

确定问题如下从firebase下载图像并将其保存在imageimage2image3中,但是当您使用滑动加载图像时,您将传递给banner 1}}字符串而不是图像变量,所以正确的解决方案将如下所示

               Glide.with(ViewPagerAdapter.this.context)
               .asBitmap()
               .load(image).into(images[0]); 

               Glide.with(ViewPagerAdapter.this.context)
              .asBitmap()
              .load(image2)
              .into(images[1]);  

               Glide.with(ViewPagerAdapter.this.context)
              .asBitmap()
              .load(image3)
              .into(images[2]);

答案 1 :(得分:0)

我设法通过使用MidasLefko的建议来设置setOffScreenPageLimit(3)来解决问题。它默认为1.