Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- Generic
- CLASS
- lifecycle
- react native
- map
- Filter
- animation
- elementAt
- ConstraintLayout
- enum
- docker
- AWS
- Service
- Swift
- vuex
- class component
- docker-compose
- Foreign Key
- union
- Kotlin
- ReactNative
- LiveData
- Interface
- mongoose
- recyclerview
- MINUS
- list
- function
- 생명주기
- collection
Archives
- Today
- Total
개발 일기
RecyclerView 이란? 본문
RecyclerView 위젯은 ListView 보다 더 진보하고 유연해진 버전입니다.
RecyclerView는 개발자가 제공한 layoutmanager에서 제공한 뷰로 채워집니다.
LinearLayoutManager |
1차원 목록을 제공합니다. |
GridLayoutManager |
다차원에 그리드로 제공합니다. |
StaggeredGridLayoutManager |
양 옆에 열이 일치하지 않는 다차원에 그리드로 제공합니다. |
RecyclerView.ItemAnimator 을 이용하여 Item 애니메이터를 제공할수도 있습니다.
RecyclerView.ItemDecoration 을 이용해서 Item View 사이에 간격을 조절할수 있습니다.
예제 :)
implementation "androidx.recyclerview:recyclerview:1.1.0"
XML :)
<androidx.recyclerview.widget.RecyclerView
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/white"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
</androidx.recyclerview.widget.RecyclerView>
Code :)
- Activity
class MyActivity : Activity() {
private lateinit var recyclerView: RecyclerView
private lateinit var viewAdapter: RecyclerView.Adapter<*>
private lateinit var viewManager: RecyclerView.LayoutManager
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.my_activity)
viewManager = LinearLayoutManager(this)
viewAdapter = MyAdapter(myDataset)
recyclerView = findViewById<RecyclerView>(R.id.my_recycler_view).apply {
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
setHasFixedSize(true)
// use a linear layout manager
// LayoutManager 설정
layoutManager = viewManager
// specify an viewAdapter (see also next example)
// RecyclerView Adapter 설정
adapter = viewAdapter
}
}
// ...
}
class MyAdapter(private val myDataset: Array<String>) :
RecyclerView.Adapter<MyAdapter.MyViewHolder>() {
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder.
// Each data item is just a string in this case that is shown in a TextView.
// RecyclerView.ViewHolder 제공합니다.
class MyViewHolder(val textView: TextView) : RecyclerView.ViewHolder(textView)
// Create new views (invoked by the layout manager)
// 콘텐츠에 표시되는 화면에 뷰를 설정합니다.
override fun onCreateViewHolder(parent: ViewGroup,
viewType: Int): MyAdapter.MyViewHolder {
// create a new view
val textView = LayoutInflater.from(parent.context)
.inflate(R.layout.my_text_view, parent, false) as TextView
// set the view's size, margins, paddings and layout parameters
...
return MyViewHolder(textView)
}
// Replace the contents of a view (invoked by the layout manager)
// 해당 포지션에 맞는 데이터를 뷰 홀더에 레이아웃에 그리는 역할입니다.
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
holder.textView.text = myDataset[position]
}
// Return the size of your dataset (invoked by the layout manager)
// 화면에 표시되는 뷰에 사이즈 입니다.
override fun getItemCount() = myDataset.size
// 해당 포지션 마다 ViewHolder가 다른경우 여기서 설정이 가능합니다.
override fun getItemViewType(position: Int): Int {
return viewModel.getItemViewType(position)
}
}
'Client > 안드로이드' 카테고리의 다른 글
Edit Text ? (0) | 2020.03.10 |
---|---|
TextView ? (0) | 2020.03.09 |
android key Hash 구하기 (0) | 2020.03.07 |
Android Realm 사용방법 (0) | 2020.03.06 |
ConstraintLayout (0) | 2020.03.05 |
Comments