首页 > 解决方案 > 如何在 xamarin android 中使用这个库(smarteist Android-Image-Slider)?

问题描述

我是 xamarin 的初学者,并试图在 android xamarin 上使用这个库(smarteist Android-Image-Slider)。

图书馆来源:

NuGet:

https://www.nuget.org/packages/Karamunting.Android.Smarteist.AutoImageSlider/1.3.2

GitHub:

https://github.com/smarteist/Android-Image-Slider

它正在 android xamarin 上运行,但我不知道如何使用它,只有 android studio 示例。

此类“SliderAdapterExample”现在工作正常

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Smarteist.AutoImageSlider;
using BumpTech.GlideLib;


namespace PriceChecker
{
    public class SliderAdapterExample : SliderViewAdapter
    {
        private Context context;
        private int mCount;
        public SliderAdapterExample(Context context)
        {
            this.context = context;
        }

        public void setCount(int count)
        {
            this.mCount = count;
        }
        public int getCount()
        {
            //slider view count could be dynamic size
            return mCount;
        }

        public override int Count => 4;


        public override void OnBindViewHolder(Java.Lang.Object viewHolder, int position)
        {
            SliderAdapterVH _viewHolder = (SliderAdapterVH)viewHolder;
            _viewHolder.textViewDescription.Text = "This is slider item " + position;

            switch (position)
            {
                case 0:
                    Glide.With(_viewHolder.itemView)
                            .Load("https://images.pexels.com/photos/218983/pexels-photo-218983.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260")
                            .Into(_viewHolder.imageViewBackground);
                    break;
                case 1:
                    Glide.With(_viewHolder.itemView)
                            .Load("https://images.pexels.com/photos/747964/pexels-photo-747964.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260")
                            .Into(_viewHolder.imageViewBackground);
                    break;
                case 2:
                    Glide.With(_viewHolder.itemView)
                            .Load("https://images.pexels.com/photos/929778/pexels-photo-929778.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260")
                            .Into(_viewHolder.imageViewBackground);
                    break;
                default:
                    Glide.With(_viewHolder.itemView)
                            .Load("https://images.pexels.com/photos/218983/pexels-photo-218983.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260")
                            .Into(_viewHolder.imageViewBackground);
                    break;

            }
        }

        public override Java.Lang.Object OnCreateViewHolder(ViewGroup parent)
        {
            View inflate = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.image_slider_layout_item, null);
            return new SliderAdapterVH(inflate);
        }


        class SliderAdapterVH : SliderViewAdapter.ViewHolder
        {

            public View itemView;
            public ImageView imageViewBackground;
            public TextView textViewDescription;

            public SliderAdapterVH(View itemView) : base(itemView)
            {

                imageViewBackground = itemView.FindViewById<ImageView>(Resource.Id.iv_auto_image_slider);
                textViewDescription = itemView.FindViewById<TextView>(Resource.Id.tv_auto_image_slider);
                this.itemView = itemView;

            }
        }
    }
}

但是现在我在图像中的Activity中有很多错误

图片

标签: c#androidxamarinandroidimageslider

解决方案


不幸的是,分销商没有为他们的 nuget 包提供 xamarin 的示例代码。通常代码看起来很相似,所以你可以使用 android 代码示例来猜测/尝试你的方式。

从扩展类开始

public class SliderAdapterExample : SliderViewAdapter<SliderAdapterExample.SliderAdapterVH> {

}

然后您可以在其中键入“公共覆盖”,Visual Studio 将提供一个可能的覆盖方法列表,然后您选择一个与 android 代码示例中的方法相匹配的方法。通常,在 Xamarin 中,方法将以大写字母开头,而 android 则以小写字母开头。

您还可以在已导入库的类中键入“SliderViewAdapter”,然后单击命令打开 nuget 实现摘要,这可以为您提供有关哪些部分可用以及它们如何命名和应该实现的更多信息.

像这样在 xamarin 中重新创建示例代码:

using System;
using Android.Content;
using Android.Views;
using Android.Widget;
using BumpTech.GlideLib;
using Java.Lang;
using Smarteist.AutoImageSlider;

namespace testproject.Droid
{
    public class testclass :  SliderViewAdapter
    {
        private Context context;
        public testclass(Context context)
        {
            this.context = context;
        }

        public override int Count => 4;

        public override void OnBindViewHolder(Java.Lang.Object viewHolder, int position)
        {
            SliderAdapterVH _viewHolder = (SliderAdapterVH)viewHolder;
            _viewHolder.textViewDescription.Text = "This is slider item " + position;

            switch (position)
            {
                case 0:
                    Glide.With(_viewHolder.itemView)
                            .Load("https://images.pexels.com/photos/218983/pexels-photo-218983.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260")
                            .Into(_viewHolder.imageViewBackground);
                    break;
                case 1:
                    Glide.With(_viewHolder.itemView)
                            .Load("https://images.pexels.com/photos/747964/pexels-photo-747964.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260")
                            .Into(_viewHolder.imageViewBackground);
                    break;
                case 2:
                    Glide.With(_viewHolder.itemView)
                            .Load("https://images.pexels.com/photos/929778/pexels-photo-929778.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260")
                            .Into(_viewHolder.imageViewBackground);
                    break;
                default:
                    Glide.With(_viewHolder.itemView)
                            .Load("https://images.pexels.com/photos/218983/pexels-photo-218983.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260")
                            .Into(_viewHolder.imageViewBackground);
                    break;

            }
        }

        public override Java.Lang.Object OnCreateViewHolder(ViewGroup parent)
        {
            View inflate = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.image_slider_layout_item, null);
            return new SliderAdapterVH(inflate);
        }


        class SliderAdapterVH : SliderViewAdapter.ViewHolder
        {

            public View itemView;
            public ImageView imageViewBackground;
            public TextView textViewDescription;

        public SliderAdapterVH(View itemView) : base(itemView)
        {
            imageViewBackground = itemView.FindViewById(Resource.Id.iv_auto_image_slider);
            textViewDescription = itemView.FindViewById(Resource.Id.tv_auto_image_slider);
            this.itemView = itemView;
        }
    }
}
}

推荐阅读