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

Moster 04 display selected data items #4

Merged
merged 6 commits into from
Apr 19, 2020

Conversation

AnelCC
Copy link
Owner

@AnelCC AnelCC commented Apr 18, 2020

4. Moster display selected data items

  1. Handle RecyclerView click events
   override fun onMonsterItemClick(monster: Monster) {
        viewModel.selectedMonster.value = monster
        Log.i(LOG_TAG, "Selected monster value: ${viewModel.selectedMonster.value}")
        navController.navigate(R.id.action_mainFragment_to_detailFragment)
    }
  1. Create and navigate to a detail fragment
DetailFragment.kl

detail_fragment_1 copy

  1. Add an Up button to a secondary fragment
   (requireActivity() as AppCompatActivity).run {
            supportActionBar?.setDisplayHomeAsUpEnabled(true)
        }
        setHasOptionsMenu(true)

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == android.R.id.home) {
            navController.navigateUp()
        }
        return super.onOptionsItemSelected(item)
    }

detail_fragmet

  1. Pass data in a shared ViewModel
    SharedViewModel is used in MainFragment and DetailFragment to share selectedMonster mutable data.
    Update selectedMonster from MainFragment
 viewModel.selectedMonster.value = monster

Instance the viewModel in DetailFragment

viewModel = ViewModelProvider(requireActivity()).get(SharedViewModel::class.java)

5. Display details with data binding

Gradle settings

  dataBinding {
        enabled = true
    }

Update the layout: instance a variable ViewModel

<layout
    ...
    <data>
    <variable
        name="viewModel"
        type="com.anelcc.monster.ui.share.SharedViewModel"/>
</layout> 

Bind info: apply viewModel in layout

<TextView
            android:id="@+id/nameText"
            android:text="@{viewModel.selectedMonster.name}"/>
<TextView
            android:text="@{viewModel.selectedMonster.description}"/>
<TextView
            android:rating="@{viewModel.selectedMonster.scariness}"/>

detail_fragment_databinding_1 copy detail_fragment_databinding_2 copy

  1. Customize data bindings with an adapter
    Create BindingAdapters.kt and link the data with the attribute name.

BindingAdapters.kt

@BindingAdapter("imageUrl")
fun loadImage(view: ImageView, imageUrl: String) {
}

@BindingAdapter("price")
fun itemPrice(view: TextView, value: Double) {
} 

fragment_detail.xml

app:imageUrl="@{viewModel.selectedMonster.imageUrl}"
app:price="@{viewModel.selectedMonster.price}"

@BindingAdapter("imageUrl") ....................... @BindingAdapter("price")
detail_fragment_databinding_without_price copy detail_fragment_databinding_with_price copy

@AnelCC AnelCC merged commit f883ce4 into master Apr 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant