How do I Add OnScrollListener to Image gallery demo?

75 views Asked by At

I have found some image gallery demo on below link: http://www.androidauthority.com/how-to-build-an-image-gallery-app-718976/

But it's loading all the images together. How do I add OnScrollListeners to this project?

1

There are 1 answers

3
Abhishek kumar On

You have to do code on your Adapter OnBindViewHolder() method

This is your Code :

  @Override
public void onBindViewHolder(MyAdapter.ViewHolder viewHolder, int i) {
    viewHolder.title.setText(galleryList.get(i).getImage_title());
    viewHolder.img.setScaleType(ImageView.ScaleType.CENTER_CROP);
    viewHolder.img.setImageResource((galleryList.get(i).getImage_ID()));        
}

In the above code onBindViewHolder(MyAdapter.ViewHolder viewHolder, int i) here int i is the position of the image by getting position you can perform action within OnBindViewHolder.

  @Override
public void onBindViewHolder(MyAdapter.ViewHolder viewHolder, int i) {
    viewHolder.title.setText(galleryList.get(i).getImage_title());
    viewHolder.img.setScaleType(ImageView.ScaleType.CENTER_CROP);
    viewHolder.img.setImageResource((galleryList.get(i).getImage_ID()));    
    Log.e("POSITION",""+i); //This will show you position on LOG.
    Toast.makeText(getApplicationContext()," "+i,Toast.LENGTH_SHORT).show();

    //Example : Hiding second position image  
    if(i==2){
       viewHolder.img.setVisibility(View.GONE);
            }

}

Note : In RecyclerView OnBindViewHolder is calling in every row ,So what you have to do just code inside of the OnBindViewHolder.Its not setting images in a single time Recyclerview getting images by calling OnbindViewHolder for every row and setting the images.