1. Adapter의 onBindViewHolder 함수에서 holder의 adapterposition 값을 저장하지 말 것

- Drag and drop 기능 구현 시 adapterposition을 저장한 값을 사용하면 이전 값을 가져오거나 잘못된 값을 가져올 수 있음


2. Adapter의 onBindViewHolder 함수에서 특정 이미지나 텍스트를 변경 시 조건문으로 만들 것

- 기본적으로 recyclerview는 아이템을 재사용하기 때문에 재사용된 데이터가 다른 위치에서 보일 수 있다. 하지만 정확히 조건문을 구현하여 변경 전과 후를 구현하면 방지할 수 있다.


3. Adapter 내 getItemCount 함수와 getItemId 함수를 구현할 것

- 2번과 비슷한 이유로 재사용 이슈로 인해 일부 잘못 표기되는 경우가 존재한다.


4. Listview의 head/foot view는 Recyclerview에선 view type으로 구현 가능

- Recyclerview는 listview와 별개로 별도의 head/foot view를 만들 수 있는 함수가 없다. 좀 더 확장한 개념의 view type을 사용하여 단순히 head/foot view 뿐만 아니라 리스트 데이터의 따라 다양한 형태로 view를 구현 가능하다.


5. Recyclerview에서 drag and drop 기능과 swipe 기능 구현

https://medium.com/@ipaulpro/drag-and-swipe-with-recyclerview-b9456d2b1aaf


* 개발하면서 배우는 점을 지속적으로 업데이트할 예정

by JamesY 2018. 7. 22. 21:56

Kotlin으로 작성된 커스텀 리스트뷰 예제



MainActivity.kt

import android.content.Context
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

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

val list: ArrayList<CustomItem> = ArrayList()
list.add(CustomItem("James", 23, "Male"))
list.add(CustomItem("Jamie", 20, "Female"))
list.add(CustomItem("John", 30, "Male"))

listview1.adapter = CustomAdapter(this, list)
}

private class CustomAdapter(context: Context, array: ArrayList<CustomItem>): BaseAdapter() {

private var list: ArrayList<CustomItem> = array
private var inflater: LayoutInflater = LayoutInflater.from(context)

override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View {

val view: View?
val viewHolder: ViewHolder

if(p1 == null) {
view = this.inflater.inflate(R.layout.listview_item, p2,false)
viewHolder = ViewHolder(view)
view.tag = viewHolder
}
else {
view = p1
viewHolder = view.tag as ViewHolder
}

viewHolder.name.text = list[p0].name
viewHolder.age.text = list[p0].age.toString()
viewHolder.gender.text = list[p0].gender

return view!!
}

override fun getItem(p0: Int): Any {
return list[p0]
}

override fun getItemId(p0: Int): Long {
return p0.toLong()
}

override fun getCount(): Int {
return list.size
}

}

private class ViewHolder(view: View?) {

val name: TextView = view?.findViewById(R.id.nameTextView) as TextView
val age: TextView = view?.findViewById(R.id.ageTextView) as TextView
val gender: TextView = view?.findViewById(R.id.genderTextView) as TextView
}

data class CustomItem(var name: String, var age: Int, var gender: String)
}


listview_item.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">

<TextView
android:id="@+id/nameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />

<TextView
android:id="@+id/ageTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />

<TextView
android:id="@+id/genderTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
</LinearLayout>


GitHub : https://github.com/Hot6ix/SimpleCustomListView

by JamesY 2017. 12. 21. 17:52
| 1 |