Xamarin.Android如何使用ViewPager创建可滑动式图库? [英] Xamarin.Android How do I create a swipeable image gallery using ViewPager?

查看:118
本文介绍了Xamarin.Android如何使用ViewPager创建可滑动式图库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android Gallery现在已弃用,因此我需要使用ViewPager.我只找到了Xamarin.Forms的代码示例,因此,如果有人可以为我提供Xamarin.Android的代码示例,我将不胜感激.

Android gallery is now deprecated, so I need to use ViewPager. I only found code samples for Xamarin.Forms, so I would appreciate if someone could help me with a code sample for Xamarin.Android.

我的问题不同于单击Viewpager Xamarin Android ,因为我没有需要单击照片,我只需要滑动图库中的图像即可.

My question is different from Click on Viewpager Xamarin Android because I don't need to click the photos, I only needed to swipe the images in the gallery.

推荐答案

尽管这是一个不同的问题,但是这里有一个解决方案,不需要您将图像片段用于简单的滑块. https://stackoverflow.com/a/33209130/5436000

Although its a different question there is a solution here that doesn't require you to use an image fragment for a simple slider. https://stackoverflow.com/a/33209130/5436000

public class ImageSliderAdapter : PagerAdapter
{
    Context _context;
    List<string> _imageUrls;

    public ImageSliderAdapter (Context context, List<string> imageUrls)
    {
        _imageUrls = imageUrls;
        _context = context;
    }

    public override bool IsViewFromObject (Android.Views.View view, Java.Lang.Object @object)
    {
        return view == ((LinearLayout)@object);
    }

    public override int Count {
        get {
            return _imageUrls.Count;
        }
    }

    public override void DestroyItem (ViewGroup container, int position, Java.Lang.Object objectValue)
    {
    }

    public override Java.Lang.Object InstantiateItem (ViewGroup container, int position)
    {

        View view = container;
        var inflater = _context.GetSystemService (Context.LayoutInflaterService) as LayoutInflater;
        view = inflater.Inflate (Resource.Layout.image_slider_item, null);
        var child = view.FindViewById<ImageView> (Resource.Id.image_slider_item);
        child.Click += (o, e) =>
        {
            //your code here
        };

        Bitmap image = null;
        Task.Run (() => {
            URL url = new URL (_imageUrls [position]);
            image = BitmapFactory.DecodeStream (url.OpenConnection ().InputStream);
        }).ContinueWith (t => {
            (_context as MainView).RunOnUiThread (() => {
                child.SetImageBitmap (image);
            });
        });

        container.AddView (view);
        return view;
    }
}

这篇关于Xamarin.Android如何使用ViewPager创建可滑动式图库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆