我在search_header_layout.xml文件中使用了三个imageview和一个edittext。用于设计目的的第一个imageview。第二个imageview id为clear\u search\u query,用于清除edittext(edittext.settext(“”),第三个imageview id为voice\u search\u query,用于在recyclerview中使用voice search。我的edittext id是search\u edit\u text这是为在recyclerview中使用搜索而设计的。
heare是我的search_header_layout.xml文件代码:-
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/search_card_view"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_marginHorizontal="12dp"
android:layout_marginVertical="8dp"
app:cardCornerRadius="12dp"
app:cardElevation="16dp"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/search_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:src="@drawable/ic_search"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="ContentDescription" />
<ImageView
android:id="@+id/clear_search_query"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:background="?attr/selectableItemBackground"
android:padding="8dp"
android:src="@drawable/ic_clear"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="ContentDescription" />
<ImageView
android:id="@+id/voice_search_query"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:background="?attr/selectableItemBackground"
android:padding="8dp"
android:src="@drawable/ic_mic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="ContentDescription" />
<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/search_edit_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="24dp"
android:background="@android:color/transparent"
android:hint="@string/search_articles_hint"
android:inputType="text"
android:paddingTop="8dp"
android:textAppearance="?attr/textAppearanceHeadline6"
app:layout_constraintBottom_toBottomOf="@id/search_image_view"
app:layout_constraintEnd_toStartOf="@id/voice_search_query"
app:layout_constraintStart_toEndOf="@id/search_image_view" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
heare是我的activity_main.xml文件代码:-
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<include
android:id="@+id/search_box_container"
layout="@layout/search_header_layout" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/search_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="8dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/search_box_container"
tools:itemCount="12"
tools:listitem="@layout/item_search" />
<TextView
android:id="@+id/no_search_results_found_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/no_search_results_found"
android:textAppearance="?attr/textAppearanceHeadline4"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="@id/search_list"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/search_list" />
</androidx.constraintlayout.widget.ConstraintLayout>
我发现kotlin代码的用途相同,但我不理解kotlin,我需要使用java
以下是mainactivity.kt代码:-
package com.developersbreach.simplesearchapp
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.speech.RecognizerIntent
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.AppCompatEditText
import androidx.core.widget.doOnTextChanged
import androidx.recyclerview.widget.RecyclerView
import java.util.*
import kotlin.collections.ArrayList
class MainActivity : AppCompatActivity(), SearchAdapter.SportsAdapterListener {
private lateinit var recyclerView: RecyclerView
private lateinit var searchAdapter: SearchAdapter
private lateinit var editText: AppCompatEditText
private lateinit var noSearchResultsFoundText: TextView
private lateinit var sportsList: List<Sports>
private lateinit var clearQueryImageView: ImageView
private lateinit var voiceSearchImageView: ImageView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
recyclerView = findViewById(R.id.search_list)
noSearchResultsFoundText = findViewById(R.id.no_search_results_found_text)
editText = findViewById(R.id.search_edit_text)
voiceSearchImageView = findViewById(R.id.voice_search_query)
clearQueryImageView = findViewById(R.id.clear_search_query)
sportsList = sportsList(applicationContext)
attachAdapter(sportsList)
editText.doOnTextChanged { text, _, _, _ ->
val query = text.toString().toLowerCase(Locale.getDefault())
filterWithQuery(query)
toggleImageView(query)
}
voiceSearchImageView.setOnClickListener {
val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH).apply {
putExtra(
RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM
)
}
startActivityForResult(intent, SPEECH_REQUEST_CODE)
}
clearQueryImageView.setOnClickListener {
editText.setText("")
}
}
private fun attachAdapter(list: List<Sports>) {
searchAdapter = SearchAdapter(list, this)
recyclerView.adapter = searchAdapter
}
private fun filterWithQuery(query: String) {
if (query.isNotEmpty()) {
val filteredList: List<Sports> = onFilterChanged(query)
attachAdapter(filteredList)
toggleRecyclerView(filteredList)
} else if (query.isEmpty()) {
attachAdapter(sportsList)
}
}
private fun onFilterChanged(filterQuery: String): List<Sports> {
val filteredList = ArrayList<Sports>()
for (currentSport in sportsList) {
if (currentSport.title.toLowerCase(Locale.getDefault()).contains(filterQuery)) {
filteredList.add(currentSport)
}
}
return filteredList
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == SPEECH_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
val spokenText: String? =
data?.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS).let { results ->
results?.get(0)
}
// Do something with spokenText
editText.setText(spokenText)
}
super.onActivityResult(requestCode, resultCode, data)
}
private fun toggleRecyclerView(sportsList: List<Sports>) {
if (sportsList.isEmpty()) {
recyclerView.visibility = View.INVISIBLE
noSearchResultsFoundText.visibility = View.VISIBLE
} else {
recyclerView.visibility = View.VISIBLE
noSearchResultsFoundText.visibility = View.INVISIBLE
}
}
private fun toggleImageView(query: String) {
if (query.isNotEmpty()) {
clearQueryImageView.visibility = View.VISIBLE
voiceSearchImageView.visibility = View.INVISIBLE
} else if (query.isEmpty()) {
clearQueryImageView.visibility = View.INVISIBLE
voiceSearchImageView.visibility = View.VISIBLE
}
}
override fun onSportSelected(sports: Sports?) {
val intent = Intent(applicationContext, DetailActivity::class.java)
intent.putExtra("DETAIL_SPORTS_DATA", sports)
startActivity(intent)
}
companion object {
const val SPEECH_REQUEST_CODE = 0
}
}
请帮助我使用edittext和voice搜索在recyclerview中进行搜索。
暂无答案!
目前还没有任何答案,快来回答吧!