我做了一个RecyclerView,如果里面有很多元素,那么它会混合这些元素。可能是什么问题呢 ?
inner class AboutUsAdapter: RecyclerView.Adapter<AboutUsAdapter.ViewHolder>() {
inner class ViewHolder (view: View) : RecyclerView.ViewHolder(view){
var imageView = view.findViewById<ImageView>(R.id.image_show)
var name = view.findViewById<TextView>(R.id.name)
var subtitle = view.findViewById<TextView>(R.id.sub_text)
val ad = view.findViewById<TextView>(R.id.ad_text)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(LayoutInflater.from(applicationContext).inflate(R.layout.about_us_element, parent, false))
}
override fun getItemCount(): Int {
return texts.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.name.text = texts[position]
holder.subtitle.text = subtitles[position]
if(position == 0){
holder.ad.text = ""
}
if (position == 0 || position == 1){
holder.imageView.setImageBitmap(applicationContext.resources.getDrawable(R.mipmap.ic_launcher).toBitmap())
} else {
holder.imageView.setImageBitmap(getPicFromAsset(files[position - 2]))
}
holder.itemView.setOnClickListener {
val sound: MediaPlayer = MediaPlayer.create(applicationContext, R.raw.simple_tap)
sound.isLooping = false
sound.setOnCompletionListener {
when (position) {
0 -> {
if (showAd) {
mInterstitialAd.adListener = object : AdListener() {
val adIntent = Intent(applicationContext, ChooseImage::class.java)
override fun onAdOpened() {
}
override fun onAdClosed() {
adIntent.putExtra("bundle",extras)
startActivity(adIntent)
val ad = AdRequest.Builder()
.addNetworkExtrasBundle(AdMobAdapter::class.java, extras)
.build()
mInterstitialAd.loadAd(ad)
}
}
if (mInterstitialAd.isLoaded) {
mInterstitialAd.show()
} else {
val intent = Intent(applicationContext, ChooseImage::class.java)
intent.putExtra("bundle",extras)
startActivity(intent)
}
} else {
val intent = Intent(applicationContext, ChooseImage::class.java)
intent.putExtra("bundle",extras)
startActivity(intent)
}
}
else -> {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(array[position - 1]))
startActivity(intent)
}
}
}
sound.start()
}
}
正如您在我附加的代码中看到的那样,我为每个持有者分配了一个位置,我通过该位置确定了图像。通过删除它并使用作为参数传递的位置,一切都对我有用。
RecyclerView 本身不能混合任何东西。RecyclerView 所做的只是显示您给它显示的列表。
在您的代码中,数据列表通常如何进入您的 RecyclerView 并不清楚。但是您应该在您收到/生成此列表的确切位置查找问题。