Upgrade to Pro — share decks privately, control downloads, hide ads and more …

イケてるAndroid開発を完全に理解した__.pdf

entaku
April 25, 2019

 イケてるAndroid開発を完全に理解した__.pdf

entaku

April 25, 2019
Tweet

More Decks by entaku

Other Decks in Technology

Transcript

  1. ๻ͷϨϕϧ • ͔ͳΓiOSΑΓ • Ұ͔ΒαΫοͱ࡞ΕΔ(͸ͣ) • RxSwiftͰMVVMͷಋೖ͸ܦݧ͋Δ • ͔͠͠ •

    Android։ൃ(Kotlin)͸࠷ۙ͋Μ·Γ΍ͬͯͳ͍ • ؆୯ͳมߋ͸Ͱ͖Δ͚Ͳ
  2. JPT "OESPJE 7JFXͷදࣔϩδοΫ $POUSPMMFS "DUJWJUZ'SBHNFOUͷؔ܎ੑ͸$POUBJOFS7JFXͱ ෳ਺ͷ7JFX$POUSPMMFSͰ࣮ݱ "DUJWJUZ'SBHNFOU ༧Ί༻ҙ͞Ε͍ͯΔ 7JFXͷදࣔϩδοΫ $POUSPMMFS

    EFMFHBUFEBUBTPVSDF ෦෼͕ͦΕʹ౰ͨΔ͸ͣ "EBQUFS 7JFXͱ.PEFMΛܨ͙ ΋ͷ 7JFX.PEFM 3YͳͲͷϥΠϒϥϦ͕ඞཁɻઃܭ͕ΦϨΦϨ "OESPJE7JFX.PEFM ެࣜͰఏڙ %BUBCJOEJOH $POUSPMMFSFUD 3YͳͲͷϥΠϒϥϦ͕ඞཁɻ $POUSPMMFSPSEBUBTPVSDFΛผϑΝΠϧͰࢦఆ͠ ͦ͜ͰCJOEͷ಺༰Λॻ͘ %BUB#JOEJOH6UJM ެࣜͰఏڙ  ࣗಈੜ੒͞ΕΜͷ͕ΩϞ ͍ .PEFM "1*$MJFOUͰୟ͍ͨ݁ՌΛఆٛͨ͠ߏ଄ମ TUSVDU ʹ֨ೲ "MBNPpSF"1*,JUͳͲ "1*$MJFOUͰୟ͍ͨ݁ՌΛ ఆٛͨ͠ߏ଄ମ EBUB DMBTT ʹ֨ೲ 3FUSP'JU 7JFX 4UPSU#PBSEYJCFUD ๻͸4UPSU#PBSE޷͖Ͱ͢ YNM YNM͏Β΍·͍͠ iOS/Android։ൃͬ͘͟Γൺֱ
  3. ViewͷදࣔϩδοΫ - Lifecycle Lifecycle-aware components perform actions in response to

    a change in the lifecycle status of another component, such as activities and fragments. These components help you produce better- organized, and often lighter-weight code, that is easier to maintain. ϥΠϑαΠΫϧରԠίϯϙʔωϯτ͸ɺΞΫςΟϏςΟ΍ϑϥάϝϯτͳͲɺଞͷίϯϙʔωϯτͷϥΠϑ αΠΫϧεςʔλεͷมԽʹԠͯ͡ΞΫγϣϯΛ࣮ߦ͠·͢ɻ͜ΕΒͷίϯϙʔωϯτΛ࢖༻͢Δͱɺ੔ཧ ͕༰қͰɺΑΓܰྔͳίʔυΛ؆୯ʹ࡞੒Ͱ͖·͢ɻ
  4. ViewͷදࣔϩδοΫ override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) if (savedInstanceState

    == null) { //ϓϩδΣΫτҰཡͷFragment val fragment = ProjectListFragment() supportFragmentManager .beginTransaction() .add(R.id.fragment_container, fragment, TAG_OF_PROJECT_LIST_FRAGMENT) .commit() } } J04Ͱ͍͏Կͳͷ͔͸ Θ͔ΒΜ 㶦꒳㶦 ŝžŕ Activity͸FragmentΛݺͼग़͢
  5. ViewͷදࣔϩδοΫ override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View?

    { //dataBinding༻ͷϨΠΞ΢τϦιʔεΛηοτ binding = DataBindingUtil.inflate(inflater, R.layout.fragment_project_list, container, false) //ΠϕϯτͷcallbackΛadapterʹ఻ୡ projectAdapter = ProjectAdapter() //্هadapterΛreclclerViewʹద༻ requireNotNull(binding).projectList.adapter = projectAdapter //Loading։࢝ requireNotNull(binding).isLoading = true //rootViewΛऔಘ return requireNotNull(binding).root } override fun onActivityCreated(savedInstanceState: Bundle?) { super.onActivityCreated(savedInstanceState) val viewModel = ViewModelProviders.of(this).get(ProjectListViewModel::class.java) //؂ࢹΛ։࢝ observeViewModel(viewModel) } J04Ͱ͍͏ $POUSPMMFS Έ͍ͨͳ MVVM͔ͩΒFragment͸ViewModel΍Adapter΁ड͚౉͢͠Δ͚ͩ
  6. ViewͷදࣔϩδοΫ - Adapter An Adapter object acts as a bridge

    between an AdapterView and the underlying data for that view. The Adapter provides access to the data items. The Adapter is also responsible for making a View for each item in the data set. AdapterΦϒδΣΫτ͸ɺAdapterViewͱͦͷϏϡʔͷجʹͳΔσʔλͱͷؒͷϒϦοδͱͯ͠ػೳ͠· ͢ɻΞμϓλ͸σʔλ߲໨΁ͷΞΫηεΛఏڙ͠·͢ɻΞμϓλ͸ɺσʔληοτ಺ͷ֤߲໨ʹରͯ͠Ϗϡʔ Λ࡞੒͢Δ੹೚΋͋Γ·͢ɻ JPT։ൃͰ͍͏ UBCMFWJFXͱ͔ʹ౉͢ EBUBTPVSDFͩ
  7. ViewͷදࣔϩδοΫ - Adapter & DataBinding 3Y4XJGUͰ΋ಉ͡ϊϦͰ CJOEJOH͢ΔΑ // σʔλΛset͢Δ fun

    setProjectList(projectList: List<Team>) { this.projectList = projectList result.dispatchUpdatesTo(this) } } // σʔλΛview಺ͷitemʹ౰ͯ͸ΊΔ override fun onCreateViewHolder(parent: ViewGroup, viewtype: Int): ProjectViewHolder { val binding = DataBindingUtil.inflate( LayoutInflater.from(parent.context), R.layout.project_list_item, parent, false) as ProjectListItemBinding //ࣗಈੜ੒͞ΕͨDataBinding༻ͷjavaclass return ProjectViewHolder(binding) }
  8. ViewModel The ViewModel class is designed to store and manage

    UI-related data in a lifecycle conscious way. The ViewModel class allows data to survive configuration changes such as screen rotations. The Android framework manages the lifecycles of UI controllers, such as activities and fragments. The framework may decide to destroy or re-create a UI controller in response to certain user actions or device events that are completely out of your control. ViewModelΫϥε͸ɺϥΠϑαΠΫϧΛҙࣝͨ͠ํ๏ͰUIؔ࿈ͷσʔλΛ֨ೲ͓Αͼ؅ཧ͢ΔΑ͏ʹઃܭ ͞Ε͍ͯ·͢ɻ ViewModelΫϥεΛ࢖༻͢ΔͱɺσʔλΛը໘ͷճసͳͲͷߏ੒มߋʹ଱͑Δ͜ͱ͕Ͱ͖ ·͢ɻ 3Y4XJGUͰ΍ͬͨ΍ͭͩ ͳʂ
  9. ViewModel class ProjectListViewModel(application: Application) : AndroidViewModel(application) { val projectListObservable: LiveData<List<Team>>

    init { // APIͷ݁ՌΛObservable΁ projectListObservable = FootballRepository.instance.getProjectList() } } 3Y4XJGUͰ ΋ࢥ͚ͬͨͲ γϯϓϧͳߏ੒ͳΒ 7JFX.PEFM͍Βͳ͍͔΋Ͷ σʔλݺͼग़ͯ͠Observable(؂ࢹͰ͖ΔΑ͏ʹ)͢Δʂ
  10. View <?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android"> <data> <variable name="user" type="com.example.User"/>

    </data> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{user.firstName}"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{user.lastName}"/> </LinearLayout> </layout> J04Ͱ͍͏ DPVTUPN7JFXʹ.PEFM౉͢ ϊϦ
  11. Model public class User { private final String firstName; private

    final String lastName; public User(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } public String getFirstName() { return this.firstName; } public String getLastName() { return this.lastName; } } .PEFMͷ࡞Γํ͸ͦΜͳ มΘΜͳ͍ͳ