如何将此库 (smarteist Android-Image-Slider) 与 xamarin android 一起使用?

How can I use this library (smarteist Android-Image-Slider) with xamarin android?

我是 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 示例。

这个 class "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有很多错误

image

不幸的是,发行商没有为他们的 nuget 包提供 xamarin 的示例代码。通常代码看起来非常相似,因此您可以使用 android 代码示例 guess/try 自己的方式。

首先扩展 class

public class SliderAdapterExample : SliderViewAdapter<SliderAdapterExample.SliderAdapterVH> {

}

然后你可以在其中输入 "public override",visual studio 将提供一个可能的方法列表来覆盖,然后你选择一个似乎与 [=26= 中的方法匹配的方法] 代码示例。通常在 Xamarin 中,方法将以大写字母开头,而不是 android 以小写字母开头。

您还可以在导入库的 class 中键入 "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;
        }
    }
}
}

感谢@Joachim Haglund 这里是完整的答案

MyActivity 布局:

<android.widget.RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MyActivity"
    android:id="@+id/my_activity">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <com.smarteist.autoimageslider.SliderView
                android:id="@+id/imageSlider1"
                android:layout_width="0dip"
                android:layout_height="match_parent"
                app:sliderAnimationDuration="600"
                app:sliderAutoCycleDirection="back_and_forth"
                app:sliderAutoCycleEnabled="true"
                app:sliderCircularHandlerEnabled="true"
                app:sliderIndicatorAnimationDuration="600"
                app:sliderIndicatorGravity="center_horizontal|bottom"
                app:sliderIndicatorMargin="15dp"
                app:sliderIndicatorOrientation="horizontal"
                app:sliderIndicatorPadding="3dp"
                app:sliderIndicatorRadius="2dp"
                app:sliderIndicatorSelectedColor="#5A5A5A"
                app:sliderIndicatorUnselectedColor="#FFF"
                app:sliderScrollTimeInSec="1"
                app:sliderStartAutoCycle="true"
                android:layout_weight=".5"/>

        </LinearLayout>

</android.widget.RelativeLayout>

SliderAdapterExample Class:

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 MyApp
{
    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;


            }
        }
    }
}

我的活动:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using Android.App;
using Android.OS;
using Android.Widget;
using Smarteist.AutoImageSlider;

namespace MyApp
{
    [Activity(Label = "MyActivity", MainLauncher = true)]
    public class MyActivity: Activity
    {

        SliderView sliderView;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.my_activity);

            sliderView = FindViewById<SliderView>(Resource.Id.imageSlider1);

            SliderAdapterExample adapter = new SliderAdapterExample(this);

            adapter.setCount(25);

            sliderView.SliderAdapter = adapter;

            sliderView.SetIndicatorAnimation(IndicatorAnimations.Slide); //set indicator animation by using SliderLayout.IndicatorAnimations. :WORM or THIN_WORM or COLOR or DROP or FILL or NONE or SCALE or SCALE_DOWN or SLIDE and SWAP!!
            sliderView.SetSliderTransformAnimation(SliderAnimations.CubeInRotationTransformation);
            sliderView.AutoCycleDirection =SliderView.AutoCycleDirectionBackAndForth;
            sliderView.IndicatorSelectedColor = Android.Graphics.Color.White;
            sliderView.IndicatorUnselectedColor = Android.Graphics.Color.Gray;
            sliderView.ScrollTimeInSec=4;
            //sliderView.AutoCycleDirection = SliderAnimations.CubeInRotationTransformation;
            sliderView.StartAutoCycle();

        }
    }
}