gpt4 book ai didi

android-recyclerview - RecyclerView.Adapter 通过position获取ItemView

转载 作者:行者123 更新时间:2023-12-05 09:19:18 28 4
gpt4 key购买 nike

在我的 Adapter 中,我调用 LayoutManager.ChildAt(position) 来获取 itemview,但是我得到的 View 不是匹配的 itemview,当我调用 notifyItemChanged(position) 时,应用崩溃:

E/AndroidRuntime: FATAL EXCEPTION: mainProcess: com.paicaifu.riches, PID: 8502
Java.lang.IllegalArgumentException: Called attach on a child which is not detached: ViewHolder{adb21588 position=0 id=-1, oldPos=-1, pLpos:-1}
at Android.support.v7.widget.RecyclerView$5.attachViewToParent(RecyclerView.java:654)
at android.support.v7.widget.ChildHelper.attachViewToParent(ChildHelper.java:239)
at android.support.v7.widget.RecyclerView.addAnimatingView(RecyclerView.java:1107)
at android.support.v7.widget.RecyclerView.animateChange(RecyclerView.java:3270)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep3(RecyclerView.java:3088)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2917)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3283)

at android.view.Choreographer.doCallbacks(Choreographer.java:574)

谢谢,这是我在适配器中的代码:

    @Override
public void onBindViewHolder(RecyclerView.ViewHolder holder,
final int position) {
if (holder instanceof CardHolder) {
final CardHolder cardHolder = (CardHolder) holder;
cardHolder.rootview.setOnClickListener(this);
cardHolder.rootview.setTag(position);
cardHolder.rootview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//get last itemview in adapter,but preView isnot match the
//position,it is the visibable views on screen
View preView = mLinearLayoutManager.getChildAt(preIndex);
//change the datasource
mData.getResult().getCardList().get(preIndex).setOpen(false);
//update item
notifyItemChanged(preIndex);//when the item is out of screen,
//this line will cause crashes
preIndex = position;
}
});
}

我只想找到最后一个项目并更改它的状态(打开或关闭),当项目超出屏幕时,notifyItemChanged(int position) 会出现问题。

最佳答案

这可能有点太晚了,但是当您没有在 onCreateViewHolder 中创建新 View 而是返回缓存 View 时,通常会发生“调用附加到未分离的子项”的崩溃已经充气并贴上了。

关于android-recyclerview - RecyclerView.Adapter 通过position获取ItemView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41161607/

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