提交 7bc35678 编写于 作者: G Gaëtan Muller 提交者: Takeshi Hagikura

Convert Java files to Kotlin (#328)

* Convert Java files to Kotlin

* Update following @thagikura's comments
上级 6aa475ed
...@@ -17,6 +17,21 @@ ...@@ -17,6 +17,21 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules. // Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript { buildscript {
ext {
minSdkVersion = 9
targetSdkVersion = 25
compileSdkVersion = 25
buildToolsVersion = "25.0.3"
androidGradlePluginVersion = "2.3.3"
androidMavenGradlePluginVersion = "1.5"
gradleBintrayPluginVersion = "1.6"
kotlinVersion = "1.1.3"
supportLibVersion = "25.3.1"
espressoVersion = "2.2.2"
testRunnerVersion = "0.5"
junitVersion = "4.12"
}
repositories { repositories {
jcenter() jcenter()
maven { maven {
...@@ -24,25 +39,15 @@ buildscript { ...@@ -24,25 +39,15 @@ buildscript {
} }
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:2.3.3' classpath "com.android.tools.build:gradle:$androidGradlePluginVersion"
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5' classpath "com.github.dcendents:android-maven-gradle-plugin:$androidMavenGradlePluginVersion"
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.6' classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:$gradleBintrayPluginVersion"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
// NOTE: Do not place your application dependencies here; they belong // NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files // in the individual module build.gradle files
} }
} }
ext {
minSdkVersion = 9
targetSdkVersion = 25
compileSdkVersion = 25
buildToolsVersion = "25.0.3"
supportLibVersion = "25.3.1"
espressoVersion = "2.2.2"
testRunnerVersion = "0.5"
junitVersion = "4.12"
}
allprojects { allprojects {
repositories { repositories {
jcenter() jcenter()
...@@ -63,9 +68,9 @@ project.ext.preDexLibs = !project.hasProperty('disablePreDex') ...@@ -63,9 +68,9 @@ project.ext.preDexLibs = !project.hasProperty('disablePreDex')
subprojects { subprojects {
project.plugins.whenPluginAdded { plugin -> project.plugins.whenPluginAdded { plugin ->
if ("com.android.build.gradle.AppPlugin".equals(plugin.class.name)) { if ("com.android.build.gradle.AppPlugin" == plugin.class.name) {
project.android.dexOptions.preDexLibraries = rootProject.ext.preDexLibs project.android.dexOptions.preDexLibraries = rootProject.ext.preDexLibs
} else if ("com.android.build.gradle.LibraryPlugin".equals(plugin.class.name)) { } else if ("com.android.build.gradle.LibraryPlugin" == plugin.class.name) {
project.android.dexOptions.preDexLibraries = rootProject.ext.preDexLibs project.android.dexOptions.preDexLibraries = rootProject.ext.preDexLibs
} }
} }
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
*/ */
apply plugin: 'com.android.application' apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
android { android {
compileSdkVersion rootProject.ext.compileSdkVersion compileSdkVersion rootProject.ext.compileSdkVersion
...@@ -39,8 +40,9 @@ android { ...@@ -39,8 +40,9 @@ android {
} }
dependencies { dependencies {
compile project(path: ":flexbox")
compile "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}" compile "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
compile "com.android.support:design:${rootProject.ext.supportLibVersion}" compile "com.android.support:design:${rootProject.ext.supportLibVersion}"
compile "com.android.support:recyclerview-v7:${rootProject.ext.supportLibVersion}" compile "com.android.support:recyclerview-v7:${rootProject.ext.supportLibVersion}"
compile project(path: ":flexbox") compile "org.jetbrains.kotlin:kotlin-stdlib:${rootProject.ext.kotlinVersion}"
} }
...@@ -14,66 +14,51 @@ ...@@ -14,66 +14,51 @@
* limitations under the License. * limitations under the License.
*/ */
package com.google.android.flexbox.apps.catgallery; package com.google.android.flexbox.apps.catgallery
import android.content.Context; import android.support.v7.widget.RecyclerView
import android.graphics.drawable.Drawable; import android.view.LayoutInflater
import android.support.v4.content.res.ResourcesCompat; import android.view.ViewGroup
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/** /**
* Adapter class that handles the data set with the {@link RecyclerView.LayoutManager} * Adapter class that handles the data set with the {@link RecyclerView.LayoutManager}
*/ */
class CatAdapter extends RecyclerView.Adapter<CatViewHolder> { internal class CatAdapter : RecyclerView.Adapter<CatViewHolder>() {
private static final int[] CAT_IMAGE_IDS = new int[]{ companion object {
R.drawable.cat_1, private val CAT_IMAGE_IDS = intArrayOf(
R.drawable.cat_2, R.drawable.cat_1,
R.drawable.cat_3, R.drawable.cat_2,
R.drawable.cat_4, R.drawable.cat_3,
R.drawable.cat_5, R.drawable.cat_4,
R.drawable.cat_6, R.drawable.cat_5,
R.drawable.cat_7, R.drawable.cat_6,
R.drawable.cat_8, R.drawable.cat_7,
R.drawable.cat_9, R.drawable.cat_8,
R.drawable.cat_10, R.drawable.cat_9,
R.drawable.cat_11, R.drawable.cat_10,
R.drawable.cat_12, R.drawable.cat_11,
R.drawable.cat_13, R.drawable.cat_12,
R.drawable.cat_14, R.drawable.cat_13,
R.drawable.cat_15, R.drawable.cat_14,
R.drawable.cat_16, R.drawable.cat_15,
R.drawable.cat_17, R.drawable.cat_16,
R.drawable.cat_18, R.drawable.cat_17,
R.drawable.cat_19, R.drawable.cat_18,
}; R.drawable.cat_19
)
private Context mContext;
CatAdapter(Context context) {
mContext = context;
} }
@Override override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CatViewHolder {
public CatViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { val view = LayoutInflater.from(parent.context)
View view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.viewholder_cat, parent, false)
.inflate(R.layout.viewholder_cat, parent, false); return CatViewHolder(view)
return new CatViewHolder(view);
} }
@Override override fun onBindViewHolder(holder: CatViewHolder, position: Int) {
public void onBindViewHolder(CatViewHolder holder, int position) { val pos = position % CAT_IMAGE_IDS.size
int pos = position % CAT_IMAGE_IDS.length; holder.bindTo(CAT_IMAGE_IDS[pos])
Drawable drawable = ResourcesCompat.getDrawable(mContext.getResources(),
CAT_IMAGE_IDS[pos], null);
holder.bindTo(drawable);
} }
@Override override fun getItemCount() = CAT_IMAGE_IDS.size * 4
public int getItemCount() {
return CAT_IMAGE_IDS.length * 4;
}
} }
...@@ -14,34 +14,26 @@ ...@@ -14,34 +14,26 @@
* limitations under the License. * limitations under the License.
*/ */
package com.google.android.flexbox.apps.catgallery; package com.google.android.flexbox.apps.catgallery
import android.graphics.drawable.Drawable; import android.support.annotation.DrawableRes
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView
import android.view.View; import android.view.View
import android.view.ViewGroup; import android.widget.ImageView
import android.widget.ImageView; import com.google.android.flexbox.FlexboxLayoutManager
import com.google.android.flexbox.FlexboxLayoutManager;
/** /**
* ViewHolder that represents a cat image. * ViewHolder that represents a cat image.
*/ */
class CatViewHolder extends RecyclerView.ViewHolder { internal class CatViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private ImageView mImageView;
CatViewHolder(View itemView) { private val imageView = itemView.findViewById(R.id.imageview) as ImageView
super(itemView);
mImageView = (ImageView) itemView.findViewById(R.id.imageview);
}
void bindTo(Drawable drawable) { internal fun bindTo(@DrawableRes drawableRes: Int) {
mImageView.setImageDrawable(drawable); imageView.setImageResource(drawableRes)
ViewGroup.LayoutParams lp = mImageView.getLayoutParams(); val lp = imageView.layoutParams
if (lp instanceof FlexboxLayoutManager.LayoutParams) { if (lp is FlexboxLayoutManager.LayoutParams) {
FlexboxLayoutManager.LayoutParams flexboxLp = (FlexboxLayoutManager.LayoutParams) lp; lp.flexGrow = 1f
flexboxLp.setFlexGrow(1.0f);
} }
} }
} }
...@@ -14,18 +14,16 @@ ...@@ -14,18 +14,16 @@
* limitations under the License. * limitations under the License.
*/ */
package com.google.android.flexbox.apps.catgallery; package com.google.android.flexbox.apps.catgallery
import android.os.Bundle; import android.os.Bundle
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar
import com.google.android.flexbox.AlignItems
import com.google.android.flexbox.AlignItems; import com.google.android.flexbox.FlexDirection
import com.google.android.flexbox.FlexDirection; import com.google.android.flexbox.FlexWrap
import com.google.android.flexbox.FlexWrap; import com.google.android.flexbox.FlexboxLayoutManager
import com.google.android.flexbox.FlexboxLayout;
import com.google.android.flexbox.FlexboxLayoutManager;
/** /**
* Launcher Activity for the cat gallery demo app that demonstrates the usage of the * Launcher Activity for the cat gallery demo app that demonstrates the usage of the
...@@ -34,22 +32,25 @@ import com.google.android.flexbox.FlexboxLayoutManager; ...@@ -34,22 +32,25 @@ import com.google.android.flexbox.FlexboxLayoutManager;
* Thus compared to using the {@link FlexboxLayout}, it's much less likely to abuse the memory, * Thus compared to using the {@link FlexboxLayout}, it's much less likely to abuse the memory,
* which some times leads to the OutOfMemoryError. * which some times leads to the OutOfMemoryError.
*/ */
public class MainActivity extends AppCompatActivity { class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val toolbar = findViewById(R.id.toolbar) as Toolbar
setSupportActionBar(toolbar)
@Override val flexboxLayoutManager = FlexboxLayoutManager(this).apply {
protected void onCreate(Bundle savedInstanceState) { flexWrap = FlexWrap.WRAP
super.onCreate(savedInstanceState); flexDirection = FlexDirection.ROW
setContentView(R.layout.activity_main); alignItems = AlignItems.STRETCH
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); }
setSupportActionBar(toolbar);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview); val recyclerView = findViewById(R.id.recyclerview) as RecyclerView
FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(this); recyclerView.apply {
layoutManager.setFlexWrap(FlexWrap.WRAP); layoutManager = flexboxLayoutManager
layoutManager.setFlexDirection(FlexDirection.ROW); adapter = CatAdapter()
layoutManager.setAlignItems(AlignItems.STRETCH); }
recyclerView.setLayoutManager(layoutManager);
RecyclerView.Adapter adapter = new CatAdapter(this);
recyclerView.setAdapter(adapter);
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册