[英]How can I see my List in my RecyclerView.ListAdapter?
So I'm doing a project and I'm looking to see if my RecyclerView
works.所以我正在做一个项目,我想看看我的RecyclerView
有效。 Here's what I got so far这是我到目前为止所得到的
The data class:数据类:
@Parcelize
@Entity(tableName = "asteroid_feed")
data class Asteroid(val id: Long, val codename: String, val closeApproachDate: String,
val absoluteMagnitude: Double, val estimatedDiameter: Double,
val relativeVelocity: Double, val distanceFromEarth: Double,
val isPotentiallyHazardous: Boolean) : Parcelable
The Adapter适配器
class AsteroidViewAdapter (private val list: MutableList<Asteroid>) : ListAdapter<Asteroid, AsteroidViewAdapter.AsteroidViewHolder>(DiffCallback) {
companion object DiffCallback : DiffUtil.ItemCallback<Asteroid>() {
override fun areItemsTheSame(oldItem: Asteroid, newItem: Asteroid): Boolean {
return oldItem.id == newItem.id
}
override fun areContentsTheSame(oldItem: Asteroid, newItem: Asteroid): Boolean {
return oldItem == newItem
}
}
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): AsteroidViewHolder {
return AsteroidViewHolder(AsteroidListContainerBinding.inflate(LayoutInflater.from(parent.context)))
}
override fun onBindViewHolder(holder: AsteroidViewHolder, position: Int) {
holder.bind(getItem(position))
}
class AsteroidViewHolder (private val binding: AsteroidListContainerBinding) : RecyclerView.ViewHolder(binding.root) {
fun bind(item: Asteroid){
binding.value = item
}
}
}
The Fragment片段
class MainFragment : Fragment() {
private lateinit var manager: RecyclerView.LayoutManager
private val viewModel: MainViewModel by lazy {
ViewModelProvider(this).get(MainViewModel::class.java)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val binding = FragmentMainBinding.inflate(inflater)
binding.lifecycleOwner = this
binding.viewModel = viewModel
val mutableList: MutableList<Asteroid> = ArrayList()
mutableList.add(Asteroid(1, "fgnuugrhrg", "bihagtyjerwailgubivb", 4.0, 8.0,3.0, 9.0, false))
mutableList.add(Asteroid(2, "fguk.nuugrhrg", "bidjswjyhagrwailgubivb", 3.0, 90.0,355.0, 9.0, true))
mutableList.add(Asteroid(3, "fgnssuugrhrg", "bshjtihagrwailgubivb", 4.0, 33.0,33.0, 9.0, false))
mutableList.add(Asteroid(4, "fgnuw4suugrhrg", "bjsryjihagrwailgubivb", 6.0, 8.0,11.0, 9.0, true))
mutableList.add(Asteroid(5, "fgnuugrudkdkhrg", "bihjjkkuagrwailgubivb", 4.0, 5.0,77.0, 9.0, false))
manager = LinearLayoutManager(this.context)
binding.asteroidRecycler.apply {
adapter = AsteroidViewAdapter(mutableList)
layoutManager = manager
}
setHasOptionsMenu(true)
return binding.root
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.main_overflow_menu, menu)
super.onCreateOptionsMenu(menu, inflater)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return true
}
}
XML Files: The Layout XML 文件:布局
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="viewModel"
type="com.udacity.asteroidradar.main.MainViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/app_background">
<FrameLayout
android:id="@+id/activity_main_image_of_the_day_layout"
android:layout_width="match_parent"
android:layout_height="220dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/activity_main_image_of_the_day"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
app:srcCompat="@drawable/placeholder_picture_of_day"/>
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:textColor="@android:color/white"
android:textStyle="bold"
android:textSize="20sp"
android:layout_gravity="bottom"
android:background="#55010613"
android:text="@string/image_of_the_day" />
</FrameLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/asteroid_recycler"
android:layout_width="0dp"
android:layout_height="0dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/activity_main_image_of_the_day_layout"
app:layout_constraintVertical_bias="0.0" />
<ProgressBar
android:id="@+id/status_loading_wheel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
The Container:容器:
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
<variable
name="value"
type="com.udacity.asteroidradar.Asteroid" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/app_background">
<TextView
android:id="@+id/codename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:textColor="@color/default_text_color"
android:textSize="20sp"
android:textStyle="bold"
android:text="@{value.codename}"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="@string/codename" />
<TextView
android:id="@+id/date_field"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:textColor="@color/default_text_color"
android:text="@{value.closeApproachDate}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="@+id/codename"
app:layout_constraintTop_toBottomOf="@+id/codename"
tools:text="@tools:sample/date/mmddyy" />
<ImageView
android:id="@+id/danger_pic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="@+id/codename"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="@drawable/ic_status_potentially_hazardous" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
As you can see, I was using a MutableList to fill in the fields and having that list viewed on my RecyclerView.ListAdapter in the fragment_main but it is not showing anything.如您所见,我使用 MutableList 来填充字段,并在 fragment_main 中的 RecyclerView.ListAdapter 上查看该列表,但它没有显示任何内容。 Again this is just checking to see if The RecyclerView is working.同样,这只是检查 RecyclerView 是否正常工作。 Thanks for the Help.谢谢您的帮助。
The issue that you send the list to the adapter, but didn't submit it using submitList()
您将列表发送到适配器但未使用submitList()
提交的问题
You can do that in init{}
of the adapter您可以在适配器的init{}
中执行此操作
class AsteroidViewAdapter(private val list: MutableList<Asteroid>) :
ListAdapter<Asteroid, AsteroidViewAdapter.AsteroidViewHolder>(DiffCallback) {
init {
submitList(list)
}
// omitted code
Based on my understanding list view doesn't know what size of the items in the list to populate.根据我的理解,列表视图不知道要填充的列表中项目的大小。 So you need to override item list count by below inside list adapter class.所以你需要通过下面的列表适配器类来覆盖项目列表计数。
override fun getItemCount(): Int {
return items.size
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.