How to interpret and translate kotlin code in java?

I am trying to translate this Kotlin code in Java since the project is in Java. I translated by studying the syntax of Kotlin. However, there are others that are hard for me to understand.

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

In particular:

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 setViewPagerScroller I managed to translate the first part.

 Field scrollerField = ViewPager.class.getDeclaredField("mScroller"); scrollerField.setAccessible(true); Field interpolator = ViewPager.class.getDeclaredField("sInterpolator"); interpolator.setAccessible(true); 
+5
source share
1 answer

The setViewPagerScroller method uses the kotlin anonymous inner class . This is part of an β€œobject” that has no real counterpart in Java syntax.

 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 casting in java. Hope you can use this to translate fun generateFinalButtonBehaviour() , it contains more than one.

The Lazy construct unsurprisingly gets more details in java. You must use the discipline not to access the viewpager incorrectly if you decide to follow the structure below.

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

You can also use the class to more accurately represent the lazy initialization of your fields. (So ​​that you cannot mistakenly access the field that you would like to lazily initialize). It gets even more detailed, but may be worth it.

+3
source

Source: https://habr.com/ru/post/1272765/


All Articles