-
[android] android 커스텀 리스트뷰 (custom listView)app/android 2022. 1. 17. 17:58반응형
<실습시간> 앞에 배운 리스트뷰를 이용하여 xml로 커스텀 리스트뷰를 만들어보장~~~
Student 클래스
이름
소속학과
학생번호
사진
클래스 구현방법
class StudentC (var name : String, var dept : String, var studentid : Int , var sajin : int) {}
총 2가지 화면이 필요하기 때문에
activity_main.xml
rowitem.xml
MainActivity.kt
StudentC.kt
ListViewAdapter.kt
이 구현 필요
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" tools:context=".MainActivity"> <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
rowitem.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/itemImage" android:layout_width="100dp" android:layout_height="80dp" /> <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/itemName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="이름" android:textSize="20sp" android:textColor="#ff0000" /> <TextView android:id="@+id/itemDept" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="소속학과" android:textSize="15sp" android:textColor="#0022FF" /> <TextView android:id="@+id/itemId" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="학생번호"/> </LinearLayout> </LinearLayout>
MainActivity.kt
사진은 drawable에 저장해두었긔
package com.cookandroid.customlistview import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.view.View import android.view.ViewGroup import android.widget.BaseAdapter import android.widget.Toast import androidx.core.widget.ListViewAutoScrollHelper import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.rowitem.view.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) var studentList : ArrayList<StudentC> = arrayListOf<StudentC>() studentList.add( StudentC("배수지","멀티미디어과", 2101, R.drawable.suji)) studentList.add( StudentC("한지민","컴퓨터소프트웨어공학과", 2102, R.drawable.jimin)) studentList.add( StudentC("남주혁","스타트업과", 2103, R.drawable.juhyuk)) studentList.add( StudentC("조정석","간담췌외과 이익준역", 2104, R.drawable.talent1)) studentList.add( StudentC("유연석","소와외과 안정원역", 2105, R.drawable.talent2)) studentList.add( StudentC("정경호","흉부외과 김준완역", 2106, R.drawable.talent3)) studentList.add( StudentC("김대명","산부인과 양석형역", 2107, R.drawable.talent4)) studentList.add( StudentC("전미도","신경외과 채송화역", 2108, R.drawable.talent5)) var adapter1 = ListViewAdapter(studentList) listView1.adapter = adapter1 listView1.setOnItemClickListener { adapterView, view, i, l -> Toast.makeText(applicationContext, studentList.get(i).name, Toast.LENGTH_LONG).show() } } }
StudentC.kt
package com.cookandroid.customlistview class StudentC(var name : String, var dept : String, var studentId : Int, var sajin : Int){}
ListViewAdapter.kt
package com.cookandroid.customlistview import android.view.View import android.view.ViewGroup import android.widget.BaseAdapter import kotlinx.android.synthetic.main.rowitem.view.* class ListViewAdapter(var studentList : ArrayList<StudentC>) : BaseAdapter() { override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View { var rowItemView = p1 if (rowItemView == null) { rowItemView = View.inflate(p2?.context, R.layout.rowitem, null) } val student1 : StudentC = studentList[p0] rowItemView!!.itemImage.setImageResource(student1.sajin) rowItemView.itemName.text = student1.name rowItemView.itemDept.text = student1.dept rowItemView.itemId.text = student1.studentId.toString() return rowItemView } override fun getItem(p0: Int): Any { return studentList[p0] } override fun getItemId(p0: Int): Long { return p0.toLong() } override fun getCount(): Int { return studentList.size } }
실행결과 >>>
짜짠~~~~~~~~~~~~~
반응형'app > android' 카테고리의 다른 글
[android] android 데이터 관리 +CustomerManagement 최종구현 (0) 2022.01.17 [android] android 데이터 저장과 관리 (0) 2022.01.17 [android] android 어뎁터 뷰 (0) 2022.01.13 [android] android 액티비티와 인텐트 (0) 2022.01.13 [android] android 파일 처리 , 실습 간단 일기장 (0) 2022.01.13