Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

영화 목록 출력 #22

Open
wants to merge 17 commits into
base: handnew04
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.example.toyproject.ui.main

import android.os.Bundle
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.GridLayoutManager
import com.example.toyproject.R
import com.example.toyproject.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
lateinit var binding: ActivityMainBinding
lateinit var adapter: MainMoviePostingRecyclerAdapter
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adapter는 context가 필요하지 않아서 굳이 lateinit로 선언하지 않아도 될것같네요.
실수로 adapter가 초기화되기 전에 사용하면 NPE가 발생할 수 있기 때문에 선언과 동시에 초기화하는 편이 좋을 것 같습니다 ~

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c04fe6d
그 부분은 생각하지 못했는데, 감사합니다!

val mainViewModel: MainViewModel by viewModels()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(
this, R.layout.activity_main
)

initailize()
initBinding()
test()
}

private fun initailize() {
adapter = MainMoviePostingRecyclerAdapter()
binding.rcvPoster.layoutManager = GridLayoutManager(this@MainActivity, 3)
}

private fun initBinding() {
binding.vm = mainViewModel
binding.lifecycleOwner = this@MainActivity
binding.rcvPoster.adapter = adapter
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

동일한 표현이지만 한 객체의 여러 프로퍼티를 초기화한다면 apply도 사용 가능합니다

        binding.apply {
            viewModel = mainViewModel
            lifecycleOwner = this@MainActivity
            rcvPoster.adapter = adapter
        }

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c04fe6d
앗 감사합니다!

}

private fun test() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기선 test 보다는 초기화, 데이터 로딩, 시작 등의 의미가 느껴지는 네이밍을 사용한다면 역할이 더 잘 와닿을거 같습니다 ㅎㅎ

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c04fe6d
수정했습니다. 감사합니다!

mainViewModel.searchMovie()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.example.toyproject.ui.main

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.example.toyproject.R
import com.example.toyproject.data.entity.SearchMovieData
import com.example.toyproject.databinding.ItemMainBinding

class MainMoviePostingRecyclerAdapter :
RecyclerView.Adapter<MainMoviePostingRecyclerAdapter.ViewHolder>() {
private val movies: ArrayList<SearchMovieData> = ArrayList()

fun setItemList(movies: ArrayList<SearchMovieData>) {
this.movies.clear()
this.movies.addAll(movies)
notifyDataSetChanged()
}

override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): ViewHolder {
val binding = DataBindingUtil.inflate<ItemMainBinding>(
LayoutInflater.from(parent.context),
R.layout.item_main,
parent,
false
)
return ViewHolder(binding)
}

override fun getItemCount(): Int {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

= 로 더 간결하게 표현이 가능합니다!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c04fe6d
감사합니다. 계속 return 을 쓰네요.. ㅠㅠ

return movies.size
}

override fun onBindViewHolder(
holder: ViewHolder,
position: Int
) {
val item = movies[position]
holder.bind(item)
}


inner class ViewHolder(val binding: ItemMainBinding) : RecyclerView.ViewHolder(binding.root) {
init {
binding.root.setOnClickListener({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dataBinding을 사용하시니까 xml에서 직접 clickListener를 사용하는 것이 어떨까요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3b18433
감사합니다 ㅜㅜ!

// TODO: 2020-03-06 clickListener
})
}

fun bind(movieData: SearchMovieData) {
binding.movieData = movieData
}
}
}