Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 获取视图和视图模型以使用公共协议进行通信_Android_Kotlin_Viewmodel - Fatal编程技术网

Android 获取视图和视图模型以使用公共协议进行通信

Android 获取视图和视图模型以使用公共协议进行通信,android,kotlin,viewmodel,Android,Kotlin,Viewmodel,我试图告诉视图,它需要使用LiveData导航到两个目的地之一: class IntroFragment : Fragment() { private lateinit var introViewModel : IntroViewModel override fun onCreateView( inflater : LayoutInflater, container : ViewGroup?,

我试图告诉视图,它需要使用LiveData导航到两个目的地之一:

class IntroFragment : Fragment()
{
    private lateinit var introViewModel : IntroViewModel

    override fun onCreateView( inflater : LayoutInflater,
                               container : ViewGroup?,
                               savedInstanceState : Bundle?): View? 
    {
        introViewModel = ViewModelProviders.of( this ).get( IntroViewModel::class.java )
        return inflater.inflate( R.layout.fragment_Intro, container, false )
    }

    override

    fun onViewCreated( view: View, savedInstanceState: Bundle? )
    {
        introViewModel.navigateToSection1.observe(this, Observer {
            Navigation.findNavController( view ).navigate( R.id.action_showSection1 )
        });

        introViewModel.navigateToSection2.observe(this, Observer {
            Navigation.findNavController( view ).navigate( R.id.action_showSection2 )
        });
    }
}
简介模型:

class IntroViewModel : ViewModel()
{
    private val _navigateToSection1 = MutableLiveData<Boolean>()
    val navigateToNotificationSettings: LiveData<Boolean>
        get() = _navigateToSection1

    private val _navigateToSection2 = MutableLiveData<Boolean>()
    val navigateToSection2: LiveData<Boolean>
        get() = _navigateToSection2

    init
    {
        _navigateToSection1 = true
    }
}
class IntroViewModel : ViewModel()
{
    private val _navigateTo = MutableLiveData<IntroProtocol>()
    val navigateTo: LiveData<IntroProtocol>
        get() = _navigateTo

    init
    {
        _navigateTo = IntroProtocol.navigateToSection1() // I get "Unresolved reference navigateToSection1"
    }
}
然后将interfaces方法作为参数传递回视图,如下所示:

class IntroFragment : Fragment(), IntroProtocol
{
    private lateinit var introViewModel : IntroViewModel

    override fun onCreateView( inflater           : LayoutInflater,
                               container          : ViewGroup?,
                               savedInstanceState : Bundle?         ): View?
    {
        introViewModel = ViewModelProviders.of( this ).get( IntroViewModel::class.java )
        return inflater.inflate( R.layout.fragment_Intro, container, false )
    }


    override fun onViewCreated( view: View, savedInstanceState: Bundle? )
    {
        introViewModel.navigateTo.observe( this, Observer {
            callLocalMethodByName( this ) // not sure how call the local method by name dynamically
        });
    }


    override fun navigateToSection1()
    {
        Navigation.findNavController( view ).navigate( R.id.action_showSection1 )
    }


    override fun navigateToSection2()
    {
        Navigation.findNavController( view ).navigate( R.id.action_showSection2 )
    }
}
_navigateToSection1.value = true
简介模型:

class IntroViewModel : ViewModel()
{
    private val _navigateToSection1 = MutableLiveData<Boolean>()
    val navigateToNotificationSettings: LiveData<Boolean>
        get() = _navigateToSection1

    private val _navigateToSection2 = MutableLiveData<Boolean>()
    val navigateToSection2: LiveData<Boolean>
        get() = _navigateToSection2

    init
    {
        _navigateToSection1 = true
    }
}
class IntroViewModel : ViewModel()
{
    private val _navigateTo = MutableLiveData<IntroProtocol>()
    val navigateTo: LiveData<IntroProtocol>
        get() = _navigateTo

    init
    {
        _navigateTo = IntroProtocol.navigateToSection1() // I get "Unresolved reference navigateToSection1"
    }
}

但我不知道该怎么做,如果可能的话。我希望传递interfaces方法的原因是,如果有一个干净的定义/协议,ViewModel知道它可以调用哪些方法,并且视图将继承这些方法,这将非常方便,从而产生良好的结构。这可能吗?如果不需要,还有什么替代方案?

我想说您不需要该接口,但要为您的可变LiveData提供一个值,您需要这样做:

class IntroFragment : Fragment(), IntroProtocol
{
    private lateinit var introViewModel : IntroViewModel

    override fun onCreateView( inflater           : LayoutInflater,
                               container          : ViewGroup?,
                               savedInstanceState : Bundle?         ): View?
    {
        introViewModel = ViewModelProviders.of( this ).get( IntroViewModel::class.java )
        return inflater.inflate( R.layout.fragment_Intro, container, false )
    }


    override fun onViewCreated( view: View, savedInstanceState: Bundle? )
    {
        introViewModel.navigateTo.observe( this, Observer {
            callLocalMethodByName( this ) // not sure how call the local method by name dynamically
        });
    }


    override fun navigateToSection1()
    {
        Navigation.findNavController( view ).navigate( R.id.action_showSection1 )
    }


    override fun navigateToSection2()
    {
        Navigation.findNavController( view ).navigate( R.id.action_showSection2 )
    }
}
_navigateToSection1.value = true