我有一个程序实体,我想用RecyclerView在警报对话框中显示它们。我使用像binding.programAdapter = adapter
这样的数据绑定,但我不知道如何在布局中绑定adapter和recyclerView。为了澄清,我想使用数据绑定或其他工作方式在警报对话框中显示带有recyclerView的项目。唯一的问题是我不能写//binding.programAlertAdapter = adapter
。
主要活动
val adapter = ProgramAlertAdapter(this, it)
//binding.programAlertAdapter = adapter
val mDialogView = LayoutInflater.from(this).inflate(R.layout.program_of_today_alert_dialog, null)
val db = AlertDialog.Builder(this,R.style.MyDialogTheme).setView(mDialogView).setTitle("Programs of Today")
.show()
适配器
class ProgramAlertAdapter(
var mContext: Context,
var programList: List<Program>
) : RecyclerView.Adapter<ProgramAlertAdapter.CardViewHolder>() {
inner class CardViewHolder(binding: AlertProgramCardDesignBinding): RecyclerView.ViewHolder(binding.root) {
var binding: AlertProgramCardDesignBinding
init {
this.binding = binding
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CardViewHolder {
val layoutInflater = LayoutInflater.from(mContext)
val binding: AlertProgramCardDesignBinding = DataBindingUtil.inflate(layoutInflater,
R.layout.alert_program_card_design, parent, false)
return CardViewHolder(binding)
}
override fun onBindViewHolder(holder: CardViewHolder, position: Int) {
val program = programList[position]
val p = holder.binding
p.programObject = program
}
override fun getItemCount(): Int {
return programList.size
}
}
卡设计
<data>
<variable
name="programObject"
type="com.gorkemersizer.gymapp.data.entity.Program" />
</data>
...
<Cardview...
<TextView
android:text="@{programObject.program_name}"
...
警报对话框设计
...
<data>
<variable name="ProgramAlertAdapter" type="com.gorkemersizer.gymapp.ui.adapters.ProgramAlertAdapter" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvAlertProgram"
android:layout_width="0dp"
android:layout_height="0dp"
android:adapter="@{ProgramAlertAdapter}"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
enter code here
先谢谢你
1条答案
按热度按时间dtcbnfnu1#
从XML中删除
android:adapter="@{programAlertAdapter}"
,因为在这种情况下您不能使用数据绑定,需要以不同方式给予它,使用findViewById如下所示: