ViewPager2 没有显示
ViewPager2 doesn't show up
我使用 ViewPager2。所以我跟着一些例子写了代码。但我不明白为什么它不起作用。我不认为我写的这段代码是错误的。请让我知道为什么我什么都看不到。我知道ViewPager2的实现类似于RecyclerView adapter,所以我照做了。
class UserAdapter(context: Context,userList : List<User>) : RecyclerView.Adapter<UserAdapter.UserViewHolder>() {
val mContext = context
val mUserList : List<User> = userList
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UserViewHolder {
var view = LayoutInflater.from(mContext).inflate(R.layout.item,parent,false)
return UserViewHolder(view)
}
override fun getItemCount(): Int =0
override fun onBindViewHolder(holder: UserViewHolder, position: Int) {
holder.icon.setImageResource(mUserList.get(position).icon)
holder.textname.text = mUserList.get(position).name
holder.textage.text = mUserList.get(position).age
}
class UserViewHolder internal constructor(itemView: View) :
RecyclerView.ViewHolder(itemView){
lateinit var icon :ImageView
lateinit var textname: TextView
lateinit var textage: TextView
init{
icon = itemView.findViewById(R.id.icon)
textname = itemView.findViewById(R.id.textView)
textage = itemView.findViewById(R.id.textView2)
}
}
}
class MainActivity : AppCompatActivity() {
private lateinit var userList: ArrayList<User>
private lateinit var binding: ActivityMainBinding
private lateinit var userAdapter:UserAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.root);
userList = arrayListOf()
userList.add(User(R.drawable.child,"child","21"))
userList.add(User(R.drawable.sun,"sun","22"))
userList.add(User(R.drawable.moon,"moon","23"))
userAdapter = UserAdapter(this,userList)
binding.viewpager2.adapter = userAdapter
}
}
更改或替换override fun getItemCount(): Int =0
override fun getItemCount(): Int = mUserList.size()
我使用 ViewPager2。所以我跟着一些例子写了代码。但我不明白为什么它不起作用。我不认为我写的这段代码是错误的。请让我知道为什么我什么都看不到。我知道ViewPager2的实现类似于RecyclerView adapter,所以我照做了。
class UserAdapter(context: Context,userList : List<User>) : RecyclerView.Adapter<UserAdapter.UserViewHolder>() {
val mContext = context
val mUserList : List<User> = userList
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UserViewHolder {
var view = LayoutInflater.from(mContext).inflate(R.layout.item,parent,false)
return UserViewHolder(view)
}
override fun getItemCount(): Int =0
override fun onBindViewHolder(holder: UserViewHolder, position: Int) {
holder.icon.setImageResource(mUserList.get(position).icon)
holder.textname.text = mUserList.get(position).name
holder.textage.text = mUserList.get(position).age
}
class UserViewHolder internal constructor(itemView: View) :
RecyclerView.ViewHolder(itemView){
lateinit var icon :ImageView
lateinit var textname: TextView
lateinit var textage: TextView
init{
icon = itemView.findViewById(R.id.icon)
textname = itemView.findViewById(R.id.textView)
textage = itemView.findViewById(R.id.textView2)
}
}
}
class MainActivity : AppCompatActivity() {
private lateinit var userList: ArrayList<User>
private lateinit var binding: ActivityMainBinding
private lateinit var userAdapter:UserAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.root);
userList = arrayListOf()
userList.add(User(R.drawable.child,"child","21"))
userList.add(User(R.drawable.sun,"sun","22"))
userList.add(User(R.drawable.moon,"moon","23"))
userAdapter = UserAdapter(this,userList)
binding.viewpager2.adapter = userAdapter
}
}
更改或替换override fun getItemCount(): Int =0
override fun getItemCount(): Int = mUserList.size()