- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个包含两个子 fragment 的 Activity Timeline
和 Milestones
。这两个 fragment 都包含由自定义 Cursor 适配器填充的 ListView
这是一个图形表示:
现在,当我在 TIMELINE 上并打开搜索 View 时,我输入的内容都很好,我得到了想要的结果。但是,当我使用 searchview 中的一些文本
从时间轴导航到里程碑时,搜索 View 不会被清除,因此我也在里程碑页面上根据我在时间轴中提供的参数获得过滤结果。
我正在使用 AppCompact 库来开发我的 ActionBar。那里的选项卡不是 ActionBar 选项卡,而是简单的 SlidingTabLayout
。
到目前为止我已经尝试过使用
getActivity().supportInvalidateOptionsMenu();在两个 fragment 的 onResume() 中,不起作用。
我已经尝试过 searchView.setQuery("",false) - 不起作用并且随机给我一个 NPE。
那么我在这里想念什么?
最佳答案
您可以看一下我的示例,我在其中展示了如何控制 fragment 之间的 searchView。
首先。您需要创建 BaseFragment,它与 appBarLayout 的 Activity 上下文一起工作。
open class BaseFragment: Fragment() {
lateinit var rootActivity: MainActivity
lateinit var appBarLayout: AppBarLayout
lateinit var searchView: androidx.appcompat.widget.SearchView
override fun onAttach(context: Context) {
super.onAttach(context)
this.rootActivity = context as MainActivity
appBarLayout = rootActivity.findViewById(R.id.app_bar_layout)
searchView = rootActivity.findViewById(R.id.search_input)
}
override fun onResume() {
super.onResume()
resetAppBarLayout()
}
private fun resetAppBarLayout() {
appBarLayout.elevation = 14f
}
fun setupSearch(query: String) {
searchView.visibility = View.VISIBLE
searchView.clearFocus()
when(query.isNotEmpty()) {
true -> {
searchView.setQuery(query, true)
searchView.isIconified = false
}
false -> {
searchView.isIconified = true
searchView.isIconified = true
}
}
}
fun hideSearchKeyboard() {
context?.let {
KeyboardHelper.hideSearchKeyboard(it, searchView.findViewById(R.id.search_src_text))
}
}
fun hideSearch() {
searchView.visibility = View.GONE
searchView.clearFocus()
}
}
其次。从 BaseFragment 继承您的 fragment ,覆盖 onResume() 方法并通过调用 BaseFragment 的方法控制 fragment 中的 searchView。像这样。
class FragmentA : BaseFragment() {
private var searchQuery = ""
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val root = inflater.inflate(R.layout.fragment, container, false)
val textView: TextView = root.findViewById(R.id.textView)
textView.text = "Fragment A"
return root
}
override fun onResume() {
super.onResume()
setupSearch()
}
private fun setupSearch() {
searchView.setOnQueryTextListener(object : androidx.appcompat.widget.SearchView.OnQueryTextListener {
override fun onQueryTextChange(newText: String?): Boolean {
when(newText.isNullOrEmpty()) {
true -> searchQuery = ""
false -> searchQuery = newText
}
return true
}
override fun onQueryTextSubmit(query: String?): Boolean {
hideSearchKeyboard()
return true
}
})
super.setupSearch(searchQuery)
}
}
您可以在此处找到完整示例 https://github.com/yellow-cap/android-manage-searchview
关于android - 清除导航上的 Searchview 文本。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29625618/
我的 Activity 的主要内容区域中有一个 SearchView。 我需要覆盖我的 style.xml 文件中的 SearchView 样式(这样我就可以删除 SearchView 的下划线 -
我正在使用 Firebase-UI 的 FirebaseRecyclerAdapter。我无法在 SearchView 中实现过滤器。 我的数据库: 我的问题: 如何使用 FirebaseRecycl
我正在尝试以编程方式删除 SearchView 提示中的图标所占用的空间,但由于某种原因它不会消失。 此代码使图标不可见,但不会删除空格: try { Field mDrawa
我想在用户未输入任何内容时在 SearchView 中显示一些默认建议。我正在使用矩阵光标手动设置我的自定义建议适配器。我尝试在 onFocusChange 和 onClickListner 中设置适
如何删除或更改编辑文本中的搜索 View 图标?我正在使用 Appcompat 库。 我使用下面的代码修改和删除但它不起作用: SearchManager searchManager = (Searc
当我启动我的应用程序时,会引发以下错误: E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.app, PID: 5513 jav
在我的应用程序中使用 SearchView。 无论如何我可以延迟对 onQueryTextChange() 方法的调用。例如,当用户键入一系列字符时,他必须等待此方法被调用。 此等待应该不取决于键入的
我正在尝试从 Fragment 中的工具栏获取我的 SearchView。 我在 onCreateOptionsMenu 中引入了一个菜单项。 问题:searchItem.getActionView(
我不确定为什么会收到此错误。这是有问题的菜单: 这是根据 developer guide 的可搜索配置. 添加到我的 list 文件中: 我在新的搜索 Activity 中也有一个
我是一名开发 Android 应用程序的新手,以下是该应用程序的第一个屏幕。我希望我的应用程序使用该搜索对话框执行搜索。 为此,我关注了http://developer.android.com/gui
我正在 CustomAdapter 上执行 SearchView,它将在其中搜索门名称。在我尝试运行该应用程序后,它突然崩溃并给我如上所述的错误。我不知道我什至做错了哪一部分导致我每次尝试运行我的应用
您好,我需要在 searchView 中放置一些文本并聚焦/展开 searchView 小部件。 这是我尝试过的,但它不起作用 @Override public boolean onCreateOpt
所以当我遇到错误“无法解析类 SearchView”时,我试图在我的应用程序中包含一个搜索栏。谷歌没有帮助,所以我来到这里。这是我的代码: 其余的 xml 已正确完成,我直接从 android 获取
我的搜索 View 有问题。 我有 2 个主题,当我第一次接触 searchview 时,我得到了丑陋的第一张图片。但是当我再次进入搜索 View 时,我从第二张图片中得到了很好的主题。 我总是想走第
我使用的不是 menuItem 而是布局中的搜索 View 。我支持的最低 API 级别是 9,搜索 View 是 11。 现在我这样创建了我的搜索 View : 在直线布局中。我注意到警告,所以
我在 here 中解决了我的问题之后当我在搜索框中输入文本时,搜索过滤器仍然不起作用。 这是我的代码: package intikom.streammobile.Customer; import an
我正在创建类似于屏幕上的 Activity 。我在工具栏中阅读了许多关于 SearchView 的教程,并在我的 Activity 中实现了它并且它有效(上面的代码)。但是如何制作这个下拉列表呢?感谢
我按照 Android 的官方说明在工具栏中实现了 SearchView。 SearchView 本身工作得很好,但是当我点击搜索图标时,它会向左移动,而不是显示搜索提示和关闭按钮,尽管如果我单击这个
Android Studio 3.2.1 在我的app / build.gradle中: android { compileSdkVersion 28 defaultConfig {
我有一个 RecylerView,它有 SearchView 小部件和包含复选框和玩家名称的项目列表。当我滚动项目并选择项目复选框时没有问题。但是,当我通过名称搜索项目然后选择它们时,就会出现问题,当
我是一名优秀的程序员,十分优秀!