我在Android Studio中收到以下警告: "来自视图适配器的无条件布局膨胀:应使用View Holder模式(使用传递给此方法的循环视图作为第二个参数)以实现更顺畅的滚动。"在inflater.inflate(R.layout.animal_ticket,null)行。
如何修复警告?我无法找到解决该问题的方法。
谢谢!
override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View {
val animal = listOfAnimals[p0]
val inflater = LayoutInflater.from(context)
var holder = ViewHolder()
val myView = inflater.inflate(R.layout.animal_ticket, null)
myView.tvName.text = animal.name!!
myView.tvDes.text = animal.description!!
myView.jvAnimalImage.setImageResource(animal.image!!)
return myView
}
答案 0 :(得分:2)
如果之前另一个View
相同的视图类型(getItemViewType(position: Int)
返回相同的值)滚动屏幕,列表视图可能会将该实例作为第二个参数传递给{{1 }}。重新使用该视图比扩展新视图更快。
您还应该使用getView()
来缓存有关视图的内容,例如相对昂贵的ViewHolder
。您可以通过标记将其附加到视图中并从视图中检索它。
findViewById()
答案 1 :(得分:2)
对于任何RecyclerView,您需要自己的ViewHolder类,其中包含animal_ticket
中列出的所有视图。
基本上,它的工作原理如下:
1)创建'持有的ViewHolder。您要显示的项目的所有视图;
2)将ViewHolder绑定到RecyclerView并为内部视图指定值;
这是我写的一个示例适配器:
class MyActivity : Activity() {
//users is the list we're going to use to get information for the views
val users = ArrayList<User>()
//...getting user information
//.. your activity stuff here
//Creating our adapter
/*
Note that to extend the RecyclerView.Adapter, you need to specify a
ViewHolder. The code becomes much easier to manage if you just put the
ViewHolder inside your adapter.
*/
inner class MyAdapter : RecyclerView.Adapter<MyAdapter.MyViewHolder>() {
//This is the view holder
inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
//Here you declare your views and get them from the itemView
//The itemView is one that is passed each time to the RecyclerView
//(the items inside your XML layout file)
internal val userImageView = itemView.findViewById(R.id.userImage)
internal val userFullName = itemView.findViewById(R.id.userName)
}
//This is where you return your own ViewHolder with your layout
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
//user_list_item.xml is below
val itemView = layoutInflater.inflate(R.layout.user_list_item, parent, false))
return MyViewHolder(itemView)
}
//In here is where you want to set your values for the views
override fun onBindViewHolder(holder: SentRequestViewHolder, position: Int) {
val currentUser = users[position]
holder.userImage.drawable = currentUser.drawable
holder.userFullName.text = currentUser.name
}
//You must override this method as well for the adapter to work properly
override fun getItemCount() = users.size
}
使用RecyclerView.Adapter时必须覆盖这些方法
这是user_list_item.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp">
<ImageView
android:id="@+id/userImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/userName"
android:layout_below="@id/userImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>