간단한 RecyclerView 예제입니다.

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
val array = ArrayList<String>() | |
array.add("a") | |
array.add("b") | |
array.add("c") | |
array.add("a") | |
array.add("b") | |
array.add("c") | |
array.add("a") | |
array.add("b") | |
array.add("c") | |
array.add("a") | |
array.add("b") | |
array.add("c") | |
array.add("a") | |
array.add("b") | |
array.add("c") | |
val customAdapter = CustomAdapter(array) | |
val rv = findViewById<RecyclerView>(R.id.rv) | |
rv.adapter = customAdapter | |
rv.layoutManager = LinearLayoutManager(this) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CustomAdapter(private val dataSet: ArrayList<String>) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() { | |
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) { | |
val textView: TextView | |
init { | |
// Define click listener for the ViewHolder's View. | |
textView = view.findViewById(R.id.textView) | |
} | |
} | |
// Create new views (invoked by the layout manager) | |
override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): ViewHolder { | |
// Create a new view, which defines the UI of the list item | |
val view = LayoutInflater.from(viewGroup.context) | |
.inflate(R.layout.text_row_item, viewGroup, false) | |
return ViewHolder(view) | |
} | |
// Replace the contents of a view (invoked by the layout manager) | |
override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) { | |
// Get element from your dataset at this position and replace the | |
// contents of the view with that element | |
viewHolder.textView.text = dataSet[position] | |
} | |
// Return the size of your dataset (invoked by the layout manager) | |
override fun getItemCount() = dataSet.size | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<androidx.constraintlayout.widget.ConstraintLayout | |
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" | |
tools:context=".MainActivity"> | |
<androidx.recyclerview.widget.RecyclerView | |
android:id="@+id/rv" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"/> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="100dp" | |
android:layout_marginLeft="10dp" | |
android:layout_marginRight="10dp" | |
android:gravity="center_vertical"> | |
<TextView | |
android:id="@+id/textView" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:textSize="30dp" | |
android:text="HELLO"/> | |
</FrameLayout> |
'Android(Kotlin)' 카테고리의 다른 글
WorkManager Chaining (0) | 2022.03.30 |
---|---|
Android Log가 안뜰 때 (0) | 2022.03.18 |
Android Notification (0) | 2022.03.06 |
Android Studio Bumblebee classpath (0) | 2022.02.20 |
Retrofit에 관하여 - Call/Response, GSON, OKHttp (0) | 2022.02.05 |