-
Notifications
You must be signed in to change notification settings - Fork 9
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
base: handnew04
Are you sure you want to change the base?
영화 목록 출력 #22
Changes from 1 commit
bfa8270
f7e7548
3a1810a
34e7087
e174844
46a946a
8ddd99b
15754e2
ccc2a2b
c37aaab
8ab4e5e
10c37b6
cce7801
c04fe6d
3b18433
bc145e2
55dbfed
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 동일한 표현이지만 한 객체의 여러 프로퍼티를 초기화한다면 apply도 사용 가능합니다
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. c04fe6d |
||
} | ||
|
||
private fun test() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기선 test 보다는 초기화, 데이터 로딩, 시작 등의 의미가 느껴지는 네이밍을 사용한다면 역할이 더 잘 와닿을거 같습니다 ㅎㅎ There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. = 로 더 간결하게 표현이 가능합니다! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. c04fe6d |
||
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({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dataBinding을 사용하시니까 xml에서 직접 clickListener를 사용하는 것이 어떨까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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가 발생할 수 있기 때문에 선언과 동시에 초기화하는 편이 좋을 것 같습니다 ~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
c04fe6d
그 부분은 생각하지 못했는데, 감사합니다!