Last active
March 23, 2020 22:22
Star
You must be signed in to star a gist
Java To Kotlin Conversion Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class NonLazyDemoAdapter extends FragmentStatePagerAdapter { | |
static class FragmentBundle implements Serializable { | |
int id; | |
String name; | |
FragmentBundle(int id, String name) { | |
this.id = id; | |
this.name = name; | |
} | |
} | |
public static NonLazyDemoAdapter createAdapter(FragmentManager fm) { | |
return new NonLazyDemoAdapter(fm); | |
} | |
private SomePage[] pages = new SomePage[]{FIRST, SECOND}; | |
private PageFragment[] cachedFragments = new PageFragment[pages.length]; | |
NonLazyDemoAdapter(FragmentManager fm) { | |
super(fm); | |
for (int i = 0; i < pages.length; i++) { | |
cachedFragments[i] = PageFragment.newInstance(pages[i], new FragmentBundle(i, pages[i].toString())); | |
} | |
} | |
@Override | |
public Fragment getItem(int position) { | |
return cachedFragments[position]; | |
} | |
@Override | |
public int getCount() { | |
return cachedFragments.length; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class NonLazyDemoAdapter extends FragmentStatePagerAdapter { | |
static class FragmentBundle implements Serializable { | |
@SerializedName("id") | |
int id; | |
@NonNull | |
@SerializedName("name") | |
String name; | |
FragmentBundle(int id, @NonNull String name) { | |
this.id = id; | |
this.name = name; | |
} | |
} | |
@NonNull | |
public static NonLazyDemoAdapter createAdapter(@NonNull FragmentManager fm) { | |
return new NonLazyDemoAdapter(fm); | |
} | |
@NonNull | |
private SomePage[] pages = new SomePage[]{FIRST, SECOND}; | |
@NonNull | |
private PageFragment[] cachedFragments = new PageFragment[pages.length]; | |
NonLazyDemoAdapter(@NonNull FragmentManager fm) { | |
super(fm); | |
for (int i = 0; i < pages.length; i++) { | |
cachedFragments[i] = PageFragment.newInstance(pages[i], new FragmentBundle(i, pages[i].toString())); | |
} | |
} | |
@NonNull | |
@Override | |
public Fragment getItem(int position) { | |
return cachedFragments[position]; | |
} | |
@Override | |
public int getCount() { | |
return cachedFragments.length; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NonLazyDemoAdapter internal constructor( | |
fm: FragmentManager | |
) : FragmentStatePagerAdapter(fm) { | |
internal class FragmentBundle( | |
@field:SerializedName("id") var id: Int, | |
@field:SerializedName("name") var name: String | |
) : Serializable | |
companion object { | |
fun createAdapter(fm: FragmentManager): NonLazyDemoAdapter { | |
return NonLazyDemoAdapter(fm) | |
} | |
} | |
private val pages = arrayOf(SomePage.FIRST, SomePage.SECOND) | |
private val cachedFragments = arrayOfNulls<PageFragment>(pages.size) | |
init { | |
for (i in pages.indices) { | |
cachedFragments[i] = PageFragment.newInstance(pages[i], FragmentBundle(i, pages[i].toString())) | |
} | |
} | |
override fun getItem(position: Int): Fragment { | |
return cachedFragments[position]!! | |
} | |
override fun getCount(): Int { | |
return cachedFragments.size | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NonLazyDemoAdapter internal constructor( | |
fm: FragmentManager | |
) : FragmentStatePagerAdapter(fm) { | |
internal class FragmentBundle( | |
@JvmField | |
@SerializedName("id") // if this field was used directly before we need JvmField annotation | |
var id: Int, | |
@JvmField | |
@SerializedName("name") // if this field was used directly before we need JvmField annotation | |
var name: String | |
) : Serializable | |
companion object { | |
@JvmStatic // if this method was used from Java before we need JvmSatic annotation | |
fun createAdapter(fm: FragmentManager): NonLazyDemoAdapter { | |
return NonLazyDemoAdapter(fm) | |
} | |
} | |
private val pages = arrayOf(SomePage.FIRST, SomePage.SECOND) | |
private val cachedFragments: ArrayList<PageFragment> = ArrayList() | |
init { | |
for (i in pages.indices) { | |
cachedFragments[i] = PageFragment.newInstance(pages[i], FragmentBundle(i, pages[i].toString())) | |
} | |
} | |
override fun getItem(position: Int): Fragment { | |
return cachedFragments[position] | |
} | |
override fun getCount(): Int { | |
return cachedFragments.size | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NonLazyDemoAdapter internal constructor( | |
fm: FragmentManager | |
) : FragmentStatePagerAdapter(fm) { | |
internal data class FragmentBundle( | |
@SerializedName("id") // caller side now can be refactored to use property, not JvmField | |
var id: Int, | |
@SerializedName("name") | |
var name: String | |
) : Serializable | |
companion object { | |
@JvmStatic | |
fun createAdapter(fm: FragmentManager): NonLazyDemoAdapter = NonLazyDemoAdapter(fm) | |
} | |
private val pages = arrayOf(SomePage.FIRST, SomePage.SECOND) | |
private val cachedFragments by lazy { | |
pages.mapIndexed { i, page -> PageFragment.newInstance(page, FragmentBundle(i, pages.toString())) } | |
} | |
override fun getItem(position: Int): Fragment = cachedFragments[position] | |
override fun getCount(): Int = cachedFragments.size | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment