리사이클러뷰의 각 항목에 숫자를 1부터 70까지 출력되도록 코드를 작성해 놓은 상태입니다.
프래그먼트에 담아놓은 리사이클러뷰를 화면에 출력시켜주지 않습니다.
버튼을 눌렀을때, 프래그먼트를 불러보면, 여기에 담긴 리사이클러뷰가 출력이 되질 않습니다.
간단한 텍스트뷰라든지 이미지 같은 것은 잘만 출력시켜주는데, 리사이클러뷰만 출력이 안됩니다.
FragmentActivity1.kt
class FragmentActivity1 : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
val view: View = inflater.inflate(R.layout.activity_fragment1, container, false)
val dataSet = (1..70).toList()
recy.adapter = SampleAdapter2(dataSet)
return view
}
class SampleAdapter2(private val dataSet: List<Int>) : RecyclerView.Adapter<SampleAdapter2.NumberViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NumberViewHolder {
val layoutView: LinearLayout = LayoutInflater.from(parent.context).inflate(R.layout.activity_bible3927, parent, false) as LinearLayout
return NumberViewHolder(layoutView)
}
override fun getItemCount(): Int = dataSet.size
override fun onBindViewHolder(holder: NumberViewHolder, position: Int) {
if(position < dataSet.size) {
holder.number.text = dataSet[position].toString()
}
}
inner class NumberViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val number = itemView.findViewById(R.id.textView) as TextView
}
}
★질문하면서, 코드를 넣었다 뺏다 하면서 알아봤는데, 아래 코드를 넣으면 에러가 나고, 안넣으면 에러가 안났습니다. ㅠㅠ 해결했다는 뜻이 아니라, 그냥 이정도만 알아냈다는 것입니다.
val dataSet = (1..70).toList()
recy.adapter = SampleAdapter2(dataSet)
activity_fragment.xml
<?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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".HomeActivity"
tools:showIn="@layout/activity_home">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recy"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintEnd_toStartOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
activity_item.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="wrap_content"
android:orientation="vertical">
<!-- height값을 wrap_content로 해야 글자간 여백이 늘어나는 오류가 생기지 않는다. -->
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="안녕\n하세요" />
</LinearLayout>