반응형
ListView
설명
- 여러 항목들을 제공하고 위 아래로 스크롤하여 항목을 보여주는 View
- ListView처럼 여러 뷰를 노출하는 뷰에서는 AdapterView를 활용한다.
- 뷰를 구성하기 위해 개발자가 코드를 통해 결정해 줘야 하는 항목이 있는 뷰를 통칭해서 AdapterView라고 부른다.
- 항목 하나의 뷰에 2개 이상의 데이터를 세팅할 때는 SimpleAdapter를 사용한다.
- 그 외에 Custom 뷰를 지원해야할 경우 BaseAdapter를 상속받은 Adapter를 사용한다.
기본 코드
<?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"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
class MainActivity : AppCompatActivity() {
var textView: TextView? = null
var listView: ListView? = null
var data = arrayOf("리스트1", "리스트2", "리스트3", "리스트4", "리스트5", "리스트6")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
listView = findViewById(R.id.listView)
textView = findViewById(R.id.textView)
val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, data)
listView?.adapter = adapter
listView?.setOnItemClickListener { parent, view, position, id ->
textView?.text = data[position]
}
}
}
Custom ListView 코드 (텍스트 1개)
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"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
row1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/rowTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
MainActivity
class MainActivity : AppCompatActivity() {
var textView: TextView? = null
var listView: ListView? = null
var data = arrayOf("리스트1", "리스트2", "리스트3", "리스트4", "리스트5", "리스트6")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
listView = findViewById(R.id.listView)
textView = findViewById(R.id.textView)
listView?.adapter = ArrayAdapter(this, R.layout.row1, R.id.rowTextView, data)
listView?.setOnItemClickListener { parent, view, position, id ->
textView?.text = data[position]
}
}
}
Custom ListView 코드 (텍스트 N개)
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"
android:orientation="vertical"
tools:context=".MainActivity">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
row2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
MainActivity
class MainActivity : AppCompatActivity() {
var listView: ListView? = null
var data1 = arrayOf("리스트1", "리스트2", "리스트3", "리스트4", "리스트5", "리스트6")
var data2 = arrayOf("리스트A", "리스트B", "리스트C", "리스트D", "리스트E", "리스트F")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val data = data1.mapIndexed { index, value -> mutableMapOf("data1" to value, "data2" to data2[index]) }
val keys = arrayOf("data1", "data2")
val ids = intArrayOf(R.id.textView1, R.id.textView2)
listView = findViewById(R.id.listView)
listView?.adapter = SimpleAdapter(this, data, R.layout.row2, keys, ids)
}
}
Custom Adapter 코드
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"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
row3.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/row3TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="@+id/row3Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button" />
</LinearLayout>
MainActivity
class MainActivity : AppCompatActivity() {
var textView: TextView? = null
var listView: ListView? = null
val data = arrayOf("리스트1", "리스트2", "리스트3", "리스트4", "리스트5", "리스트6")
val buttonClickListener = ButtonClickListener()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
textView = findViewById(R.id.textView)
listView = findViewById(R.id.listView)
listView?.adapter = ListAdapter()
}
inner class ListAdapter : BaseAdapter() {
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View? {
var view: View? = convertView
if (view == null) {
view = layoutInflater.inflate(R.layout.row3, null)
}
val textView: TextView? = view?.findViewById(R.id.row3TextView)
val button: Button? = view?.findViewById(R.id.row3Button)
textView?.text = data[position]
button?.tag = position
button?.setOnClickListener(buttonClickListener)
return view
}
override fun getItem(position: Int): Any? {
return null
}
override fun getItemId(position: Int): Long {
return 0;
}
override fun getCount(): Int {
return data.size
}
}
inner class ButtonClickListener : View.OnClickListener {
override fun onClick(view: View?) {
val position = view?.tag as Int
textView?.text = data[position]
}
}
}
참고
반응형
'Development > Android' 카테고리의 다른 글
[Android] ViewPager (0) | 2021.02.09 |
---|---|
[Android] Spinner (0) | 2021.02.09 |
[Android] Layout (0) | 2021.02.09 |
[Android] View (0) | 2021.02.09 |
[Android] Toast, Snackbar (0) | 2021.02.09 |