Я прочитал решения здесь , но в моем случае я хотел бы получить navHostFragment, а затем перейти к следующему пункту назначения в действии. Я хочу переместить метод onClick в ViewController.
НЕКОТОРЫЙ ДОСТУПНЫЙ КОД:
class SimpleViewModel : ViewModel() {
// private set // This is to prevent external modification of the variable.
private val _flowStepNumberArg = MutableLiveData(1)
val flowStepNumberArg: LiveData<Int> = _flowStepNumberArg
fun onFragOneButtonPressed() {
_flowStepNumberArg.value = 2
}
fun onClickFriend() {
Log.d("BindingAdapters", "BUTTON CLICKED")
}
}
class HomeFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.findViewById<Button>(R.id.navigate_action_button)?.setOnClickListener {
val flowStepNumberArg = 1
val action = HomeFragmentDirections.nextAction(flowStepNumberArg)
findNavController().navigate(action)
}
}
//in fragment_layout.xml::
<data>
<variable
name="viewmodel"
type="com.example.android.codelabs.navigation.data.SimpleViewModel" />
</data>
<Button
android:id="@+id/next_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/next_step_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text"
android:onClick="@{() -> viewmodel.onClickFriend2()}"
/>