我在recyclerView中的notifyDataSetChanged期间在View中遇到问题。我实现了pull torefresh逻辑。所以我刷新了我的RecyclerView并调用notifyDataSetChanged。我动态创建一个线性布局并将其添加到父视图中。动态线性布局具有图像。最初加载时它会正确显示。在拉动刷新期间,图像会加载但会立即消失。我无法理解问题会是什么?
BindViewHolder的一部分
if(tempAvailable == true && heartAvailable == true && lungAvailable == true) {
for(i in 1..3) {
if(i == 1) {
val df = DecimalFormat("#.##");
var childLinearLayout:LinearLayout? = createChildView(BitmapFactory.decodeResource(context.getResources(), R.drawable.temperature), df.format(coreBodyTemp).toString(), context.getString(R.string.temperatureStr))
parentLinearLayout.addView(childLinearLayout)
//childLinearLayout = null
} else if(i == 2) {
var childLinearLayout:LinearLayout? = createChildView(BitmapFactory.decodeResource(context.getResources(), R.drawable.heart), heartRate.toString(), context.getString(R.string.heartRate))
parentLinearLayout.addView(childLinearLayout)
//childLinearLayout = null
} else if(i == 3) {
var childLinearLayout:LinearLayout? = createChildView(BitmapFactory.decodeResource(context.getResources(), R.drawable.lung), "", "")
parentLinearLayout.addView(childLinearLayout)
// childLinearLayout = null
}
}
} else if (tempAvailable) {
val df = DecimalFormat("#.##");
var childLinearLayout:LinearLayout? = createChildView(BitmapFactory.decodeResource(context.getResources(), R.drawable.temperature), df.format(coreBodyTemp).toString()+" \u00B0F", context.getString(R.string.temperatureStr))
parentLinearLayout.addView(childLinearLayout)
//childLinearLayout = null
} else if (heartAvailable) {
var childLinearLayout:LinearLayout? = createChildView(BitmapFactory.decodeResource(context.getResources(), R.drawable.heart), heartRate.toString()+" bpm", context.getString(R.string.heartRate))
parentLinearLayout.addView(childLinearLayout)
// childLinearLayout = null
} else if (lungAvailable) {
var childLinearLayout:LinearLayout? = createChildView(BitmapFactory.decodeResource(context.getResources(), R.drawable.lung), "", "")
parentLinearLayout.addView(childLinearLayout)
//childLinearLayout = null
}
动态创建线性布局的方法
fun createChildView(icon: Bitmap,readingVal: String, readingTag: String) : LinearLayout {
val lp = LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT)
lp.weight = 1f
val parent: LinearLayout = LinearLayout(context)
parent.setOrientation(LinearLayout.HORIZONTAL)
parent.setLayoutParams(lp)
val imageView = ImageView(context)
val layoutParams = LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT)
layoutParams.weight = 0.4f
layoutParams.height = 60
layoutParams.width = 60
layoutParams.leftMargin = 10
layoutParams.rightMargin = 10
layoutParams.topMargin = 10
layoutParams.bottomMargin =10
imageView.setLayoutParams(layoutParams)
imageView.setImageBitmap(icon)
parent.addView(imageView)
val readingLayout = LinearLayout(context)
readingLayout.setOrientation(LinearLayout.VERTICAL)
val lp1 = LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT)
lp1.weight = 0.6f
lp1.topMargin = 10
readingLayout.setLayoutParams(lp)
val displayValue = TextView(context)
val layoutParams1 = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 0)
layoutParams1.weight = 0.7f
layoutParams1.topMargin = 24
layoutParams1.bottomMargin = 4
displayValue.setLayoutParams(layoutParams1)
displayValue.setText(readingVal)
displayValue.setTextSize(12f)
displayValue.setTextColor(Color.BLACK)
readingLayout.addView(displayValue)
val displayTag = TextView(context)
layoutParams1.weight = 0.3f
layoutParams1.topMargin = 0
displayTag.setLayoutParams(layoutParams1)
displayTag.setText(readingTag)
displayTag.setTextSize(8f)
displayTag.setTextColor(Color.BLACK)
displayTag.setAlpha(0.6f)
readingLayout.addView(displayTag)
parent.addView(readingLayout)
return parent
}
更新:
我通过在Recycler视图构造函数
中放置setHasStableIds(true)来解决此问题答案 0 :(得分:0)
来自Android documentation,
void setHasStableIds(boolean hasStableIds)
指示数据集中的每个项目是否都可以用a表示 Long类型的唯一标识符。
您必须为回收视图设置setHasStableIds(true)