添加的 View 将 RecyclerView 作为父级,但视图不是真正的子级
Posted
技术标签:
【中文标题】添加的 View 将 RecyclerView 作为父级,但视图不是真正的子级【英文标题】:Added View has RecyclerView as parent but view is not a real child 【发布时间】:2017-01-25 23:10:55 【问题描述】:当 RecyclerView 的内容发生变化时,有时会出现问题。我有过滤 RecyclerView 数据的搜索视图。过滤后我打电话给notifyDataSetChanged();
我使用this 作为适配器。
我在想这个问题与添加页脚有关,但在删除页脚后问题仍然出现,但不是那么频繁
例外:
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.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 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.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.layout(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
【问题讨论】:
得到了这个问题,你有没有机会解决这个问题? 【参考方案1】:我遇到了同样的问题...我通过使用来自 parent.getContext()
的 Context
膨胀 ViewHolder
的 View
并指定 @987654325 来解决它@ 在对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) ;
【讨论】:
【参考方案2】:我们实际上是通过编写自己的适配器来解决的:
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;
;
【讨论】:
以上是关于添加的 View 将 RecyclerView 作为父级,但视图不是真正的子级的主要内容,如果未能解决你的问题,请参考以下文章