gpt4 book ai didi

android - 如何在android中为 slider 图像使用glide库

转载 作者:行者123 更新时间:2023-11-29 00:04:02 25 4
gpt4 key购买 nike

如何在 android 中使用 glide 库创建多个图像的图像 slider ?

我已经为一张图片使用了 glide,但无法用于创建图片 slider 。有人可以帮助我吗?

最佳答案

您也可以查看此主题,它对我有帮助:https://es.stackoverflow.com/questions/10869/c%c3%b3mo-crear-slide-de-im%c3%a1genes-con-viewpager-y-glide-en-android?newreg=6914a57c15384b95b02e445f995490b1 **

按照这个主题,我创建了自己的 Custompageradapter:

public class CustomPagerAdapter extends PagerAdapter {

private static final String TAG = "ImageViewPage";
Context mContext;
LayoutInflater mLayoutInflater;
String[] mResources;

public CustomPagerAdapter(Context context, String[] resources) {
mContext = context;
mResources = resources;
mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

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

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

@Override
public Object instantiateItem(ViewGroup container, int position) {
Log.d(TAG,
"instantiateItem() called with: " + "container = [" + container + "], position = [" + position + "]");

View itemView = mLayoutInflater.inflate(R.layout.item_pager, container, false);

Log.d(TAG, "load in gallery:" + mResources[position] + "#end");
final ImageView ivPhoto = (ImageView) itemView.findViewById(R.id.iv_photo);

if (!mResources[position].equals("")){
Glide.with(mContext)
.load(mResources[position].trim())
.crossFade()
.into(ivPhoto);
}

container.addView(itemView);

return itemView;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
Log.d(TAG, "destroyItem() called with: " + "container = [" + container + "], position = [" + position
+ "], object = [" + object + "]");
container.removeView((LinearLayout) object);
}
}

我这样调用它:

   String[] photoUrls = {"http://example/photo.jpg",
"http://example2.jpeg"};


ViewPager viewPager = (ViewPager) binding.slider;

if (viewPager != null) {
viewPager.setAdapter(new CustomPagerAdapter(getContext(), photoUrls));
}

希望对你有帮助

关于android - 如何在android中为 slider 图像使用glide库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37347740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com