Xamarin.Android Binding 源自github第三方库的绑定(中级教学)aar文件

摘要:
今天来个稍微复杂一点的源自github的android第三方库:CarouselView今天我们就是要解决这个问题,这个问题源自这个项目依赖的库效果图:今天我们的任务就是把它从Java整成C#,嘿嘿嘿嘿!!!!===============================================================================================好吧,以上是假教学,下面正式开始今天的教学===============================================================================================自己脑补假教学的1.2步骤,我们直接从第3步开始.如何解决这类waring呢?--warning : Couldn'tloadclasscom/synnapps/carouselview/CarouselView$1:java.lang.NoClassDefFoundError:android/support/v4/view/ViewPager$OnPageChangeListener其实这是一个依赖的问题,你注意到第三张图片里有好多未打勾的aar,jar文件了吗?

今天来个稍微复杂一点的源自github的android第三方库:CarouselView

(今天我们依然不用在metadata.xml里面写代码,虽然在build时不会有error出现,但是会有烦人的warning出现,导致我们在dll里面找不到我们需要使用的class)

今天我们就是要解决这个问题,这个问题源自这个项目依赖的库

效果图:Xamarin.Android Binding 源自github第三方库的绑定(中级教学)aar文件第1张

今天我们的任务就是把它从Java整成C#,嘿嘿嘿嘿!!!!

CarouselView的Github地址在这里:https://github.com/sayyam/carouselview

1.首先创建项目: 模板--->Android---->单一视图应用(Android)--->名称:CarouselViewProject

自己脑补图片0.0

2.CarouselViewProject--->引用(右键)--->Generate Xamarin.Android Bindings via gradle

从GitHub项目得到Gradle:compile 'com.synnapps:carouselview:0.1.4'

ok-->ok
默认的只勾选第一个aar文件:carouselview-0.1.4.aar剩下的就是这个aar所依赖的jar和aar文件.
Xamarin.Android Binding 源自github第三方库的绑定(中级教学)aar文件第2张
Xamarin.Android Binding 源自github第三方库的绑定(中级教学)aar文件第3张

3.重新生成项目

你会惊喜的发现,出现不少warning

warning : Couldn't load class com/synnapps/carouselview/CarouselView$1 : java.lang.NoClassDefFoundError:android/support/v4/view/ViewPager$OnPageChangeListener

什么鬼,这个核心的类,居然无法加载.

4.复制dll文件,然后删除掉自动添加的 绑定库项目 :Binding_CarouselView, 然后在CarouselViewProject项目里引用这个dll文件.

在引用里,双击Binding_CarouselView,在对象浏览器里,查看Binding_CarouselView,你会发现一件事情,xamarin你欺骗我,我的类呢0.0?????

Xamarin.Android Binding 源自github第三方库的绑定(中级教学)aar文件第4张

===============================================================================================

好吧,以上是假教学,下面正式开始今天的教学

===============================================================================================

自己脑补假教学的1.2步骤,我们直接从第3步开始.
如何解决这类waring呢?--warning : Couldn't load class com/synnapps/carouselview/CarouselView$1 : java.lang.NoClassDefFoundError:android/support/v4/view/ViewPager$OnPageChangeListener
其实这是一个依赖的问题,你注意到第三张图片里有好多未打勾的aar,jar文件了吗?这就是这个库所依赖的文件.
但是我们并不会打勾这些aar,jar文件,那我们如何引用这些库呢?
3. 其实这些都是android文件,而且Xamarin官方已经帮我们做好了dll文件,我们只需要在解决方案里,为 app,android binding library两个项目添加上这些nuget包即可,同时需要注意nuget包的版本和图三匹配(尽量做到匹配)
Nuget包:
Xamarin.Android.Support.v7.RecyclerView (25.1.0版本)

Xamarin.Android.Support.v7.AppCompat(25.1.0版本)

Xamarin.Android.Support.v4(25.1.0版本)

Xamarin.Android.Support.Annotations(好像并没有25.1.0版本,直接选择最新稳定版即可25.4.0.2)

虽然我们只选择了这四个包安装,但是因为nuget包之间有依赖关系,最终我们会安装上11个nuget包.

Xamarin.Android Binding 源自github第三方库的绑定(中级教学)aar文件第5张

Xamarin.Android Binding 源自github第三方库的绑定(中级教学)aar文件第6张

Xamarin.Android Binding 源自github第三方库的绑定(中级教学)aar文件第7张

Xamarin.Android Binding 源自github第三方库的绑定(中级教学)aar文件第8张

Xamarin.Android Binding 源自github第三方库的绑定(中级教学)aar文件第9张

Xamarin.Android Binding 源自github第三方库的绑定(中级教学)aar文件第10张

Xamarin.Android Binding 源自github第三方库的绑定(中级教学)aar文件第11张

Xamarin.Android Binding 源自github第三方库的绑定(中级教学)aar文件第12张

Xamarin.Android Binding 源自github第三方库的绑定(中级教学)aar文件第13张

4.Build整个解决方案,取走Binding_CarouselView的Binding_CarouselView.dll文件----(文件地址CarouselViewProject\CarouselViewProject\AndroidBindings\Binding_CarouselView\bin\Debug

(保存好这个dll)

5.先删除Binding_CarouselView项目,然后在CarouselViewProject项目的引用里添加刚才我们保存的Binding_CarouselView.dll

Xamarin.Android Binding 源自github第三方库的绑定(中级教学)aar文件第14张

6.一切准备就绪,让我们开始使用这个Com.Synnapps.Carouselview吧

让我们先看一下github的介绍,或者去sample项目浏览一下Java是怎么做的,然后我们要把它的代码变成C#!

如同周伯通,左手画方,右手画圆.

Github上面的代码,作者是这么写的:

Java版本

Usage

Include following code in your layout:
<com.synnapps.carouselview.CarouselView android:id="@+id/carouselView"android:layout_width="match_parent"android:layout_height="200dp"app:fillColor="#FFFFFFFF"app:pageColor="#00000000"app:radius="6dp"app:slideInterval="3000"app:strokeColor="#FF777777"app:strokeWidth="1dp"/>
Include following code in your activity: public class SampleCarouselViewActivity extendsAppCompatActivity { CarouselView carouselView; int[] sampleImages ={R.drawable.image_1, R.drawable.image_2, R.drawable.image_3, R.drawable.image_4, R.drawable.image_5}; @Override protected voidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sample_carousel_view); carouselView =(CarouselView) findViewById(R.id.carouselView); carouselView.setPageCount(sampleImages.length); carouselView.setImageListener(imageListener); } ImageListener imageListener = newImageListener() { @Override public void setImageForPosition(intposition, ImageView imageView) { imageView.setImageResource(sampleImages[position]); } }; }

C#版本

一.我们先找到5张图片放到Resources\drawable文件夹里面.(我直接使用github上面的5张图片)

二.在layout/Main.axml 里面填写以下xml代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">
  <com.synnapps.carouselview.CarouselView
      android:id="@+id/myCarouselView"android:layout_width="match_parent"android:layout_height="200dp"app:fillColor="#FFFFFFFF"app:pageColor="#00000000"app:radius="6dp"app:slideInterval="3000"app:strokeColor="#FF777777"app:strokeWidth="1dp" />
</LinearLayout>

三.在MainActivity.cs填写以下代码

usingSystem;
usingAndroid.App;
usingAndroid.Content;
usingAndroid.Runtime;
usingAndroid.Views;
usingAndroid.Widget;
usingAndroid.OS;
using Com.Synnapps.Carouselview;

namespaceCarouselViewProject
{
    [Activity(Label = "CarouselViewProject", MainLauncher = true, Icon = "@drawable/icon")]
    public classMainActivity : Activity, IImageListener
    {
        CarouselView myCarouselView;
        int[] sampleImages ={ Resource.Drawable.image_1, Resource.Drawable.image_2, Resource.Drawable.image_3, Resource.Drawable.image_4, Resource.Drawable.image_5 };

        public void SetImageForPosition(int p0, ImageView p1)
        {
            p1.SetImageResource(sampleImages[p0]);
        }

        protected override voidOnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            //Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
            myCarouselView = (CarouselView)FindViewById<CarouselView>(Resource.Id.myCarouselView);
            myCarouselView.PageCount =sampleImages.Length;
            myCarouselView.SetImageListener(this);
        }
    }
}

四.运行,效果图如下

Xamarin.Android Binding 源自github第三方库的绑定(中级教学)aar文件第15张

====================================================================================================

====================================================================================================

让我们接着跟着github作者的步骤走,提升以下难度,这次我们要自定义轮播喽,在图片左上角 增加文字和点击事件

====================================================================================================

====================================================================================================

Java版本:

If you want to add custom view, implementViewListener.

public class SampleCarouselViewActivity extendsAppCompatActivity {

    CarouselView customCarouselView;
    int NUMBER_OF_PAGES = 5;

    @Override
    protected voidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sample_carousel_view);

        customCarouselView =(CarouselView) findViewById(R.id.customCarouselView);
        customCarouselView.setPageCount(NUMBER_OF_PAGES);
        //set ViewListener for custom view 
customCarouselView.setViewListener(viewListener);
    }

    ViewListener viewListener = newViewListener() {
    
        @Override
        public View setViewForPosition(intposition) {
            View customView = getLayoutInflater().inflate(R.layout.view_custom, null);
            //set view attributes here
            
            returncustomView;
        }
    };

If you'd like to receive touch events for each image

customCarouselView.setImageClickListener(newImageClickListener() {
            @Override
            public void onClick(intposition) {
                Toast.makeText(SampleCarouselViewActivity.this, "Clicked item: "+position, Toast.LENGTH_SHORT).show();
            }
        });

C#版本:

一.layout,右键,添加,新建项,Android布局,view_custom.axml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:gravity="center">
    <ImageView
        android:id="@+id/fruitImageView"android:layout_width="fill_parent"android:layout_height="fill_parent"android:scaleType="centerCrop" />
    <TextView
        android:id="@+id/labelTextView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_marginTop="30dp"android:textSize="24sp"android:paddingLeft="10dp"android:paddingRight="10dp"android:paddingTop="5dp"android:paddingBottom="5dp"android:background="#44FFFFFF"android:fontFamily="sans-serif-thin"android:textColor="@android:color/white"/>
</RelativeLayout>

二.修改Main.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">
    <com.synnapps.carouselview.CarouselView
        android:id="@+id/customCarouselView"android:layout_width="match_parent"android:layout_height="200dp"app:fillColor="#FFFFFFFF"app:pageColor="#00000000"app:radius="6dp"app:slideInterval="3000"app:strokeColor="#FF777777"app:strokeWidth="1dp" />
</LinearLayout>

三.修改MainActivity.cs

usingSystem;
usingAndroid.App;
usingAndroid.Content;
usingAndroid.Runtime;
usingAndroid.Views;
usingAndroid.Widget;
usingAndroid.OS;
using Com.Synnapps.Carouselview;

namespaceCarouselViewProject
{
    [Activity(Label = "CarouselViewProject", MainLauncher = true, Icon = "@drawable/icon")]
    public classMainActivity : Activity, IViewListener, IImageClickListener
    {
        CarouselView customCarouselView;

        int[] sampleImages ={ Resource.Drawable.image_1, Resource.Drawable.image_2, Resource.Drawable.image_3, Resource.Drawable.image_4, Resource.Drawable.image_5 };
        String[] sampleTitles = { "Orange", "Grapes", "Strawberry", "Cherry", "Apricot"};


        public void OnClick(intp0)
        {
            Toast.MakeText(ApplicationContext, sampleTitles[p0], ToastLength.Long).Show();
        }


        public View SetViewForPosition(intp0)
        {
            View customView = this.LayoutInflater.Inflate(Resource.Layout.view_custom, null);
            TextView labelTextView =(TextView)customView.FindViewById(Resource.Id.labelTextView);
            ImageView fruitImageView =(ImageView)customView.FindViewById(Resource.Id.fruitImageView);

            labelTextView.Text=sampleTitles[p0];
            fruitImageView.SetImageResource(sampleImages[p0]);

            returncustomView;
        }

        protected override voidOnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            //Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);

            customCarouselView = (CarouselView)FindViewById<CarouselView>(Resource.Id.customCarouselView);
            customCarouselView.PageCount =sampleImages.Length;
            customCarouselView.SetViewListener(this);
            customCarouselView.SetImageClickListener(this);
        }
    }
}

效果图:

Xamarin.Android Binding 源自github第三方库的绑定(中级教学)aar文件第16张

再见0.0

免责声明:文章转载自《Xamarin.Android Binding 源自github第三方库的绑定(中级教学)aar文件》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇播放器测试的测试点服务CPU 100%排查思路下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

浅析Android动画(二),属性动画高级实例探究

转载请注明出处!http://www.cnblogs.com/wondertwo/p/5312482.html ObjectAnimator实现属性动画 为了写好Android动画这几篇博客,在动笔之前我是下过很大决心的,我对自己的要求是尽量把一个小知识点写清楚写明白,说白了就是相对于大而全的长篇大论,我更倾向于去写小而美的博客!为了保证在高产的同时能坚...

Android 三种动画详解

Android 三种动画详解 1 背景 不能只分析源码呀,分析的同时也要整理归纳基础知识,刚好有人微博私信让全面说说Android的动画,所以今天来一发Android应用的各种Animation大集合。英文厉害的请直接移步参考Android Developer。 Android系统提供了很多丰富的API去实现UI的2D与3D动画,最主要的划分可以分为如...

xamarin跨平台iOS、Android 与Windows

http://xamarin.csdn.net/ Xamarin是一个行动App开发平台,提供跨平台开发能力,开发人员透过Xamarin开发工具与程序语言,即可开发出iOS、Android 与Windows 等平台的原生(Native) App 应用程序,不须个别使用各平台的开发工具与程序语言,不只是「write-once, run everywhere」...

Xamarin Mono For Android 4.6.07004 完整离线安装破解版(C#开发Android、IOS工具)

Xamarin Mono For Android 常见问题解决方法/工具/教程大全:http://www.wuleba.com/tag/Mono 2013-05-24 附加消息,吾乐吧软件站提醒大家: Xamarin 4.6.07004 压缩包里面的 Xamarin iOS 插件最新版(mtvs-1.1.200.0)“不能”破解,请不要安装最新这个。与此同时...

《QT Creator快速入门》第十一章(一):图形视图

一、图形视图框架结构 图形视图框架由场景QGraphicsScene、视图QGraphicsView、图形项QGraphicsItem组成,它提供了一套基于图形项模型视图编程方法。图形视图框架可以管理数量庞大的自定义2D图形项,比如要绘制上万个图形并对这些图形进行拖动、检测位置等操作的话使用图形视图框架就可以方便的管理它们。场景中可以包含各种形状的图形项,...

Android蓝牙开发技术学习总结

Android开发,提供对蓝牙的通讯栈的支持,允许设别和其他的设备进行无线传输数据。应用程序层通过安卓API来调用蓝牙的相关功能,这些API使程序无线连接到蓝牙设备,并拥有P2P或者多端无线连接的特性。 蓝牙的功能: 1、扫描其他蓝牙设备 2、为可配对的蓝牙设备查询蓝牙适配器 3、建立RFCOMM通道(其实就是尼玛的认证) 4、通过服务搜索来链接其他的设备...