如何在android中使用滑动库滑块图像

时间:2016-05-20 13:17:16

标签: android android-glide

如何使用android中的滑动库创建多个图像的图像滑块?

我曾使用滑动来制作一张图片但不能用于制作图片滑块。有人可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

您还可以查看此主题,它可以帮助我: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));
    }

希望这个帮助

答案 1 :(得分:0)

你可以使用android viewflipper。它使用起来很简单,你已经告诉过你可以将它用于一个图像我希望你能找到多个图像,你可以使用viewflippper来完成你的工作。