gpt4 book ai didi

android - 如何在 onBindViewHolder() 中的 notifyItemChange() 之后为 recyclerView 元素中的 subview 设置动画

转载 作者:行者123 更新时间:2023-12-04 12:50:22 24 4
gpt4 key购买 nike

我想为 textView 制作动画在 recyclerView 内当我调用 notifyItemChanged(position) 时向上过渡.这是我的onBindViewholder()当适配器发生更改时会自动调用它:

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
if (unfoldedIndexes.contains(position)) { // if already unfolded
bindUnfoldedState(holder, holder.getLayoutPosition());
}else{ // show the folded state}
}

public void bindUnfoldedState(ViewHolder holder, int position){


if(lc.isBtnEnabled()){
holder.Price.setText("text");
holder.Price.animate().alpha(1.0f).setDuration(500);
holder.Price.animate().translationY(-38).setDuration(500);
holder.checkText.animate().translationY(38).setDuration(500);
}else {
holder.Price.animate().alpha(0.0f).setDuration(500);
holder.Price.animate().translationY(0).setDuration(500);
holder.checkText.animate().translationY(0).setDuration(500);
}

}

我的 notifyItemChanged()正在从 fragment 调用 onClick像这样:
 ok.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
// some changes to the data
adapter.getLcns().get(Position).setBtnEnabled(true);
adapter.notifyItemChanged(Position);
}
}
isBtnEnabled()如果 s etBtnEnabled(true) 则为真叫做。

问题是动画没有发生。通过日志我可以确认 onBindViewHolder()正在调用并且动画应该出现但它没有发生。

我正在使用异常 listView在我转换为 recyclerView 之前并且动画正在处理 getView()

最佳答案

使用 Adapter.notifyItemChanged(position, payload) 将更新部署到已经可见的 ViewHolders:

点击:

ok.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
// some changes to the data
adapter.getLcns().get(Position).setBtnEnabled(true);
adapter.notifyItemChanged(Position, true); // "true" is the payload
}
}

适配器:
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position, List<Object> payloads) {
if(payloads != null && !payloads.isEmpty()){ // update payloads, partial rebind
Object lastPayload = payloads.get(payloads.size -1)); // in this case only last payload is meaningful
if(lastPayload instanceof Boolean)
bindUnfoldedState(holder, lastPayload);
}else
onBindViewHolder(holder, position); // regular binding of item
}

public void bindUnfoldedState(ViewHolder holder, boolean unfold){
if(unfold){ ... }
else { ... }
}

// you should also consider adding this override to prevent animation leaking when viewholder
// is recycled and reused for another position:
@Override
public void onViewRecycled(ViewHolder holder){
holder.Price.animate().cancel();
holder.checkText.animate().cancel();
}

关于android - 如何在 onBindViewHolder() 中的 notifyItemChange() 之后为 recyclerView 元素中的 subview 设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57871203/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com