작성
·
272
·
수정됨
0
안녕하세요 개발자님! 왕초보편 수강하고있는 대학생입니다. 다름이아니라 개발자님의 앱 개발 왕초보편 강의 중 트로트 가수 앱 만들기를 조금 변형시켜서, 프래그먼트에서 객체 데이터를 전달받고 보여주는 리사이 클러 뷰를 만들어 보려고 했습니다.
근데 실행시켜 보니 딱히 오류는 뜨지 않지만, 리사이클러 뷰가 보이지 않는 현상을 겪고 있어서 질문드립니다!
도와주시면 감사하겠습니다!
구글 드라이브 : https://drive.google.com/file/d/1hiygqt0gl78embhelzkivfPbgyDGb3WV/view?usp=drive_link
adapter
package com.wosong.musiclist
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
class RVAdapter(val context: MutableList<RVdata>) : RecyclerView.Adapter<RVAdapter.ViewHolder>(){
var data = mutableListOf<RVdata>()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RVAdapter.ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.rv_item,parent,false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: RVAdapter.ViewHolder, position: Int) {
holder.bindItems(data[position])
}
override fun getItemCount(): Int {
return data.size
}
inner class ViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView){
fun bindItems(item : RVdata){
val rv_text = itemView.findViewById<TextView>(R.id.rvTextId)
rv_text.text = item.text
val image = itemView.findViewById<ImageView>(R.id.itemimage)
image.setImageResource(item.img)
}
}
}
data class
package com.wosong.musiclist
data class RVdata(
val text : String,
val img : Int
)
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="50dp">
<ImageView
android:layout_width="50dp"
android:layout_height="match_parent"
android:src="@drawable/zino"
android:id="@+id/itemimage"
/>
<TextView
android:layout_margin="10dp"
android:textStyle="bold"
android:text="List of songs"
android:id="@+id/rvTextId"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
fragment
package com.wosong.musiclist
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.navigation.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
class sing1Fragment : Fragment() {
// TODO: Rename and change types of parameters
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_sing1, container, false)
val items : MutableList<RVdata> = mutableListOf()
items.add(RVdata(img = R.drawable.jazzyfact,text = "A tribe called jazzyfact"))
items.add(RVdata(img = R.drawable.jazzyfact,text = "A tribe called jazzyfact"))
items.add(RVdata(img = R.drawable.jazzyfact,text = "A tribe called jazzyfact"))
val rv = view.findViewById<RecyclerView>(R.id.JazzyFactView)
val rvAdapter = RVAdapter(items)
rv.adapter = rvAdapter
rv.layoutManager = LinearLayoutManager(context)
view.findViewById<ImageView>(R.id.image2).setOnClickListener{
it.findNavController().navigate(R.id.action_sing1Fragment_to_sing2Fragment)
}
view.findViewById<ImageView>(R.id.image3).setOnClickListener{
it.findNavController().navigate(R.id.action_sing1Fragment_to_sing3Fragment)
}
return view
}
}
권한 수정 했습니다!