出于某種原因,我必須在配接器內部做一個操作。換句話說,滾動時不應重復此程序。活動中的 onCreate 方法在活動打開時起作用一次。有沒有一種方法可以在配接器內部執行此操作?
更新
我的配接器類:
class EducationAdapter @Inject constructor(
var userManager: UserManager
) : ListAdapter<EducationModel, RecyclerView.ViewHolder>.
(EDUCATION_ITEM_DIFF_CALLBACK) {
companion object {
}
var callBack: EducationAdapterCallBack? = null
interface EducationAdapterCallBack {
fun onClickLayer(educationModel: EducationModel)
}
class EducationViewHolder(var binding: ItemEducationBinding) :
RecyclerView.ViewHolder(binding.root) {
fun setItem(item: EducationModel) {
binding.educationItem = item
}
}
@Nonnull
override fun onCreateViewHolder(
@Nonnull parent: ViewGroup,
viewType: Int
): RecyclerView.ViewHolder {
val binding: ItemEducationBinding = DataBindingUtil.inflate(
LayoutInflater.from(parent.context),
R.layout.item_education, parent, false
)
return EducationAdapter.EducationViewHolder(binding)
}
override fun onBindViewHolder(@Nonnull holder: RecyclerView.ViewHolder, position: Int) {
val item = getItem(position)
when (holder) {
is EducationAdapter.EducationViewHolder -> {
holder.setItem(item)
holder.binding.itemEducation.setOnClickListener {
callBack?.onClickLayer(item)
}
}
}
}
}
滾動串列視圖時呼叫 OncreateViewHolder 和 onBindViewHolder。但是我想要一個單發程序有沒有任何方法可以運行一次?
更新 2
我創建了一個dummy operation(我的原始代碼太長了,我創建了一個summary dummy operation)。OnBind 方法應該是這樣的:
override fun onBindViewHolder(@Nonnull holder: RecyclerView.ViewHolder, position: Int) {
val item = getItem(position)
when (holder) {
is EducationAdapter.EducationViewHolder -> {
holder.setItem(item)
holder.binding.itemEducation.setOnClickListener {
callBack?.onClickLayer(item)
}
var isExpandClicked = false
holder.binding.btnExpandProject.setOnClickListener {
if (isExpandClicked) {
isExpandClicked = false
val context = holder.binding.llTaskFaaliyet.context
holder.binding.btnExpandProject.setImageDrawable(
ContextCompat.getDrawable(
context,
R.drawable.ic_arrow_down
)
)
val headerText = TextView(context)
headerText.apply {
text = subTask.title
textSize = 16f
setTextColor(ContextCompat.getColor(context, R.color.marine))
gravity = Gravity.START
typeface = ResourcesCompat.getFont(context, R.font.ubuntu_bold)
setPadding(48, 16, 4, 0)
setBackgroundColor(ContextCompat.getColor(context, R.color.white))
}
holder.binding.llTask.add(headerText)
}else{
isExpandClicked = true
val context = holder.binding.llTask.context
holder.binding.btnExpandProject.setImageDrawable(
ContextCompat.getDrawable(
context,
R.drawable.ic_close
)
)
holder.binding.llTask.removeAllViews()
}
}
}
}
}
我正在嘗試在下面創建這些視圖:
展開視圖:

縮小視圖

第一次打開時應擴大視野。創建視圖時應該是單次的。
uj5u.com熱心網友回復:
如果你把這個函式放在 bind 方法上,它應該在顯示專案時被觸發。你可以做一個簡單的方法,在配接器作用域上使用布林值,在擴展函式被觸發后它會變成 false。然后使用這個布林值來檢查值是否為真,所以它應該觸發擴展函式。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/403189.html
標籤:
上一篇:com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:需要一個字串,但在第1行第39列路徑$.message處為BEG
下一篇:我正在傳遞日期索引而不是預期日期
