AndroidStudio:RecyclerView の item で setOnClickListener を使いたい。

今回は recyclerView を使ったときの item 全体にボタンイベントを設置したい。

inflate するための view

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:descendantFocusability="blocksDescendants"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <com.google.android.material.textview.MaterialTextView
        android:id="@+id/categoryName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/category"
        android:textSize="20sp"
        app:layout_constraintHorizontal_weight="7"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/categoryEditButton"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.google.android.material.button.MaterialButton
        android:id="@+id/categoryEditButton"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/edit"
        app:layout_constraintHorizontal_weight="3"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

見た目は上の画像
一応 ConstraintLayout 内の

android:descendantFocusability="blocksDescendants"

を使って親の ItemView を押せるようにしているが、いらないかもしれない。

ViewHolder(categoryName はなくてもよいかも)

import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class CategoryViewHolder(item:View):RecyclerView.ViewHolder(item) {
    val categoryName:TextView = item.findViewById(R.id.categoryName)
}

categoryName などの view を全部含んだ view に setOnClickListener を当てたいのでクラスだけあればいいかも

Adapter

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView.OnItemLongClickListener
import androidx.recyclerview.widget.RecyclerView


class CategoryAdapter(private val list:List<なんかのclass>):RecyclerView.Adapter<CategoryViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CategoryViewHolder {
        val itemView = LayoutInflater.from(parent.context).inflate(R.layout.item_category,parent,false)
        return CategoryViewHolder(itemView)
    }

    override fun onBindViewHolder(holder: CategoryViewHolder, position: Int) {
        holder.categoryName.text = list[position].category
        holder.itemView.setOnClickListener(){
            Log.i("item clicked","pos")
        }
        holder.itemView.setOnLongClickListener(){
            Log.i("item long clicked","pos")
            true
        }
    }

    override fun getItemCount(): Int {
        return list.size
    }
}

itemView (今回の主役)が categoryName などを含んでいる親の View になる。

import 関係は削っているのでいい感じにする必要があるかも。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA