Xamarin.Android 如何使用 ViewPager 创建可滑动的图片库? [英] Xamarin.Android How do I create a swipeable image gallery using ViewPager?
问题描述
Android 画廊现已弃用,因此我需要使用 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屋!