├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── mipmap-hdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── drawable-hdpi
│ │ │ │ └── ic_content_add.png
│ │ │ ├── drawable-mdpi
│ │ │ │ └── ic_content_add.png
│ │ │ ├── drawable-xhdpi
│ │ │ │ └── ic_content_add.png
│ │ │ ├── drawable-xxhdpi
│ │ │ │ └── ic_content_add.png
│ │ │ ├── drawable-xxxhdpi
│ │ │ │ └── ic_content_add.png
│ │ │ ├── values
│ │ │ │ ├── ids.xml
│ │ │ │ ├── colors.xml
│ │ │ │ ├── dimens.xml
│ │ │ │ ├── strings.xml
│ │ │ │ └── styles.xml
│ │ │ ├── layout
│ │ │ │ ├── content_main.xml
│ │ │ │ ├── empty_view.xml
│ │ │ │ ├── todo_item.xml
│ │ │ │ ├── fragment_todos.xml
│ │ │ │ └── activity_main.xml
│ │ │ └── menu
│ │ │ │ └── menu_main.xml
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── easy
│ │ │ │ └── kotlin
│ │ │ │ └── mytodoapplication
│ │ │ │ └── TAG.java
│ │ ├── kotlin
│ │ │ └── com
│ │ │ │ └── easy
│ │ │ │ └── kotlin
│ │ │ │ └── mytodoapplication
│ │ │ │ ├── model
│ │ │ │ └── Todo.kt
│ │ │ │ ├── MyTodoApplication.kt
│ │ │ │ ├── TodoAdapter.kt
│ │ │ │ ├── TodosFragment.kt
│ │ │ │ ├── MainActivity.kt
│ │ │ │ └── TodoEditFragment.kt
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── easy
│ │ │ └── kotlin
│ │ │ └── mytodoapplication
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── easy
│ │ └── kotlin
│ │ └── mytodoapplication
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── settings.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── gradle.properties
├── gradlew.bat
├── gradlew
└── README.md
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/EasyKotlin/chapter13_kotlin_android/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/EasyKotlin/chapter13_kotlin_android/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/EasyKotlin/chapter13_kotlin_android/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/EasyKotlin/chapter13_kotlin_android/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/EasyKotlin/chapter13_kotlin_android/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/EasyKotlin/chapter13_kotlin_android/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-hdpi/ic_content_add.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/EasyKotlin/chapter13_kotlin_android/HEAD/app/src/main/res/drawable-hdpi/ic_content_add.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-mdpi/ic_content_add.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/EasyKotlin/chapter13_kotlin_android/HEAD/app/src/main/res/drawable-mdpi/ic_content_add.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xhdpi/ic_content_add.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/EasyKotlin/chapter13_kotlin_android/HEAD/app/src/main/res/drawable-xhdpi/ic_content_add.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/ic_content_add.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/EasyKotlin/chapter13_kotlin_android/HEAD/app/src/main/res/drawable-xxhdpi/ic_content_add.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/EasyKotlin/chapter13_kotlin_android/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/EasyKotlin/chapter13_kotlin_android/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/EasyKotlin/chapter13_kotlin_android/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 | .externalNativeBuild
10 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxxhdpi/ic_content_add.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/EasyKotlin/chapter13_kotlin_android/HEAD/app/src/main/res/drawable-xxxhdpi/ic_content_add.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/EasyKotlin/chapter13_kotlin_android/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/EasyKotlin/chapter13_kotlin_android/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/values/ids.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/java/com/easy/kotlin/mytodoapplication/TAG.java:
--------------------------------------------------------------------------------
1 | package com.easy.kotlin.mytodoapplication;
2 |
3 | /**
4 | * Created by jack on 2017/7/20.
5 | */
6 |
7 | public class TAG {
8 | public static final String MY_TAG = "MY_TODOS";
9 | }
10 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #f2fced
4 | #456a7c
5 | #8fb3c4
6 |
7 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Thu Jul 20 23:39:17 CST 2017
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 | 16dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | 我的日程
3 | 设置
4 | 暂无待办事项,请添加
5 | 标题
6 | 待办内容
7 | 添加
8 | 保存
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/content_main.xml:
--------------------------------------------------------------------------------
1 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_main.xml:
--------------------------------------------------------------------------------
1 |
11 |
--------------------------------------------------------------------------------
/app/src/main/kotlin/com/easy/kotlin/mytodoapplication/model/Todo.kt:
--------------------------------------------------------------------------------
1 | package com.easy.kotlin.mytodoapplication.model
2 |
3 | import io.realm.RealmObject
4 | import io.realm.annotations.PrimaryKey
5 | import io.realm.annotations.RealmClass
6 |
7 | /**
8 | * Created by jack on 2017/7/21.
9 | */
10 | @RealmClass
11 | open class Todo : RealmObject() {
12 | @PrimaryKey
13 | open var id: String = "-1"
14 | open var title: String = "日程"
15 | open var content: String = "事项"
16 | }
17 |
--------------------------------------------------------------------------------
/app/src/test/java/com/easy/kotlin/mytodoapplication/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.easy.kotlin.mytodoapplication;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/empty_view.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
13 |
14 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/todo_item.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
10 |
11 |
15 |
16 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/easy/kotlin/mytodoapplication/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.easy.kotlin.mytodoapplication;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumentation test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.easy.kotlin.mytodoapplication", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
13 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_todos.xml:
--------------------------------------------------------------------------------
1 |
12 |
13 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/jack/Library/Android/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
--------------------------------------------------------------------------------
/app/src/main/kotlin/com/easy/kotlin/mytodoapplication/MyTodoApplication.kt:
--------------------------------------------------------------------------------
1 | package com.easy.kotlin.mytodoapplication
2 |
3 | import android.app.Application
4 | import io.realm.Realm
5 | import io.realm.RealmConfiguration
6 |
7 | /**
8 | * Created by jack on 2017/7/21.
9 | */
10 | class MyTodoApplication : Application() {
11 | override fun onCreate() {
12 | super.onCreate()
13 |
14 | val config = RealmConfiguration.Builder(this)
15 | .name("realm.my_todos")// 库文件名
16 | .encryptionKey(getKey()) // 加密
17 | .schemaVersion(1) // 版本号
18 | .deleteRealmIfMigrationNeeded()
19 | .build()
20 |
21 | // 非持久化的、存在于内存中的 Realm 实例
22 | // RealmConfiguration myConfig = new RealmConfiguration.Builder()
23 | // .name("realm.my_todos")
24 | // .inMemory()
25 | // .build();
26 |
27 | Realm.setDefaultConfiguration(config)// 设置默认 RealmConfiguration
28 | // 配合 Configuration 使用
29 | // Realm.deleteRealm(config); // 每次执行清除数据
30 | }
31 |
32 | /**
33 | * 64 bits
34 | * @return
35 | */
36 | private fun getKey(): ByteArray {
37 | return byteArrayOf(0, 1, 2, 3, 4, 3, 2, 1, 0, 1, 2, 3, 4, 3, 2, 1, 0, 1, 2, 3, 4, 3, 2, 1, 0, 1, 2, 3, 4, 3, 2, 1, 0, 1, 2, 3, 4, 3, 2, 1, 0, 1, 2, 3, 4, 3, 2, 1, 0, 1, 2, 3, 4, 3, 2, 1, 0, 1, 2, 3, 4, 3, 2, 1)
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
15 |
16 |
22 |
23 |
24 |
25 |
26 |
27 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/app/src/main/kotlin/com/easy/kotlin/mytodoapplication/TodoAdapter.kt:
--------------------------------------------------------------------------------
1 | package com.easy.kotlin.mytodoapplication
2 |
3 | //import butterknife.bindView
4 | import android.content.Context
5 | import android.graphics.Color
6 | import android.view.View
7 | import android.view.ViewGroup
8 | import android.widget.TextView
9 | import butterknife.BindView
10 | import butterknife.ButterKnife
11 | import com.easy.kotlin.mytodoapplication.model.Todo
12 | import io.realm.RealmBasedRecyclerViewAdapter
13 | import io.realm.RealmResults
14 | import io.realm.RealmViewHolder
15 |
16 |
17 | /**
18 | * Created by jack on 2017/7/21.
19 | */
20 |
21 | class TodoAdapter(context: Context,
22 | realmResults: RealmResults,
23 | automaticUpdate: Boolean,
24 | animateResults: Boolean,
25 | private val clickListener: TodoItemClickListener) :
26 |
27 | RealmBasedRecyclerViewAdapter(
28 | context,
29 | realmResults,
30 | automaticUpdate,
31 | animateResults) {
32 |
33 | override fun onCreateRealmViewHolder(viewGroup: ViewGroup, viewType: Int): ViewHolder {
34 | val v = inflater.inflate(R.layout.todo_item, viewGroup, false)
35 | return ViewHolder(v, clickListener)
36 | }
37 |
38 | override fun onBindRealmViewHolder(viewHolder: ViewHolder, position: Int) {
39 | val todo = realmResults[position]
40 |
41 | viewHolder.todoTitle.setText(todo.title)
42 | viewHolder.todoTitle.fontFeatureSettings = "font-size:12px"
43 | viewHolder.todoTitle.setTextColor(Color.argb(255, 69, 106, 124))
44 |
45 | viewHolder.todoContent.setText(todo.content)
46 | }
47 |
48 | interface TodoItemClickListener {
49 | fun onClick(caller: View, todo: Todo)
50 | }
51 |
52 | inner class ViewHolder(view: View, private val clickListener: TodoItemClickListener?) :
53 | RealmViewHolder(view), View.OnClickListener {
54 |
55 | // Bind a field to the view for the specified ID. The view will automatically be cast to the field type
56 | @BindView(R.id.todo_item_todo_title)
57 | lateinit var todoTitle: TextView
58 | // val todoTitle: TextView by bindView(R.id.todo_item_todo_title)
59 | @BindView(R.id.todo_item_todo_content)
60 | lateinit var todoContent: TextView
61 | // val todoContent: TextView by bindView(R.id.todo_item_todo_content)
62 |
63 | init {
64 | // Bind annotated fields and methods
65 | ButterKnife.bind(this, view)
66 | view.setOnClickListener(this)
67 | }
68 |
69 | override fun onClick(v: View) {
70 | clickListener?.onClick(v, realmResults[adapterPosition])
71 | }
72 | }
73 |
74 |
75 | }
76 |
--------------------------------------------------------------------------------
/app/src/main/kotlin/com/easy/kotlin/mytodoapplication/TodosFragment.kt:
--------------------------------------------------------------------------------
1 | package com.easy.kotlin.mytodoapplication
2 |
3 | import android.os.Bundle
4 | import android.support.v4.app.Fragment
5 | import android.util.Log
6 | import android.view.LayoutInflater
7 | import android.view.View
8 | import android.view.ViewGroup
9 | import butterknife.BindView
10 | import butterknife.ButterKnife
11 | import co.moonmonkeylabs.realmrecyclerview.RealmRecyclerView
12 | import com.easy.kotlin.mytodoapplication.TAG.MY_TAG
13 | import com.easy.kotlin.mytodoapplication.model.Todo
14 | import io.realm.Realm
15 |
16 | /**
17 | * Created by jack on 2017/7/22.
18 | */
19 |
20 |
21 | class TodosFragment : Fragment(), TodoAdapter.TodoItemClickListener {
22 |
23 | // protected val realmRecyclerView: RealmRecyclerView by bindView(R.id.todos_recycler_view)
24 | // var realmRecyclerView: RealmRecyclerView? = null // var 可变 View
25 | @BindView(R.id.todos_recycler_view)
26 | lateinit var realmRecyclerView: RealmRecyclerView
27 |
28 | private var realm: Realm? = null
29 |
30 | override fun onCreateView(inflater: LayoutInflater?,
31 | container: ViewGroup?,
32 | savedInstanceState: Bundle?): View? {
33 |
34 | val v = inflater!!.inflate(R.layout.fragment_todos, container, false)
35 | ButterKnife.bind(this, v)
36 | return v
37 | }
38 |
39 | override fun onActivityCreated(savedInstanceState: Bundle?) {
40 | super.onActivityCreated(savedInstanceState)
41 | realm = Realm.getDefaultInstance()
42 | }
43 |
44 | override fun onResume() {
45 | super.onResume()
46 | val todos = realm!!.where(Todo::class.java).findAll()
47 | Log.i(MY_TAG, "onResume: ${todos}")
48 | //realmRecyclerView = find(R.id.todos_recycler_view) // 可变 View
49 | Log.i(MY_TAG, "onResume: realmRecyclerView = ${realmRecyclerView} ")
50 | val adapter = TodoAdapter(activity, todos, true, true, this)
51 | realmRecyclerView.setAdapter(adapter)
52 | }
53 |
54 | override fun onDestroy() {
55 | super.onDestroy()
56 | realm!!.close()
57 | }
58 |
59 | // 点击事项,跳转编辑 EditFragment
60 | override fun onClick(caller: View, todo: Todo) {
61 | (activity as MainActivity).hideFab()
62 |
63 | val todoEditFragment = TodoEditFragment.newInstance(todo.id)
64 | activity.supportFragmentManager
65 | .beginTransaction()
66 | .replace(R.id.content_main, todoEditFragment, todoEditFragment.javaClass.getSimpleName())
67 | .addToBackStack(todoEditFragment.javaClass.getSimpleName())
68 | .commit()
69 | }
70 |
71 | companion object {
72 | fun newInstance(): TodosFragment {
73 | return TodosFragment()
74 | }
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 | apply plugin: 'kotlin-android'
3 | apply plugin: 'kotlin-kapt'
4 |
5 | android {
6 | compileSdkVersion 25
7 | buildToolsVersion "25.0.3"
8 |
9 | defaultConfig {
10 | applicationId "com.easy.kotlin.mytodoapplication"
11 | minSdkVersion 21
12 | targetSdkVersion 25
13 | versionCode 1
14 | versionName "1.0"
15 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
16 | }
17 |
18 | buildTypes {
19 | release {
20 | minifyEnabled false
21 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
22 | }
23 | }
24 |
25 | sourceSets {
26 | // += 添加Java的编译路径, 在main中创建kotlin文件夹, 用于存放kotlin代码
27 | main.java.srcDirs += 'src/main/kotlin'
28 | }
29 | }
30 |
31 | //构建脚本
32 | buildscript {
33 | ext.kotlin_version = '1.1.3'
34 | repositories {
35 | mavenCentral()
36 | }
37 | dependencies {
38 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
39 | }
40 | }
41 |
42 | repositories {
43 | mavenCentral()
44 | maven { url "https://jitpack.io" }
45 | maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
46 | }
47 |
48 | dependencies {
49 | compile fileTree(dir: 'libs', include: ['*.jar'])
50 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
51 | exclude group: 'com.android.support', module: 'support-annotations'
52 | })
53 | compile 'com.android.support:appcompat-v7:25.3.1'
54 | compile 'com.android.support.constraint:constraint-layout:1.0.2'
55 | compile 'com.android.support:design:25.3.1'
56 | testCompile 'junit:junit:4.12'
57 |
58 | // Kotlin
59 | compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
60 |
61 | // Anko
62 | compile 'org.jetbrains.anko:anko-sdk15:0.8.2' // sdk19, sdk21, sdk23 are also available
63 | compile 'org.jetbrains.anko:anko-support-v4:0.8.2' // In case you need support-v4 bindings
64 | compile 'org.jetbrains.anko:anko-appcompat-v7:0.8.2' // For appcompat-v7 bindings
65 |
66 | // realm:跨平台移动数据库引擎
67 | compile 'io.realm:realm-android:0.87.1'
68 | // kotlin使用realm的注解处理依赖库
69 | kapt "io.realm:realm-annotations:0.87.1"
70 | kapt "io.realm:realm-annotations-processor:0.87.1"
71 |
72 | compile 'com.github.thorbenprimke:realm-recyclerview:0.9.12' // 在jitpack.io上
73 |
74 | // Butter Knife,专门为Android View设计的绑定注解,专业解决各种findViewById
75 | // compile 'com.jakewharton:butterknife:7.0.1'
76 | // compile 'com.jakewharton:kotterknife:0.1.0-SNAPSHOT'
77 | compile 'com.jakewharton:butterknife:8.7.0'
78 | annotationProcessor 'com.jakewharton:butterknife-compiler:8.7.0'
79 | kapt 'com.jakewharton:butterknife-compiler:8.7.0'
80 | compile 'com.android.support:support-v4:25.3.1'
81 |
82 | }
83 |
--------------------------------------------------------------------------------
/app/src/main/kotlin/com/easy/kotlin/mytodoapplication/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.easy.kotlin.mytodoapplication
2 |
3 | import android.os.Bundle
4 | import android.support.design.widget.FloatingActionButton
5 | import android.support.v7.app.AppCompatActivity
6 | import android.support.v7.widget.Toolbar
7 | import android.view.Menu
8 | import android.view.MenuItem
9 | import android.view.View
10 |
11 | //import butterknife.bindView
12 |
13 | class MainActivity : AppCompatActivity() {
14 |
15 | // val fab: FloatingActionButton by bindView(R.id.fab)
16 | var fab: FloatingActionButton? = null
17 |
18 | override fun onCreate(savedInstanceState: Bundle?) {
19 | super.onCreate(savedInstanceState)
20 | setContentView(R.layout.activity_main)
21 |
22 | val toolbar = findViewById(R.id.toolbar) as Toolbar
23 | setSupportActionBar(toolbar)
24 |
25 | fab = findViewById(R.id.fab) as FloatingActionButton
26 |
27 |
28 | // 添加日程事件
29 | fab?.setOnClickListener { _ ->
30 | // Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG).setAction("Action", null).show()
31 | val todoEditFragment = TodoEditFragment()
32 | getSupportFragmentManager()
33 | .beginTransaction()
34 | .replace(R.id.content_main, todoEditFragment, todoEditFragment.javaClass.getSimpleName())
35 | .addToBackStack(todoEditFragment.javaClass.getSimpleName())
36 | .commit()
37 |
38 | hideFab()
39 | }
40 |
41 | // 待办事项 List
42 | val fragment = TodosFragment.newInstance()
43 |
44 | supportFragmentManager.beginTransaction()
45 | .replace(R.id.content_main, fragment, fragment::class.java.getSimpleName())
46 | .commit()
47 |
48 | supportFragmentManager.addOnBackStackChangedListener {
49 | val backStackCount = supportFragmentManager.backStackEntryCount
50 | if (backStackCount == 0) {
51 | showFab()
52 | }
53 | }
54 | }
55 |
56 | override fun onCreateOptionsMenu(menu: Menu): Boolean {
57 | // Inflate the menu; this adds items to the action bar if it is present.
58 | menuInflater.inflate(R.menu.menu_main, menu)
59 | return true
60 | }
61 |
62 | override fun onOptionsItemSelected(item: MenuItem): Boolean {
63 | // Handle actio1n bar item clicks here. The action bar will
64 | // automatically handle clicks on the Home/Up button, so long
65 | // as you specify a parent activity in AndroidManifest.xml.
66 | val id = item.itemId
67 | if (id == R.id.action_settings) {
68 | return true
69 | }
70 |
71 | return super.onOptionsItemSelected(item)
72 | }
73 |
74 | fun hideFab() {
75 | fab?.visibility = View.GONE
76 | }
77 |
78 | fun showFab() {
79 | fab?.visibility = View.VISIBLE
80 | }
81 |
82 | }
83 |
--------------------------------------------------------------------------------
/app/src/main/kotlin/com/easy/kotlin/mytodoapplication/TodoEditFragment.kt:
--------------------------------------------------------------------------------
1 | package com.easy.kotlin.mytodoapplication
2 |
3 | import android.graphics.Color
4 | import android.os.Bundle
5 | import android.support.v4.app.Fragment
6 | import android.view.LayoutInflater
7 | import android.view.View
8 | import android.view.ViewGroup
9 | import android.widget.Button
10 | import android.widget.EditText
11 | import com.easy.kotlin.mytodoapplication.model.Todo
12 | import io.realm.Realm
13 | import org.jetbrains.anko.*
14 | import org.jetbrains.anko.support.v4.UI
15 | import org.jetbrains.anko.support.v4.find
16 | import java.util.*
17 |
18 |
19 | class TodoEditFragment : Fragment() {
20 | val realm: Realm = Realm.getDefaultInstance()
21 | var todo: Todo? = null
22 |
23 | companion object {
24 | val TODO_ID_KEY: String = "todo_id_key"
25 |
26 | fun newInstance(id: String): TodoEditFragment {
27 | var args: Bundle = Bundle()
28 | args.putString(TODO_ID_KEY, id)
29 | var todoEditFragment: TodoEditFragment = newInstance()
30 | todoEditFragment.arguments = args
31 | return todoEditFragment
32 | }
33 |
34 | fun newInstance(): TodoEditFragment {
35 | return TodoEditFragment()
36 | }
37 | }
38 |
39 | override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
40 | return UI {
41 | // AnkoContext
42 |
43 | verticalLayout {
44 | padding = dip(30)
45 | var title = editText {
46 | // editText 视图
47 | id = R.id.todo_title
48 | hintResource = R.string.title_hint
49 | }
50 |
51 | var content = editText {
52 | id = R.id.todo_content
53 | height = 400
54 | hintResource = R.string.content_hint
55 | }
56 | button {
57 | // button 视图
58 | id = R.id.todo_add
59 | textResource = R.string.add_todo
60 | textColor = Color.WHITE
61 | setBackgroundColor(Color.DKGRAY)
62 | onClick { _ -> createTodoFrom(title, content) }
63 | }
64 | }
65 | }.view
66 | }
67 |
68 | override fun onActivityCreated(savedInstanceState: Bundle?) {
69 | super.onActivityCreated(savedInstanceState)
70 |
71 | if (arguments != null && arguments.containsKey(TODO_ID_KEY)) {
72 |
73 | val todoId = arguments.getString(TODO_ID_KEY)
74 | todo = realm.where(Todo::class.java).equalTo("id", todoId).findFirst()
75 |
76 | val todoTitle = find(R.id.todo_title)
77 | todoTitle.setText(todo?.title)
78 |
79 | val todoContent = find(R.id.todo_content)
80 | todoContent.setText(todo?.content)
81 |
82 | val add = find