I have learning Image Slider Using ViewPager. For that, I want the images to be dynamic. Glide is easy but I think I am not using it properly. Here's what I am trying to do.
//Initialize by default Images
private static final Integer[] IMAGES = {R.drawable.image1,R.drawable.image2};
// Load images into Drawables using Glide
Glide.with(this)
.load("https://google.com/someImage1.jpg")
.error(R.drawable.image1)
.into(R.drawable.image1);
Glide.with(this)
.load("https://google.com/someImage2.jpg")
.error(R.drawable.image2)
.into(R.drawable.image2);
// Set Again
IMAGES[0]= R.drawable.image1;
IMAGES[1]= R.drawable.image2;
But .into(java.lang.Integer
) is not defined and I know that. But I want something that would solve my problem. I have slider view. In that I need to pass the Images as drawables or something like that. Image loading would be carried by Adapter. I can't load directly as far as I know. Is there any way to load glide images in drawable
.
Also what is the best way to catch or store the images locally, so that if there is no internet connection, still the slider works smoothly.
Here is my Adapater
public class MainPageSlideImageAdapter extends PagerAdapter {
private ArrayList<Integer> IMAGES;
private LayoutInflater inflater;
private Context context;
public MainPageSlideImageAdapter(Context context,ArrayList<Integer> IMAGES) {
this.context = context;
this.IMAGES=IMAGES;
inflater = LayoutInflater.from(context);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public int getCount() {
return IMAGES.size();
}
@Override
public Object instantiateItem(ViewGroup view, int position) {
View imageLayout = inflater.inflate(R.layout.main_page_slidingimages, view, false);
assert imageLayout != null;
final ImageView imageView = (ImageView) imageLayout
.findViewById(R.id.image);
imageView.setImageResource(IMAGES.get(position));
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;
}
}
you must provide the view in .into()
please view the sample
sample layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/myImage"
android:layout_width="100dp"
android:layout_height="100dp"/>
</LinearLayout>
Update
@Override
public Object instantiateItem(ViewGroup view, int position) {
View imageLayout = inflater.inflate(R.layout.main_page_slidingimages, view, false);
assert imageLayout != null;
final ImageView imageView = (ImageView) imageLayout
.findViewById(R.id.image);
Glide.with(this)
.load(IMAGES.get(position))
.error(R.drawable.image2)
.into(imageView);
//imageView.setImageResource();
view.addView(imageLayout, 0);
return imageLayout;
}
imageView.setImageResource
simply needs replaced with a Glide call.
Or use an ArrayAdapter of Strings for Urls that you want to download. I don't think you can "overwrite" a drawable from Glide
Instead of storing the image in memory, store the address instead.
private static final String[] IMAGES = {"https://google.com/someImage1.jpg", "https://google.com/someImage2.jpg"};
Then load the image when you instantiateItem
@Override
public Object instantiateItem(ViewGroup view, int position) {
View imageLayout = inflater.inflate(R.layout.main_page_slidingimages, view, false);
assert imageLayout != null;
final ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
Glide.with(context)
.load(IMAGES.get(position))
.error(R.drawable.image1)
.into(imageView);
view.addView(imageLayout, 0);
return imageLayout;
}
Also, glide provides memory and disk caching. This means it should be able to show images even if there is no internet.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.