I make an app which use API and should show some basic data (exchange rates) on the screen.
But elements of recyclerView
doesn't appear on the screen. I can see only textview.
Trying to find a problem for a days, can't do anything..
Here's some code:
StartFragment:
class StartFragment : Fragment() {
private lateinit var binding: FragmentStartBinding
private lateinit var recyclerView: RecyclerView
private lateinit var adapter: StartAdapter
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
// Inflate the layout for this fragment
binding = FragmentStartBinding.inflate(inflater, container, false)
val viewModel = ViewModelProvider(this)[StartViewModel::class.java]
recyclerView = binding.rvStart
adapter = StartAdapter()
recyclerView.adapter = adapter
viewModel.getCash()
viewModel.myCashList.observe(viewLifecycleOwner) { list ->
list.body()?.let { adapter.setList(it) }
}
return binding.root
}
}
StartAdapter:
class StartAdapter:RecyclerView.Adapter<StartAdapter.StartViewHolder>() {
private var listStart = emptyList<CashItem>()
class StartViewHolder(val binding: ItemMoneyLayoutBinding):RecyclerView.ViewHolder(binding.root)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): StartViewHolder {
val binding = ItemMoneyLayoutBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return StartViewHolder(binding)
}
override fun getItemCount(): Int {
return listStart.size
}
override fun onBindViewHolder(holder: StartViewHolder, position: Int) {
holder.binding.itemName.text = listStart[position].ccy
holder.binding.itemBuy.text = listStart[position].buy
holder.binding.itemSale.text = listStart[position].sale
}
@SuppressLint("NotifyDataSetChanged")
fun setList(list: List<CashItem>){
listStart = list
notifyDataSetChanged()
}
}
fragment_start.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".screens.start.StartFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_start"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</FrameLayout>
Repository:
class Repository {
suspend fun getCashMoney(): Response<Cash>{
return RetrofitInstance.api.getCash()
}
suspend fun getNonCashMoney(): Response<NonCash>{
return RetrofitInstance.api.getNonCash()
}
}
Simply tried to delete textview "Start" cause it could block the rv. Any other attempts to change code failed. I guess using viewBinding can be an issue, but I can't find where exactly.
Lack of LayoutManager.
Just like this:
recycleView.layoutManager = LinearLayoutManager(context)