Added View has RecyclerView as parent but view is not a real child

I have an issue appears sometimes when content of RecyclerView is changes. I have search view that filters data of RecyclerView. After filtering I call notifyDataSetChanged();

I use https://gist.github.com/mheras/0908873267def75dc746 as adapter.

I was thinking that issue related with adding footer, but after removing footer issue still appears, but not so often

Exception:

java.lang.IllegalStateException: Added View has RecyclerView as parent but view is not a real child. Unfiltered index:0 at android.support.v7.widget.RecyclerView$LayoutManager.addViewInt(RecyclerView.java:7048) at android.support.v7.widget.RecyclerView$LayoutManager.addView(RecyclerView.java:7012) at android.support.v7.widget.RecyclerView$LayoutManager.addView(RecyclerView.java:7000) at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1428) at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1377) at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:578) at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3260) at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3069) at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3518) at android.view.View.layout(View.java:16636) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:598) at android.vie w.View.layout(View.java:16636) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) at android.widget.FrameLayout.onLayout(FrameLayout.java:273) at android.view.View.layout(View.java:16636) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) at android.widget.FrameLayout.onLayout(FrameLayout.java:273) at android.view.View.layout(View.java:16636) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) at android.widget.FrameLayout.onLayout(FrameLayout.java:273) at android.view.View.layout(View.java:16636) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) at android.widget.FrameLayout.onLayout(FrameLayout.java:273) at android.view.View.layout(View.java:16636) at android.view.ViewGroup.layout(ViewGroup.java:5437) at andr oid.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:435) at android.view.View.layout(View.java:16636) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) at android.widget.FrameLayout.onLayout(FrameLayout.java:273) at android.view.View.layout(View.java:16636) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586) at android.widget.LinearLayout.onLayout(LinearLayout.java:1495) at android.view.View.layout(View.java:16636) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) at android.widget.FrameLayout.onLayout(FrameLayout.java:273) at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678) at android.view.View.layout(View.java:16636) at android.view.ViewGroup.l ayout(ViewGroup.java:5437) at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858) at android.view.Choreographer.doCallbacks(Choreographer.java:670) at android.view.Choreographer.doFrame(Choreographer.java:606) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Nat


我碰到了同样的问题...我解决它通过充气ViewHolderView使用Contextparent.getContext()并指定false的调用inflate

final override fun onCreateViewHolder(parent:ViewGroup,viewType:Int):RecyclerView.ViewHolder
{
    View view = LayoutInflater.from(parent.getContext())
        .inflate(R.layout.listitem,parent,false);
    return new RecyclerView.ViewHolder(view) {};
}

我们通过编写自己的适配器实际解决它:

public abstract class BaseRVAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    static final int HEADER_VIEW_TYPE = Integer.MAX_VALUE;
    static final int FOOTER_VIEW_TYPE = Integer.MAX_VALUE - 1;

    private BaseRVAdapterHeaderImpl adapterHeader;

    private BaseRVAdapterFooterImpl adapterFooter;

    public void setFooterImpl(BaseRVAdapterFooterImpl footerImpl) {
        adapterFooter = footerImpl;
    }

    public void setHeaderImpl(BaseRVAdapterHeaderImpl headerImpl) {
        adapterHeader = headerImpl;
    }

    public abstract RecyclerView.ViewHolder onCreateContentItemViewHolder(ViewGroup parent, int viewType);

    public abstract void onBindContentItemViewHolder(RecyclerView.ViewHolder holder, int position);

    public abstract int getContentItemCount();

    public int getContentItemViewType(int position) {
        return 0;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == HEADER_VIEW_TYPE) {
            return new HeaderViewHolder(adapterHeader.getHeaderView());
        }

        if (viewType == FOOTER_VIEW_TYPE) {
            return new FooterViewHolder(adapterFooter.getFooterView());
        }

        return onCreateContentItemViewHolder(parent, viewType);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HeaderViewHolder) {
            //bind header view holder
        } else if (holder instanceof FooterViewHolder) {
            adapterFooter.onBind(holder.itemView);
        } else {
            onBindContentItemViewHolder(holder, position - getHeaderItemCount());
        }
    }

    @Override
    public int getItemCount() {
        return getContentItemCount() + getHeaderItemCount() + getFooterItemCount();
    }

    @Override
    public int getItemViewType(int position) {
        if (hasHeader() && position < getHeaderItemCount()) {
            return HEADER_VIEW_TYPE;
        }

        if (position == getItemCount() - 1 && hasFooter()) {
            return FOOTER_VIEW_TYPE;
        }

        return getContentItemViewType(position - getHeaderItemCount());
    }

    public int getAdapterContentPosition(RecyclerView.ViewHolder holder) {
        return holder.getAdapterPosition() - getHeaderItemCount();
    }

    public void notifyContentItemRangeInserted(int positionStart, int itemCount) {
        notifyItemRangeInserted(positionStart + getHeaderItemCount(), itemCount);
    }

    public void notifyContentItemRangeRemoved(int positionStart, int itemCount) {
        notifyItemRangeRemoved(positionStart + getHeaderItemCount(), itemCount);
    }

    public void notifyContentItemRemoved(int position) {
        notifyItemRemoved(position + getHeaderItemCount());
    }

    public void notifyContentItemChanged(int position) {
        notifyItemChanged(position + getHeaderItemCount());
    }

    public void notifyFooterItemChanged() {
        notifyItemChanged(getContentItemCount() + getFooterItemCount());
    }

    public boolean hasHeader() {
        return adapterHeader != null;
    }

    boolean hasFooter() {
        return adapterFooter != null;
    }

    public int getHeaderItemCount() {
        return hasHeader() ? 1 : 0;
    }

    public int getFooterItemCount() {
        return hasFooter() ? 1 : 0;
    }

    public interface BaseRVAdapterHeaderImpl {
        View getHeaderView();
    }

    public static abstract class BaseRVAdapterFooterImpl {
        public abstract View getFooterView();

        public void onBind(View view) {

        }
    }

    private class HeaderViewHolder extends RecyclerView.ViewHolder {
        HeaderViewHolder(View itemView) {
            super(itemView);
        }
    }

    private class FooterViewHolder extends RecyclerView.ViewHolder {
        FooterViewHolder(View itemView) {
            super(itemView);
        }
    }

    public RecyclerView.ItemDecoration getBottomItemDecoration() {
        int marginBottom = App.getContext().getResources().getDimensionPixelSize(R.dimen.dimen_small);

        return new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);

                int position = parent.getChildAdapterPosition(view);
                int contentItemCount = getContentItemCount();
                if (position == contentItemCount) {
                    outRect.bottom = marginBottom;
                }
            }
        };
    }
}
链接地址: http://www.djcxy.com/p/93160.html

上一篇: 滚动时添加标题时不能绑定更多的BindViewHolder recyclerview

下一篇: 添加视图具有RecyclerView作为父视图,但视图不是真正的孩子