简体   繁体   English

将 java 类转换为从 FragmentStatePageAdapter 继承的 kotlin

[英]convert java class to kotlin inherited from FragmentStatePageAdapter

I am doing a project and the only help I have are in java, and it is making it very difficult for me to have to convert it or pass it to kotlin this is the code I have to pass to kotlin:我正在做一个项目,我唯一的帮助是在 Java 中,这让我很难转换它或将它传递给 kotlin,这是我必须传递给 kotlin 的代码:

private class ScreenSliderPagerAdapter extends FragmentStatePagerAdapter
    {
        public ScreenSliderPagerAdapter(@NonNull FragmentManager fm)
        {
            super(fm);
        }
        @NonNull
        @Override
        public Fragment getItem(int position)
        {
            switch(position)
            {
                case 0:
                    SwipeFragement1 tab1 = new SwipeFragement1();
                    return tab1;
                case 1:
                    SwipeFragement2 tab2 = new SwipeFragement2();
                    return tab2;
                case 2:
                    SwipeFragemen3 tab3 = new SwipeFragement3();
                    return tab3;
            }
            return null;
        }
        @Override
        public int getCount()
        {
            return 0:
        }
    }

and when trying to convert it, it was more or less like this:当试图转换它时,它或多或少是这样的:

private class ScreenSliderPagerAdapter (fm : FragmentManager @NonNull) : FragmentStatePagerAdapter
    {
        var fm : FragmentManager = fm
        
        constructor():this()
        {
            super(fm)
        }
        @NonNull
        @Override
        public fun getItem(position: Int): SwipeFragement1?
        {
            var tab : SwipeFragement1? = null
            when(position)
            {
                0 -> tab = SwipeFragement1()
                1 -> tab = SwipeFragement2()
                2 -> tab = SwipeFragement3()
            }
            return tab
        }
        @Override
        public fun getCount(): Int
        {
            return 0
        }
    }

According to how little I know, Kotlin is looking very bad as seen in the following screenshot:据我所知,Kotlin 看起来非常糟糕,如下面的截图所示:

在此处输入图片说明

In case you're wondering I'm trying to fragment an actity万一你想知道我正在尝试分割一个活动

You got that error because the Kotlin code has some syntax errors.你得到这个错误是因为 Kotlin 代码有一些语法错误。 Here is the right one.这是正确的。

private class ScreenSliderPagerAdapter(fm: FragmentManager) :
    FragmentStatePagerAdapter(fm) {

    val NUM_TABS = 3
    
    override fun getItem(position: Int): Fragment {
        return when (position) {
            0 -> SwipeFragement1()
            1 -> SwipeFragement2()
            else -> SwipeFragement3()
        }
    }

    override fun getCount(): Int {
        return NUM_TABS
    }
}

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

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