在Java中使用Kotlin库 [英] Using Kotlin Library in java

查看:226
本文介绍了在Java中使用Kotlin库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Android Studio中使用GitHub库(MeowBottomNavigation),但是它是用kotlin编写的,我无法在其中使用侦听器. 唯一给出的就是这个

I am trying to use a GitHub library (MeowBottomNavigation)in Android Studio.But its written in kotlin and i cant use the listeners in it. The only thing which is given is this

bottomNavigation.setOnShowListener {
}

bottomNavigation.setOnClickMenuListener {
}

建议显示使用

(Function1)

(Function1)

我不确定如何在java中实现它.任何帮助将不胜感激.

i am not sure as to how to implement this in java . Any help will be appreciated.

我熟悉Java,但是该库是用Kotlin编写的.有什么办法可以在Java中使用这些侦听器?

I am familiar with java but the library is written in Kotlin. Is there any way to use these listeners in java?

bottomNavigation.setOnClickMenuListener(new 
Function1<MeowBottomNavigation.Model, Unit>() {
        @Override
        public Unit invoke(MeowBottomNavigation.Model p1) {
            int i = p1.getId();
            switch (i){
                case 4:
                    Toast.makeText(UserMainActivity.this, i, Toast.LENGTH_SHORT).show();
                    break;
                case  1:
                    Toast.makeText(UserMainActivity.this, i, Toast.LENGTH_SHORT).show();
                    break;
                case 2:
                    Toast.makeText(UserMainActivity.this, i, Toast.LENGTH_SHORT).show();
                    break;
                case 3:
                    Toast.makeText(UserMainActivity.this, i, Toast.LENGTH_SHORT).show();
                    break;
            }
            return Unit.INSTANCE;
        }
    });

推荐答案

Function0,Function1,Function2,... FunctionN是kotlin中的高阶函数.

Function0, Function1, Function2, ... FunctionN are higher-order functions in kotlin.

转换为Java后,您的点击监听器将变为以下内容.

After converting to java, your click listeners become something like below.

// Set Menu Click Listener 
bottomNavigation.setOnClickMenuListener(new Function1<MeowBottomNavigation.Model, Unit>() {
        @Override
        public Unit invoke(MeowBottomNavigation.Model p1) {
            return Unit.INSTANCE;
        }
    });

// Set Menu Show listener
bottomNavigation.setOnShowListener(new Function1<MeowBottomNavigation.Model, Unit>() {
        @Override
        public Unit invoke(MeowBottomNavigation.Model s) {
            return Unit.INSTANCE;
        }
    });

这篇关于在Java中使用Kotlin库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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