SearchView with multiple fragments using viewpager in android

前端 未结 3 1769
死守一世寂寞
死守一世寂寞 2021-02-02 16:06

I want to implement SearchView with multiple fragments present in a viewPager. All fragemnts contains lists and I want to filter those lists and create a new ListView which has

3条回答
  •  孤街浪徒
    2021-02-02 16:48

    If You wanna search your query to all fragment must use Viewmodel with Livedata. Set Your Query in Livedata then Your Livedata Observe on all Your Fragment.

    This Code in viewPager side

    val searchLiveData = MutableLiveData()
    searchLiveData.postValue("query")
    

    This code in all Fragments

    mainViewModel.searchLiveData.observe(viewLifecycleOwner, Observer { query ->
         //Add your code
    })
    
    //1
    val liveData: MutableLiveData()
    //2
    liveData.value = "Hello"
    //3
    liveData.observe(this, Observer { 
      // Update UI  
    })
    

提交回复
热议问题