티스토리 뷰

RecyclerView +  ItemClickListener  +  ItemLongClickListener


 

build.gradle (Module: app)

dependencies {
    // RecyclerView
    implementation 'androidx.recyclerview:recyclerview:1.1.0'
    // CardView
    implementation 'androidx.cardview:cardview:1.0.0'
}

 

 

 

 

 

 

cardview_item.xml

<?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="100dp"
    android:layout_margin="10dp"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        android:background="#BBBBBB"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent">

        <TextView
            android:text="혀가 길지 않은 개발자"
            android:textSize="26dp"
            android:textStyle="bold|italic"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />


        <TextView
            android:id="@+id/tvPosition"
            android:hint="1"
            android:textSize="22dp"
            android:textStyle="bold|italic"
            android:layout_marginTop="5dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

cardview_item.xml

 

 

 

 

 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:listitem="@layout/cardview_item" />

</LinearLayout>

activity_main.xml

 

 

 

 

 

 

MyAdapter.kt

package com.jwsoft.kotlinproject

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.cardview_item.view.*

class MyAdapter(var items: ArrayList<Int>,
                onClick: OnClickInterface,
                onLongClick: OnLongClickInterface
) : RecyclerView.Adapter<MyAdapter.MyViewHolder>() {

    interface OnClickInterface {
        fun onClick(itemView: View, position: Int)
    }

    interface OnLongClickInterface {
        fun onLongClick(itemView: View, position: Int)
    }

    var onClickInterface: OnClickInterface = onClick
    var onLongClickInterface: OnLongClickInterface = onLongClick

    inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        fun bind(position: Int) {
            itemView.tvPosition.text = position.toString()

            itemView.setOnClickListener {
                onClickInterface.onClick(itemView, position)
            }
            itemView.setOnLongClickListener {
                onLongClickInterface.onLongClick(itemView, position)
                true
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val context = parent.context
        val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        val view = inflater.inflate(R.layout.cardview_item, parent, false)

        return MyViewHolder(view)
    }

    override fun getItemCount(): Int = items.size

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.bind(position)
    }

}

 

 

 

 

 

 

 

MainActivity.kt

package com.jwsoft.kotlinproject

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.cardview_item.view.*

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        var obj = object: MyAdapter.OnClickInterface {
            override fun onClick(itemView: View, position: Int) {
                itemView.tvPosition.text = "Short Click $position"
            }
        }
        var obj2 = object: MyAdapter.OnLongClickInterface {
            override fun onLongClick(itemView: View, position: Int) {
                itemView.tvPosition.text = "Long Click $position"
            }
        }

        var itemClickInterface: MyAdapter.OnClickInterface = obj
        var itemLongClickInterface: MyAdapter.OnLongClickInterface = obj2

        var items: ArrayList<Int> = ArrayList()
        for (i in 0..10) {
            items.add(i)
        }

        var adapter = MyAdapter(items, itemClickInterface, itemLongClickInterface)
        recyclerView.adapter = adapter
        recyclerView.layoutManager = LinearLayoutManager(applicationContext, RecyclerView.VERTICAL, false)
        recyclerView.setHasFixedSize(true)
    }

}

실행 결과

 

 

 

'Android > Kotlin' 카테고리의 다른 글

[Kotlin]  Coroutine (1)  (0) 2020.07.23
[Kotlin]  MVVM  (0) 2020.07.21
[Kotlin]  AlertDialog  (0) 2020.07.21
[Kotlin]  Room  (0) 2020.07.21
[Kotlin]  SQLite  +  Singleton Pattern  (0) 2020.07.18
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함