简体   繁体   English

如何解释和翻译kotlin代码到java?

[英]How to interpret and translate kotlin code to java?

I have been trying to translate this Kotlin code to Java since the project is in Java. 我一直在努力将这个Kotlin代码翻译成Java,因为该项目是用Java编写的。 I am translating by looking into Kotlin syntax. 我正在通过研究Kotlin语法进行翻译。 However, there are still others that I am having a hard time understanding. 但是,还有一些人我很难理解。

https://github.com/airbnb/lottie-android/blob/master/LottieSample/src/main/kotlin/com/airbnb/lottie/samples/AppIntroActivity.kt https://github.com/airbnb/lottie-android/blob/master/LottieSample/src/main/kotlin/com/airbnb/lottie/samples/AppIntroActivity.kt

Specifically: 特别:

private val animationView: LottieAnimationView by lazy {
    rootView.inflate(R.layout.app_intro_animation_view, false) as LottieAnimationView
}

private val viewPager: LockableViewPager by lazy {
    findViewById<LockableViewPager>(R.id.intro_activity_viewPager)
}

override fun generateFinalButtonBehaviour(): IntroButton.Behaviour {
    return object : IntroButton.Behaviour {
        override fun setActivity(activity: IntroActivity) { finish() }
        override fun getActivity(): IntroActivity? = null
        override fun run() {}
    }
}

private fun setViewPagerScroller() {
    try {
        val scrollerField = ViewPager::class.java.getDeclaredField("mScroller")
        scrollerField.isAccessible = true
        val interpolator = ViewPager::class.java.getDeclaredField("sInterpolator")
        interpolator.isAccessible = true

        val scroller = object : Scroller(this, interpolator.get(null) as Interpolator) {
            override fun startScroll(startX: Int, startY: Int, dx: Int, dy: Int, duration: Int) {
                super.startScroll(startX, startY, dx, dy, duration * 7)
            }
        }
        scrollerField.set(viewPager, scroller)
    } catch (e: NoSuchFieldException) {
        // Do nothing.
    } catch (e: IllegalAccessException) {
        // Do nothing.
    }
}

For the setViewPagerScroller , I was able to translate the first part. 对于setViewPagerScroller ,我能够翻译第一部分。

Field scrollerField = ViewPager.class.getDeclaredField("mScroller");
scrollerField.setAccessible(true);

Field interpolator = ViewPager.class.getDeclaredField("sInterpolator");
interpolator.setAccessible(true);

The method setViewPagerScroller uses kotlin anonymous inner class syntax. setViewPagerScroller方法使用kotlin 匿名内部类语法。 That is the 'object' part which has no real counterpart in java syntax. 这是'对象'部分,它在java语法中没有真正的对应部分。

private void setViewPagerScroller() {
    try {
        Field scrollerField = ViewPager.class.getDeclaredField("mScroller");
        scrollerField.setAccessible(true);

        Field interpolator = ViewPager.class.getDeclaredField("sInterpolator");
        interpolator.setAccessible(true);

        Scroller scroller = new Scroller(this, (android.view.animation.Interpolator) interpolator.get(null)){

            @Override
            public void startScroll(int startX, int startY, int dx, int dy, int duration) {
                super.startScroll(startX, startY, dx, dy, duration * 7);
            }
        }

        scrollerField.set(viewPager, scroller);
    } catch (NoSuchFieldException error) {
        // Do nothing.
    } catch (IllegalAccessException error) {
        // Do nothing.
    }
}

And the as keyword is like a casting in java. as关键字就像java中的一个转换。 Hopefully you can use this to translate fun generateFinalButtonBehaviour() , it contains more of the same. 希望你可以用它来翻译fun generateFinalButtonBehaviour() ,它包含更多相同的内容。

The Lazy construct unsurprisingly gets more verbose in java. 不出所料,Lazy构造在java中变得更加冗长。 You have to employ discipline to not access the viewpager wrongly if you choose to follow the structure below. 如果您选择遵循以下结构,则必须使用规则来错误地访问viewpager。

private LockableViewPager viewPager;

private LockableViewPager getViewPager(){
    if(viewPager == null){
        // produce viewpager and store in field
    }
    return viewPager;
}  

You can also use a class to more accurately represent a lazy initialization of your fields. 您还可以使用类来更准确地表示字段的延迟初始化。 (So that you cannot by mistake access a field which you meant to be lazily initialized) This gets even more verbose but could be worth it. (这样你就不会错误地访问一个你想要懒惰地初始化的字段)这变得更加冗长但是值得。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM