提交 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 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
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 {
jcenter()
maven {
......@@ -24,25 +39,15 @@ buildscript {
}
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.6'
classpath "com.android.tools.build:gradle:$androidGradlePluginVersion"
classpath "com.github.dcendents:android-maven-gradle-plugin:$androidMavenGradlePluginVersion"
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
// 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 {
repositories {
jcenter()
......@@ -63,9 +68,9 @@ project.ext.preDexLibs = !project.hasProperty('disablePreDex')
subprojects {
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
} 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
}
}
......
......@@ -15,6 +15,7 @@
*/
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
android {
compileSdkVersion rootProject.ext.compileSdkVersion
......@@ -39,8 +40,9 @@ android {
}
dependencies {
compile project(path: ":flexbox")
compile "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
compile "com.android.support:design:${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 @@
* limitations under the License.
*/
package com.google.android.flexbox.apps.catgallery;
package com.google.android.flexbox.apps.catgallery
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.res.ResourcesCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.ViewGroup
/**
* 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[]{
R.drawable.cat_1,
R.drawable.cat_2,
R.drawable.cat_3,
R.drawable.cat_4,
R.drawable.cat_5,
R.drawable.cat_6,
R.drawable.cat_7,
R.drawable.cat_8,
R.drawable.cat_9,
R.drawable.cat_10,
R.drawable.cat_11,
R.drawable.cat_12,
R.drawable.cat_13,
R.drawable.cat_14,
R.drawable.cat_15,
R.drawable.cat_16,
R.drawable.cat_17,
R.drawable.cat_18,
R.drawable.cat_19,
};
private Context mContext;
CatAdapter(Context context) {
mContext = context;
companion object {
private val CAT_IMAGE_IDS = intArrayOf(
R.drawable.cat_1,
R.drawable.cat_2,
R.drawable.cat_3,
R.drawable.cat_4,
R.drawable.cat_5,
R.drawable.cat_6,
R.drawable.cat_7,
R.drawable.cat_8,
R.drawable.cat_9,
R.drawable.cat_10,
R.drawable.cat_11,
R.drawable.cat_12,
R.drawable.cat_13,
R.drawable.cat_14,
R.drawable.cat_15,
R.drawable.cat_16,
R.drawable.cat_17,
R.drawable.cat_18,
R.drawable.cat_19
)
}
@Override
public CatViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.viewholder_cat, parent, false);
return new CatViewHolder(view);
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CatViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.viewholder_cat, parent, false)
return CatViewHolder(view)
}
@Override
public void onBindViewHolder(CatViewHolder holder, int position) {
int pos = position % CAT_IMAGE_IDS.length;
Drawable drawable = ResourcesCompat.getDrawable(mContext.getResources(),
CAT_IMAGE_IDS[pos], null);
holder.bindTo(drawable);
override fun onBindViewHolder(holder: CatViewHolder, position: Int) {
val pos = position % CAT_IMAGE_IDS.size
holder.bindTo(CAT_IMAGE_IDS[pos])
}
@Override
public int getItemCount() {
return CAT_IMAGE_IDS.length * 4;
}
override fun getItemCount() = CAT_IMAGE_IDS.size * 4
}
......@@ -14,34 +14,26 @@
* 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.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.google.android.flexbox.FlexboxLayoutManager;
import android.support.annotation.DrawableRes
import android.support.v7.widget.RecyclerView
import android.view.View
import android.widget.ImageView
import com.google.android.flexbox.FlexboxLayoutManager
/**
* ViewHolder that represents a cat image.
*/
class CatViewHolder extends RecyclerView.ViewHolder {
private ImageView mImageView;
internal class CatViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
CatViewHolder(View itemView) {
super(itemView);
mImageView = (ImageView) itemView.findViewById(R.id.imageview);
}
private val imageView = itemView.findViewById(R.id.imageview) as ImageView
void bindTo(Drawable drawable) {
mImageView.setImageDrawable(drawable);
ViewGroup.LayoutParams lp = mImageView.getLayoutParams();
if (lp instanceof FlexboxLayoutManager.LayoutParams) {
FlexboxLayoutManager.LayoutParams flexboxLp = (FlexboxLayoutManager.LayoutParams) lp;
flexboxLp.setFlexGrow(1.0f);
internal fun bindTo(@DrawableRes drawableRes: Int) {
imageView.setImageResource(drawableRes)
val lp = imageView.layoutParams
if (lp is FlexboxLayoutManager.LayoutParams) {
lp.flexGrow = 1f
}
}
}
......@@ -14,18 +14,16 @@
* limitations under the License.
*/
package com.google.android.flexbox.apps.catgallery;
package com.google.android.flexbox.apps.catgallery
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import com.google.android.flexbox.AlignItems;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayout;
import com.google.android.flexbox.FlexboxLayoutManager;
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.Toolbar
import com.google.android.flexbox.AlignItems
import com.google.android.flexbox.FlexDirection
import com.google.android.flexbox.FlexWrap
import com.google.android.flexbox.FlexboxLayoutManager
/**
* Launcher Activity for the cat gallery demo app that demonstrates the usage of the
......@@ -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,
* 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
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
val flexboxLayoutManager = FlexboxLayoutManager(this).apply {
flexWrap = FlexWrap.WRAP
flexDirection = FlexDirection.ROW
alignItems = AlignItems.STRETCH
}
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(this);
layoutManager.setFlexWrap(FlexWrap.WRAP);
layoutManager.setFlexDirection(FlexDirection.ROW);
layoutManager.setAlignItems(AlignItems.STRETCH);
recyclerView.setLayoutManager(layoutManager);
RecyclerView.Adapter adapter = new CatAdapter(this);
recyclerView.setAdapter(adapter);
val recyclerView = findViewById(R.id.recyclerview) as RecyclerView
recyclerView.apply {
layoutManager = flexboxLayoutManager
adapter = CatAdapter()
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册