android - 在 RecyclerView Item android 上实现 Click Listener
问题描述
我为我的项目的 recyclerview 编写了以下代码。我想在这个 recyclerview 的一个项目上实现 clicklistener。你能指导我在我的代码中找到问题吗?
片段主页.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:background="@color/colorWhite"
tools:context=".view.ui.HomeFragment">
<include
android:id="@+id/toolbarHome"
layout="@layout/layout_toolbar" />
<androidx.appcompat.widget.SearchView
android:id="@+id/svSearchCountry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/_15sdp"
android:layout_marginTop="@dimen/_15sdp"
android:layout_marginEnd="@dimen/_15sdp"
android:background="@drawable/layout_rectangle_edittext"
android:textCursorDrawable="@null"
app:iconifiedByDefault="false"
app:layout_constraintTop_toBottomOf="@id/toolbarHome"
app:queryBackground="@null"
app:queryHint="Search Country" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvCountries"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginStart="@dimen/_5sdp"
android:layout_marginTop="@dimen/_15sdp"
android:layout_marginEnd="@dimen/_5sdp"
android:layout_marginBottom="@dimen/_5sdp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/svSearchCountry" />
</androidx.constraintlayout.widget.ConstraintLayout>
layout_country_item.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"
android:layout_width="match_parent"
android:id="@+id/countryItemParent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/ivCountry"
android:layout_width="match_parent"
android:layout_height="0dp"
android:padding="@dimen/_5sdp"
android:layout_marginTop="@dimen/_5sdp"
android:src="@drawable/kenya"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tvCountryName"
style="@style/CustomTextViewMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kenya"
android:textColor="@color/colorText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/ivCountry" />
</androidx.constraintlayout.widget.ConstraintLayout>
HomeFragment.kt
class HomeFragment : Fragment() {
private lateinit var listener: CountriesAdapter.CountriesAdapterListener
private lateinit var countriesAdapter: CountriesAdapter
lateinit var toolbar: Toolbar
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_home, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
toolbar = view.findViewById(R.id.toolbar) as Toolbar
setupUI()
setupListeners()
setupRecyclerView()
}
private fun setupListeners() {
listener = object : CountriesAdapter.CountriesAdapterListener {
override fun onClickedItem() {
Log.d("onItemClickedCalled","Working")
startActivity(Intent(requireActivity(), HomeActivity::class.java))
}
}
svSearchCountry.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
return false
}
override fun onQueryTextChange(newText: String?): Boolean {
countriesAdapter.filter.filter(newText)
return false
}
})
}
private fun setupRecyclerView() {
countriesAdapter =
CountriesAdapter(
listener,
CountriesModel.countriesList as ArrayList<CountriesModel>
)
rvCountries.apply {
layoutManager = GridLayoutManager(context, 4, GridLayoutManager.VERTICAL, false)
adapter = countriesAdapter
}
}
private fun setupUI() {
toolbar.title = "Home"
val searchIcon = svSearchCountry.findViewById<ImageView>(R.id.search_mag_icon)
searchIcon.setColorFilter(R.color.colorPrimary)
val cancelIcon = svSearchCountry.findViewById<ImageView>(R.id.search_close_btn)
cancelIcon.setColorFilter(R.color.colorText)
val textView = svSearchCountry.findViewById<TextView>(R.id.search_src_text)
textView.setTextColor(ContextCompat.getColor(activity as MainActivity, R.color.colorText))
}
}
国家适配器.kt
class CountriesAdapter(
private val listener: CountriesAdapterListener, var countriesList: ArrayList<CountriesModel>
) : RecyclerView.Adapter<CountriesAdapter.CountriesViewHolder>(), Filterable {
var countryFilterList = ArrayList<CountriesModel>()
init {
countryFilterList = countriesList
}
interface CountriesAdapterListener {
fun onClickedItem()
}
inner class CountriesViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val ivCountryImage: ImageView = itemView.findViewById(R.id.ivCountry)
val tvCountryTitle: TextView = itemView.findViewById(R.id.tvCountryName)
val countryItemParent: ConstraintLayout = itemView.findViewById(R.id.countryItemParent)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CountriesViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.layout_country_item, parent, false)
return CountriesViewHolder(view)
}
override fun onBindViewHolder(holder: CountriesViewHolder, position: Int) {
val countryItem = countryFilterList[position]
Glide.with(holder.ivCountryImage.context).load(countryItem.imageURI)
.into(holder.ivCountryImage)
holder.tvCountryTitle.text = countryItem.title
if (position%4==0){
holder.itemView.visibility = View.INVISIBLE
}
holder.countryItemParent.setOnClickListener {
listener.onClickedItem()
}
}
override fun getItemCount(): Int {
return countryFilterList.size
}
override fun getFilter(): Filter {
return object : Filter() {
override fun performFiltering(constraint: CharSequence?): FilterResults {
val charSearch = constraint.toString()
countryFilterList = if (charSearch.isEmpty()) {
countriesList
} else {
val resultList = ArrayList<CountriesModel>()
for (row in countriesList) {
if (row.title.toLowerCase(Locale.ROOT)
.contains(charSearch.toLowerCase(Locale.ROOT))
) {
resultList.add(row)
}
}
resultList
}
val filterResults = FilterResults()
filterResults.values = countryFilterList
return filterResults
}
@Suppress("UNCHECKED_CAST")
override fun publishResults(constraint: CharSequence?, results: FilterResults?) {
countryFilterList = results?.values as ArrayList<CountriesModel>
notifyDataSetChanged()
}
}
}
}
请帮帮我,我被困在这里,不知道如何解决这个问题。
解决方案
改变这个
holder.countryItemParent.setOnClickListener {
listener.onClickedItem()
}
有了这个
holder.itemView.setOnClickListener {
listener.onClickedItem()
}
推荐阅读
- r - 更改 navbarPage 元素中的背景颜色和字体功能
- laravel - Laravel 和 Passport 获取 SQLSTATE [42S22]:找不到列:1054 未知列 'api_token'
- reactjs - 为什么这个 setState 调用不会触发渲染?
- c++ - 知道在 C++ 中生成的特定二进制文件是由哪个编译器决定的吗?
- react-native - React Native 返回响应错误代码 500
- android - 您需要为您的 APK 或 Android App Bundle 使用不同的版本代码,因为您已经有一个版本代码为 1
- c# - 从使用 ItextSharp 或 PDFSharp 创建的扁平化 PDF 中提取文本
- python - 如果我使用在 Chrome 中单击后禁用按钮的 jquery 功能,为什么表单输入提交按钮不发送请求?
- javascript - 如何使用玩笑模拟 FileReader
- elasticsearch - 更新 Elasticsearch 文档中的字段