Skip to content

Instantly share code, notes, and snippets.

<layout... >
<data>
<variable
name="viewModel"
type="com.azaless.cheerball.viewmodels.TeamDetailViewModel" />
</data>
...
class TeamDetailViewModel(private val cheerBallDataRepository: CheerBallDataRepository,
private val teamId: Int) : ViewModel() {
private val players = MutableLiveData<List<Player>>()
private val compositeDisposable: CompositeDisposable = CompositeDisposable()
val adapter = MutableLiveData<PlayerListAdapter>()
val layoutManager = MutableLiveData<RecyclerView.LayoutManager>()
fun getPlayers(): LiveData<List<Player>> {
return players
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android" >
<data>
<variable
name="player"
type="com.azaless.cheerball.view.model.Player" />
</data>
class PlayerListAdapter : DataBindingAdapter<List<Player>, PlayerListAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val binding = DataBindingUtil.inflate<ListItemPlayerBinding>(layoutInflater, R.layout.list_item_player, parent, false)
return ViewHolder(binding)
}
override fun getItemCount() = values?.size ?: 0
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
class GroupBallItemAdapter : RecyclerView.Adapter<GroupBallItemAdapter.ViewHolder>(){
var values: List<GroupDetail> = listOf()
set(items) {
field = items
notifyDataSetChanged()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(LayoutInflater.from(parent.context)
.inflate(R.layout.list_item_team_small, parent, false))
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
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="wrap_content">
<LinearLayout
var values: List<GroupDetail> = listOf()
set(items) {
field = items
notifyDataSetChanged()
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
with(holder) {
value.keys.filterIndexed { index, _ -> index == position }
.firstOrNull()?.let { key ->
class TeamDetailViewModel(private val cheerBallDataRepository: CheerBallDataRepository,
private val teamId: Int) : ViewModel() {
private val flagURL = MutableLiveData<String>()
private val team = MutableLiveData<Team>()
private val players = MutableLiveData<List<Player>>()
private val compositeDisposable: CompositeDisposable = CompositeDisposable()
val teamName = MutableLiveData<String>()
val adapter = MutableLiveData<PlayerListAdapter>()
val layoutManager = MutableLiveData<RecyclerView.LayoutManager>()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// factory คือ การสร้าง model โดยมีการโยนค่าอื่นๆเข้าไป
val factory = InjectorUtils.provideTeamViewModelFactory(context!!, teamId, teamName)
teamDetailViewModel = ViewModelProviders.of(this, factory).get(TeamDetailViewModel::class.java)
viewDataBinding.viewModel = teamDetailViewModel
teamDetailViewModel.adapter.value = PlayerListAdapter()