I can't load images retrieved from Firebase into a viewpager

扶醉桌前 提交于 2021-01-29 05:34:27

问题


I have uploaded some images using an android application into Firebase storage, now I want to be able to display those images inside a ViewPager in that same application. I have followed a tutorial where they did the same thing but in a RecycleView, and tried to apply the same thing in my code. Now there seems to be no error when I run my code, but I can't view the images inside the imageView. I need help with my ViewPagerAdapter code please, something is missing on the instantiate method and I don't know what it is.

Here's my activity code:

public class menuSliderActivity extends AppCompatActivity {

    private ViewPager viewPager;
    private TextView textView;
    private DatabaseReference mDatabaseRef;
    List<SliderUtils> sliderImg;
    ViewPagerAdapter viewPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu_slider);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        textView = (TextView) findViewById(R.id.textView);
        sliderImg = new ArrayList<>();
        mDatabaseRef= FirebaseDatabase.getInstance().getReference("Images");
        mDatabaseRef.addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange( DataSnapshot dataSnapshot) {
            for(DataSnapshot postSnapshot : dataSnapshot.getChildren()){
                SliderUtils sliderUtils = postSnapshot.getValue(SliderUtils.class);
                sliderImg.add(sliderUtils);
            }

            viewPagerAdapter = new ViewPagerAdapter(sliderImg, menuSliderActivity.this);
            viewPager.setAdapter(viewPagerAdapter);
       }

       @Override
       public void onCancelled( DatabaseError databaseError) {
           Toast.makeText(menuSliderActivity.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();

       }
   });



    Timer timer = new Timer();
    timer.scheduleAtFixedRate(new MyTimerTask(),2000,2000);}

public class MyTimerTask extends TimerTask{
    @Override
    public void run() {
        menuSliderActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if(viewPager.getCurrentItem()==0){
                    viewPager.setCurrentItem(1);
                } else if (viewPager.getCurrentItem()==1){
                    viewPager.setCurrentItem(2);
                } else {
                    viewPager.setCurrentItem(0);
                }
            }
        });
    }


}

}

And here is my ViewPager's adapter code which I need help with :

public class ViewPagerAdapter extends PagerAdapter {

    private Context context;
    private LayoutInflater layoutInflater;
    private List<SliderUtils> sliderImg;
    private ImageLoader imageLoader;
    private DatabaseReference mDatabaseRef;
    private ViewPager viewPager;
    private ImageView imageView;

    public ViewPagerAdapter(List<SliderUtils> sliderImg, Context context){

        this.sliderImg = sliderImg;
        this.context = context;
}


    @Override
    public int getCount() {
        return sliderImg.size();
}

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

    @Override
    public Object instantiateItem( ViewGroup container, int position) {
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.custom_layout, null);
        SliderUtils utils = sliderImg.get(position);
        ImageView imageView =  view.findViewById(R.id.imageView);
        //ADDED THIS HERE.                  
        Glide.with(context).load(utils.getImageUrl()).into(imageView);
        container.addView(view);
        return view;
}

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

}

I still can't get the images to load in my viewpager, I get a working url I made sure of that, but the images aren't disapled, I can only see the background of the viewpager which I have set.


回答1:


You can use Glide to load the image. For now, you are not loading the image into image view that's why it's not working here is the code...

  @Override
public Object instantiateItem( ViewGroup container, int position) {
    layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = layoutInflater.inflate(R.layout.custom_layout, container, false);
    SliderUtils utils = sliderImg.get(position);
    ImageView imageView =  view.findViewById(R.id.imageView);

    // FIXED USING THIS
    Glide.with(context).load(**utils.getImageUrl**).into(imageView);
    container.addView(view);
    return view;
     }

EDIT

Also, you need to add the dependencies into your gradle file

dependencies {
  implementation 'com.github.bumptech.glide:glide:4.9.0'
  annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
}


来源:https://stackoverflow.com/questions/56769999/i-cant-load-images-retrieved-from-firebase-into-a-viewpager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!