RecyclerView не отображает кастомные View
Возникла проблема с RecyclerView, не могу найти решение в гугле. Есть фрагмент, внутри RecyclerView. Элементы генерятся, но не отображаются.
Класс фрагмента - https://github.com/LazyTechwork/QRScanner/blob/master/app/src/main/java/ru/lazytechwork/qrscanner/fragments/HistoryFragment.kt
class HistoryFragment : Fragment() {
private var scans: List<Scan> = emptyList()
private val ioScope = CoroutineScope(Dispatchers.IO)
private lateinit var recyclerView: RecyclerView
private lateinit var viewAdapter: ScanHistoryAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_history, container, false)
viewAdapter = ScanHistoryAdapter(scans, (activity as MainActivity).db)
recyclerView = view.findViewById<RecyclerView>(R.id.scanlist).apply {
setHasFixedSize(true)
adapter = viewAdapter
}
ioScope.launch {
scans = (activity as MainActivity).getScans()
viewAdapter.updateDataset(scans)
}
return view
}
}
XML фрагмента - https://github.com/LazyTechwork/QRScanner/blob/master/app/src/main/res/layout/fragment_history.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">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/scanlist"
android:layout_width="match_parent"
android:layout_height="0dp"
android:scrollbars="vertical"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constrainedHeight="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:listitem="@layout/history_item" />
</androidx.constraintlayout.widget.ConstraintLayout>
Код адаптера - https://github.com/LazyTechwork/QRScanner/blob/master/app/src/main/java/ru/lazytechwork/qrscanner/data/adapters/ScanHistoryAdapter.kt
class ScanHistoryAdapter(private var dataset: List<Scan>, private val db: AppDatabase) :
RecyclerView.Adapter<ScanHistoryAdapter.ViewHolder>() {
class ViewHolder(val historyItem: HistoryItem) : RecyclerView.ViewHolder(historyItem)
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): ViewHolder =
ViewHolder(HistoryItem(parent, db))
fun updateDataset(scans: List<Scan>) {
dataset = scans
notifyDataSetChanged()
}
override fun getItemCount(): Int = dataset.size
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val viewItem = holder.historyItem
val scan = dataset[position]
viewItem.setScan(scan)
}
}
В Android Studio всё нормально отображается.
Ответы (1 шт):
Автор решения: Circassian
→ Ссылка
Вот тебе новый адаптер. Проверил, все работает
class ScanHistoryAdapter2(val db: AppDatabase) :
RecyclerView.Adapter<ScanHistoryAdapter2.ScanViewHolder>() {
var items: List<Scan> = arrayListOf()
set(value) {
field = value
notifyDataSetChanged()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
ScanViewHolder(
LayoutInflater.from(parent.context).inflate(
R.layout.history_item,
parent,
false
)
)
override fun onBindViewHolder(holder: ScanViewHolder, position: Int) =
holder.bind(position)
override fun getItemCount() =
items.size
inner class ScanViewHolder(
view: View
) : RecyclerView.ViewHolder(view) {
fun bind(positon: Int) {
val scan = items[positon]
with(itemView) {
history_name.text = scan.name
history_date.text = MainActivity.DATE_FORMAT.format(scan.date)
history_data.text = scan.data
history_type.setImageResource(
when (scan.type) {
HistoryType.LINK -> R.drawable.ic_link_outline
HistoryType.TEXT -> R.drawable.ic_text_outline
HistoryType.CONTACT -> R.drawable.ic_contact_outline
}
)
favourite_switch.apply {
isChecked = scan.isFavourite
setOnCheckedChangeListener(FavouriteSwitcher2(db, scan))
}
}
}
}
}
class FavouriteSwitcher2(private val db: AppDatabase, private val scan: Scan) :
CompoundButton.OnCheckedChangeListener {
private val ioScope = CoroutineScope(Dispatchers.IO)
override fun onCheckedChanged(buttonView: CompoundButton?, isChecked: Boolean) {
ioScope.launch {
if (isChecked)
db.scansInterface().makeFavourite(scan)
else
db.scansInterface().removeFavourite(scan)
}
}
}

