RustAssistant/app/src/main/java/com/coldmint/rust/pro/SearchActivity.kt

84 lines
3.7 KiB
Kotlin
Raw Normal View History

2022-04-04 14:03:10 +00:00
package com.coldmint.rust.pro
2022-07-09 16:33:10 +00:00
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
2022-04-04 14:03:10 +00:00
import android.os.Bundle
2022-07-09 16:33:10 +00:00
import androidx.appcompat.widget.SearchView
2022-04-04 14:03:10 +00:00
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
2022-07-09 16:33:10 +00:00
import com.coldmint.rust.core.dataBean.SearchSuggestionsData
2022-04-04 14:03:10 +00:00
import com.coldmint.rust.core.interfaces.ApiCallBack
import com.coldmint.rust.core.web.Search
2022-07-09 16:33:10 +00:00
import com.coldmint.rust.pro.adapters.SearchSuggestionsAdapter
2022-04-04 14:03:10 +00:00
import com.coldmint.rust.pro.base.BaseActivity
import com.coldmint.rust.pro.databinding.ActivitySearchBinding
/**
2022-07-09 16:33:10 +00:00
* 搜索界面
2022-04-04 14:03:10 +00:00
*/
class SearchActivity : BaseActivity<ActivitySearchBinding>() {
override fun whenCreateActivity(savedInstanceState: Bundle?, canUseView: Boolean) {
2022-07-09 16:33:10 +00:00
title = getString(R.string.search)
setReturnButton()
viewBinding.recyclerView.layoutManager = LinearLayoutManager(this)
2022-04-04 14:03:10 +00:00
2022-07-09 16:33:10 +00:00
viewBinding.searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener,
android.widget.SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
if (query != null && query.isNotBlank()) {
val intent = Intent(this@SearchActivity, SearchResultActivity::class.java)
intent.putExtra("key", query)
startActivity(intent)
2022-04-04 14:03:10 +00:00
}
2022-07-09 16:33:10 +00:00
return true
2022-04-04 14:03:10 +00:00
}
2022-07-09 16:33:10 +00:00
override fun onQueryTextChange(newText: String?): Boolean {
if (newText != null && newText.isNotBlank()) {
Search.instance.suggestions(newText,
object : ApiCallBack<SearchSuggestionsData> {
override fun onResponse(t: SearchSuggestionsData) {
val dataList = t.data
if (dataList.isNullOrEmpty()) {
viewBinding.recyclerView.isVisible = false
} else {
val adapter =
SearchSuggestionsAdapter(
this@SearchActivity,
newText,
dataList
)
adapter.setItemEvent { i, itemSearchSuggestionsBinding, viewHolder, s ->
itemSearchSuggestionsBinding.root.setOnClickListener {
val intent = Intent(
this@SearchActivity,
SearchResultActivity::class.java
)
intent.putExtra("key", s)
startActivity(intent)
}
}
viewBinding.recyclerView.adapter = adapter
viewBinding.recyclerView.isVisible = true
}
}
override fun onFailure(e: Exception) {
viewBinding.recyclerView.isVisible = false
}
})
} else {
viewBinding.recyclerView.isVisible = false
}
return true
2022-04-04 14:03:10 +00:00
}
})
}
override fun getViewBindingObject(): ActivitySearchBinding {
return ActivitySearchBinding.inflate(layoutInflater)
}
2022-07-09 16:33:10 +00:00
2022-04-04 14:03:10 +00:00
}