Code Monkey home page Code Monkey logo

kotlinandroidworkshop's People

Contributors

bng86 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

kotlinandroidworkshop's Issues

練功場 part3 sample code

請注意這只是練功場示範過程中的產物,實際上的專案是不會這樣寫的,貼上來給大家方便邊回憶或邊搭配影片,自己動
手寫比較會有記憶喔!

package com.cardinalblue.myapplication

import android.content.Context
import android.net.ConnectivityManager
import android.os.Bundle
import android.util.Log
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.android.material.snackbar.Snackbar
import androidx.appcompat.app.AppCompatActivity
import android.view.Menu
import android.view.MenuItem
import androidx.core.content.ContextCompat
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.fragment_first.*

const val TAG = "MainActivity"

class MainActivity : AppCompatActivity() {
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val color = ContextCompat.getColor(this, R.color.colorAccent)
        text.setTextColor( color)

        val manager = this.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val defaultNetworkActive = manager.getActiveNetworkInfo()
        Log.i(TAG, "onCreate: ${defaultNetworkActive?.extraInfo}")
    }

    fun replaceFragment() {
        val beginTransaction = supportFragmentManager.beginTransaction()
        beginTransaction.add(R.id.container, BlankFragment.newInstance("Yanbin"))
        beginTransaction.commit()
    }

    override fun onRestart() {
        super.onRestart()
        Log.i(TAG, "onRestart: ")
    }

    override fun onResume() {
        super.onResume()
        Log.i(TAG, "onResume: ")
    }

    override fun onStart() {
        super.onStart()
        Log.i(TAG, "onStart: ")
    }

    override fun onPause() {
        super.onPause()
        Log.i(TAG, "onPause: ")
    }

    override fun onStop() {
        super.onStop()
        Log.i(TAG, "onStop: ")
    }

    override fun onDestroy() {
        Log.i(TAG, "onDestroy: ")
        super.onDestroy()
    }
    
}
package com.cardinalblue.myapplication

import androidx.lifecycle.ViewModelProviders
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

class BlankFragment() : Fragment() {

    companion object {
        // Should only have default constructor !!!!!
        fun newInstance(name: String): Fragment {
            val bundle = Bundle()
            bundle.putString("name", name)
            return BlankFragment().apply {
                this.arguments = bundle
            }
        }

        fun newInstance2(id: Int): Fragment {
            val bundle = Bundle()
            bundle.putInt("id", id)
            return BlankFragment().apply {
                this.arguments = bundle
            }
        }
    }


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.blank_fragment, container, false)
    }
}

練功場 part3 - Activity and Fragment samples

請注意這只是練功場示範過程中的產物,實際上的專案是不會這樣寫的,貼上來給大家方便邊回憶或邊搭配影片,自己動
手寫比較會有記憶喔!

package com.cardinalblue.myapplication

import android.content.Context
import android.net.ConnectivityManager
import android.os.Bundle
import android.util.Log
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.android.material.snackbar.Snackbar
import androidx.appcompat.app.AppCompatActivity
import android.view.Menu
import android.view.MenuItem
import androidx.core.content.ContextCompat
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.fragment_first.*

const val TAG = "MainActivity"

class MainActivity : AppCompatActivity() {
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val color = ContextCompat.getColor(this, R.color.colorAccent)
        text.setTextColor( color)

        val manager = this.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val defaultNetworkActive = manager.getActiveNetworkInfo()
        Log.i(TAG, "onCreate: ${defaultNetworkActive?.extraInfo}")
    }

    fun replaceFragment() {
        val beginTransaction = supportFragmentManager.beginTransaction()
        beginTransaction.add(R.id.container, BlankFragment.newInstance("Yanbin"))
        beginTransaction.commit()
    }

    override fun onRestart() {
        super.onRestart()
        Log.i(TAG, "onRestart: ")
    }

    override fun onResume() {
        super.onResume()
        Log.i(TAG, "onResume: ")
    }

    override fun onStart() {
        super.onStart()
        Log.i(TAG, "onStart: ")
    }

    override fun onPause() {
        super.onPause()
        Log.i(TAG, "onPause: ")
    }

    override fun onStop() {
        super.onStop()
        Log.i(TAG, "onStop: ")
    }

    override fun onDestroy() {
        Log.i(TAG, "onDestroy: ")
        super.onDestroy()
    }
    
}
package com.cardinalblue.myapplication

import androidx.lifecycle.ViewModelProviders
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

class BlankFragment() : Fragment() {

    companion object {
        // Should only have default constructor !!!!!
        fun newInstance(name: String): Fragment {
            val bundle = Bundle()
            bundle.putString("name", name)
            return BlankFragment().apply {
                this.arguments = bundle
            }
        }

        fun newInstance2(id: Int): Fragment {
            val bundle = Bundle()
            bundle.putInt("id", id)
            return BlankFragment().apply {
                this.arguments = bundle
            }
        }
    }


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.blank_fragment, container, false)
    }
}

練功場 part 2 - Layout 程式碼範例

示範

xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="105dp"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/image"
        android:layout_width="68dp"
        android:layout_height="68dp"
        android:layout_marginStart="20dp"
        android:layout_marginTop="20dp"
        android:background="@color/colorAccent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textTitle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginEnd="20dp"
        android:ellipsize="end"
        android:maxLines="1"
        android:text="Challeng01"
        android:textColor="#000000"
        android:textSize="22sp"
        app:layout_constraintVertical_chainStyle="packed"
        app:layout_constraintBottom_toTopOf="@+id/textSubTitle"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/image"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textSubTitle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:maxLines="2"
        android:text="XX of total XXfjafkljsla"
        android:textColor="#7C828A"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@id/textTitle"
        app:layout_constraintStart_toEndOf="@id/image"
        app:layout_constraintStart_toStartOf="@id/textTitle"
        app:layout_constraintTop_toBottomOf="@+id/textTitle" />

</androidx.constraintlayout.widget.ConstraintLayout>

參考來源:

截圖 2020-08-13 上午12 16 33

練習:

xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="#333340"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:id="@+id/textTitle"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="23dp"
        android:layout_marginStart="23dp"
        android:layout_marginEnd="18dp"
        app:layout_constraintEnd_toStartOf="@id/chart"
        android:maxLines="1"
        android:textSize="22sp"
        android:textColor="#FFFFFF"
        android:text="Chart Title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/textSub"
        app:layout_constraintTop_toBottomOf="@id/textTitle"
        app:layout_constraintStart_toStartOf="@id/textTitle"
        android:layout_marginTop="4dp"
        android:textColor="#A6FFFFFF"
        app:layout_constraintEnd_toEndOf="@id/textTitle"
        android:text="Additional text goes here"
        android:layout_width="0dp"
        android:layout_height="wrap_content"/>

    <ImageView
        android:id="@+id/chart"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:background="@color/colorAccent"
        android:layout_marginEnd="19dp"
        android:layout_width="76dp"
        android:layout_height="37dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

參考來源:

截圖 2020-08-13 上午12 18 24

練功場 part 5 - CustomLifeCycle

CustomLifeCycle

    fun lifeCycleTest() {
        val viewModel = TodoViewModel()
        val lifeCycleOwner = MyLifeCycleOwner()

        viewModel.todoLiveData.observe(lifeCycleOwner, Observer { todos ->
            Log.i("anotherTest", "$todos ")
        })

        lifeCycleOwner.start()

        Log.i("anotherTest", "end of test")
    }

    class MyLifeCycleOwner: LifecycleOwner {

        private val lifecycleRegistry = LifecycleRegistry(this)

        init {
            lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE)
        }

        fun start() {
            lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_RESUME)
        }

        override fun getLifecycle(): Lifecycle {
            return lifecycleRegistry
        }
    }

LiveData 與 LifeCycleOwner 之間的關係可以看這份文件,請特別注意 STARTED 跟 DESTROYED 的部分

https://developer.android.com/reference/android/arch/lifecycle/LiveData#observe

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.