Skip to content

Commit

Permalink
代码优化:20210701提交代码引入bug,导致搜索界面“搜索”和“取消”功能无效
Browse files Browse the repository at this point in the history
  • Loading branch information
lelelongwang committed Jul 6, 2021
1 parent be9200a commit 4608890
Showing 1 changed file with 6 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.longjunhao.wanjetpack.ui.home

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.lifecycle.lifecycleScope
Expand Down Expand Up @@ -45,6 +45,7 @@ class SearchFragment : Fragment() {
binding.articleList.adapter = articleAdapter.withLoadStateFooter(
FooterAdapter(articleAdapter::retry)
)
subscribeUi()

return binding.root
}
Expand All @@ -65,7 +66,7 @@ class SearchFragment : Fragment() {

binding.search.setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
subscribeUi()
articleSearch()
}
true
}
Expand All @@ -75,9 +76,11 @@ class SearchFragment : Fragment() {
// 但是如果按照下面的方案,通过监听viewModel.keyword来解决的话,这样在搜索框中输入文本的过程中也会实时网络请
// 求,会频繁的网络请求,感觉也不太好。故暂时注掉代码
/*viewModel.keyword.observe(viewLifecycleOwner, Observer {
subscribeUi(adapter)
articleSearch()
})*/
}

private fun articleSearch() {
homeJob?.cancel()
homeJob = lifecycleScope.launch {
viewModel.getSearchArticle().collectLatest {
Expand Down

0 comments on commit 4608890

Please sign in to comment.