[안드로이드/코틀린] Custom Adapter

박의진·2022년 9월 10일
0

1. custom_item.xml 생성

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/custom_item_type_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/custom_item_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/custom_item_type_image"
        android:layout_marginLeft="16dp"/>
    <TextView
        android:id="@+id/custom_item_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/custom_item_title"
        android:layout_alignLeft="@id/custom_item_title"/>
    <ImageView
        android:id="@+id/custom_item_menu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:src="@drawable/ic_menu"/>
</RelativeLayout>

2. activity.xml 에 리스트뷰 생성

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@+id/custom_listview"/>

3. 데이터 클래스 생성하기

data class DriveVO (
    var type: String,
    var title: String,
    var date: String
)

3. ViewHolder 생성하기

ViewHoder란? 항목을 구성하는 뷰가 여러개가 있을 때 이러한 뷰들을 홀딩, 즉 가지고 있는 역할을 하는 클래스를 의미한다.

class DriveHolder(root: View) { //어댑터의 루트 객체 전달 

//항목을 구성하는 뷰들을 선언한다. 
    var typeImageView: ImageView
    var titleView: TextView
    var dateView: TextView
    var menuImageView: ImageView

    init { //init에서 뷰를 획득 
        typeImageView = root.findViewById(R.id.custom_item_type_image)
        titleView = root.findViewById(R.id.custom_item_title)
        dateView = root.findViewById(R.id.custom_item_date)
        menuImageView = root.findViewById(R.id.custom_item_menu)
    }
}

4. Adapter 클래스 생성하기

class DriveAdapter(val cxt: Context, val resId: Int, val datas: MutableList<DriveVO>) : ArrayAdapter<DriveVO>(cxt, resId) {

    override fun getCount(): Int { //항목의 사이즈 리턴 
        return datas.size
    }

//각 항목을 구성하기위한 메소드 
    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        var convertView = convertView
        if (convertView == null) { // 아직 뷰 객체가 준비 돼 있지 않은 상태 
            val inflater =
                context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            convertView = inflater.inflate(resId, null)

            val holder = DriveHolder(convertView)
            convertView!!.tag = holder
        }
        val holder = convertView.getTag() as DriveHolder
        val typeImageView: ImageView = holder.typeImageView
        val titleView = holder.titleView
        val dateView = holder.dateView
        val menuImageView: ImageView = holder.menuImageView
        
        val (type, title, date) = datas[position]
        titleView.text = title
        dateView.text = date
        
        if (type == "doc") {
            typeImageView.setImageDrawable(
                ResourcesCompat.getDrawable(
                    context.getResources(),
                    R.drawable.ic_type_doc, null
                )
            )
        } else if (type == "file") {
            typeImageView.setImageDrawable(
                ResourcesCompat.getDrawable(
                    context.getResources(),
                    R.drawable.ic_type_file, null
                )
            )
        } else if (type == "img") {
            typeImageView.setImageDrawable(
                ResourcesCompat.getDrawable(
                    context.getResources(),
                    R.drawable.ic_type_image, null
                )
            )
        }
        menuImageView.setOnClickListener{
                val toast = Toast.makeText(context, "$title menu click", Toast.LENGTH_SHORT)
                toast.show()
        }

        return convertView
    }
}

5. MainActivity.kt 생성

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

        val mutableList = mutableListOf<DriveVO>()
        mutableList.add(DriveVO(title = "안드로이드",date = "최종 수정 날짜 : 2월 6일", type = "doc"))
        mutableList.add(DriveVO(title = "db.zip",date = "최종 수정 날짜 : 2월 7일", type = "file"))
        mutableList.add(DriveVO(title = "이미지",date = "최종 수정 날짜 : 2월 8일", type = "img"))

        val listView = findViewById<ListView>(R.id.custom_listview)
        val adapter = DriveAdapter(this, R.layout.custom_item, mutableList)
        listView.adapter = adapter

    }
}
profile
주니어 개발자의 개발일지

0개의 댓글