在滚动

时间:2017-04-04 18:57:26

标签: android scroll crash android-recyclerview

当我尝试在recyclelerView(list)滚动时尝试向recyclerView / adapter插入新项目时出现问题。当列表不滚动但是当我拖动列表并尝试插入新项目时,一切正常还在滚动)它崩溃了。关于什么可能是问题的任何想法。这是我收到的错误消息:

java.lang.IllegalArgumentException: Tmp detached view should be removed from RecyclerView before it can be recycled: ViewHolder{2414800 position=0 id=-1, oldPos=-1, pLpos:-1 update tmpDetached no parent}
                                                                                           at android.support.v7.widget.RecyclerView$Recycler.recycleViewHolderInternal(RecyclerView.java:5429)
                                                                                           at android.support.v7.widget.RecyclerView.removeAnimatingView(RecyclerView.java:1242)
                                                                                           at android.support.v7.widget.RecyclerView$ItemAnimatorRestoreListener.onAnimationFinished(RecyclerView.java:11156)
                                                                                           at android.support.v7.widget.RecyclerView$ItemAnimator.dispatchAnimationFinished(RecyclerView.java:11656)
                                                                                           at android.support.v7.widget.SimpleItemAnimator.dispatchChangeFinished(SimpleItemAnimator.java:304)
                                                                                           at android.support.v7.widget.DefaultItemAnimator$7.onAnimationEnd(DefaultItemAnimator.java:363)
                                                                                           at android.support.v4.view.ViewPropertyAnimatorCompatJB$1.onAnimationEnd(ViewPropertyAnimatorCompatJB.java:51)
                                                                                           at android.view.ViewPropertyAnimator$AnimatorEventListener.onAnimationEnd(ViewPropertyAnimator.java:1121)
                                                                                           at android.animation.ValueAnimator.endAnimation(ValueAnimator.java:1153)
                                                                                           at android.animation.ValueAnimator.doAnimationFrame(ValueAnimator.java:1313)
                                                                                           at android.animation.AnimationHandler.doAnimationFrame(AnimationHandler.java:146)
                                                                                           at android.animation.AnimationHandler.-wrap2(AnimationHandler.java)
                                                                                           at android.animation.AnimationHandler$1.doFrame(AnimationHandler.java:54)
                                                                                           at android.view.Choreographer$CallbackRecord.run(Choreographer.java:872)
                                                                                           at android.view.Choreographer.doCallbacks(Choreographer.java:686)
                                                                                           at android.view.Choreographer.doFrame(Choreographer.java:618)
                                                                                           at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
                                                                                           at android.os.Handler.handleCallback(Handler.java:751)
                                                                                           at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                           at android.os.Looper.loop(Looper.java:154)
                                                                                           at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

这是xml:

    <android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/appBar"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/appBar"/>

on main:

    adapter = new SelectionDemoAdapter(sectionList);  // SelectionDemoAdapter contains two views header and item
    recyclerView.setLayoutManager(new StickyHeaderLayoutManager());
    recyclerView.setAdapter(adapter);
    recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
    recyclerView.setItemAnimator(new DefaultItemAnimator());

来自此处的适配器:ShamylZakariya StickyHeaders

public class SelectionDemoAdapter extends SectioningAdapter {

private ArrayList<SectionManager.Section> sectionList = new ArrayList<>();


public SelectionDemoAdapter(ArrayList<SectionManager.Section> sectionList) {
    this.sectionList=sectionList;
}

public class ItemViewHolder extends SectioningAdapter.ItemViewHolder {
    public View dateLine;
    public TextView memoTitle, memoCreator, memoCreationTime;
    public ImageView memoIcon, memoProtected;

    public ItemViewHolder(View itemView) {
        super(itemView);
        memoTitle = (TextView) itemView.findViewById(R.id.textViewMemoTitle);
        memoCreator = (TextView) itemView.findViewById(R.id.textViewMemoCreator);
        memoCreationTime = (TextView) itemView.findViewById(R.id.textViewMemoCreationTime);
        dateLine=(View) itemView.findViewById(R.id.dateLine);

        memoIcon=(ImageView) itemView.findViewById(R.id.imageViewMemoIcon);
        memoProtected =(ImageView) itemView.findViewById(R.id.imageViewProtected);
    }
}
public class HeaderViewHolder extends SectioningAdapter.HeaderViewHolder {
    TextView categoryTextView;
    TextView rightTextView;


    public HeaderViewHolder(View itemView) {
        super(itemView);
        categoryTextView = (TextView) itemView.findViewById(R.id.categoryTextView);
        rightTextView = (TextView) itemView.findViewById(R.id.rightTextView);
    }

}



@Override
public int getNumberOfSections() {
    return sectionList.size();
}

@Override
public int getNumberOfItemsInSection(int sectionIndex) {
    return sectionList.get(sectionIndex).memoArrayList.size();
}


@Override
public boolean doesSectionHaveHeader(int sectionIndex) {
    return !TextUtils.isEmpty(sectionList.get(sectionIndex).category);
}

@Override
public ItemViewHolder onCreateItemViewHolder(ViewGroup parent, int itemType) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View v = inflater.inflate(R.layout.memo_row_layout, parent, false);
    return new ItemViewHolder(v);
}

@Override
public HeaderViewHolder onCreateHeaderViewHolder(ViewGroup parent, int headerType) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View v = inflater.inflate(R.layout.list_item_selectable_header, parent, false);
    return new HeaderViewHolder(v);
}

@SuppressLint("SetTextI18n")
@Override
public void onBindItemViewHolder(SectioningAdapter.ItemViewHolder viewHolder, int sectionIndex, int itemIndex, int itemType) {
    ItemViewHolder ivh = (ItemViewHolder) viewHolder;
    //Log.d("esx","Memo adapter POSITION: "+position+" of "+memoList.size());
    SectionManager.Section section=sectionList.get(sectionIndex);
    Memo memo = section.memoArrayList.get(itemIndex);
    //memo.printALL();

    ivh.memoTitle.setText(memo.getMemoHeader().getTitle());
    ivh.memoTitle.setTextColor(MemoPriority.getPriorityType(memo.getMemoHeader().getPriority()).getColor());
    ivh.memoCreator.setText(memo.getMemoHeader().getCreator());
    ivh.dateLine.setBackgroundColor(MemoPriority.getPriorityType(memo.getMemoHeader().getPriority()).getColor());
    ivh.memoCreationTime.setText(TimeHelper.getDate(memo.getCreationTime(), TimeHelper.TimeFormat.HH_MM_SS));
    ivh.memoIcon.setImageResource(memo.getMemoHeader().getIconResourceID());
    if(memo.getMemoHeader().isProtected()) {
        ivh.memoProtected.setImageResource(Utils.getDrawableResourceID("protected_eye"));
        ivh.memoProtected.setColorFilter(MemoPriority.getPriorityType(memo.getMemoHeader().getPriority()).getColor(), PorterDuff.Mode.SRC_IN);
    }else {
        ivh.memoProtected.setImageResource(Utils.getDrawableResourceID("not_protected_eye"));
        ivh.memoProtected.setColorFilter(MemoPriority.getPriorityType(memo.getMemoHeader().getPriority()).getColor(), PorterDuff.Mode.SRC_IN);
    }

    ivh.itemView.setActivated(isSectionItemSelected(sectionIndex, itemIndex));

}

@SuppressLint("SetTextI18n")
@Override
public void onBindHeaderViewHolder(SectioningAdapter.HeaderViewHolder viewHolder, int sectionIndex, int headerType) {
    SectionManager.Section s = sectionList.get(sectionIndex);
    HeaderViewHolder hvh = (HeaderViewHolder) viewHolder;

    hvh.categoryTextView.setText(s.category);
    hvh.rightTextView.setText(s.rightText);

    hvh.itemView.setActivated(isSectionSelected(sectionIndex));
}

}

0 个答案:

没有答案