<?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>
<?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"/>
data class DriveVO (
var type: String,
var title: String,
var date: String
)
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)
}
}
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
}
}
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
}
}