添加视图具有RecyclerView作为父视图,但视图不是真正的孩子
RecyclerView的内容发生变化时,有时会出现问题。 我有搜索视图,筛选RecyclerView的数据。 过滤后,我调用notifyDataSetChanged();
我使用https://gist.github.com/mheras/0908873267def75dc746作为适配器。
我认为这个问题与添加页脚有关,但是在删除页脚问题后仍然出现,但并不经常
例外:
java.lang.IllegalStateException:添加视图有RecyclerView作为父视图,但视图不是真正的孩子。 未经过滤的索引:android.support.v7.widget.RecyclerView上的0:android.support.v7.widget.RecyclerView上的$ LayoutManager.addView(RecyclerView.java:7012)在android.support上的$ LayoutManager.addViewInt(RecyclerView.java:7048) .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)在android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3518)在android.view.View.layout(View.java:16636)在android.view .ViewGroup.layout(ViewGroup.java:5437)在android.vie上android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:598) 在android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)android.widget.FrameLayout.onLayout上的android.view.ViewGroup.layout(ViewGroup.java:5437)上的View.layout(View.java:16636) (FrameLayout.java:273)在android.view.View.layout(View.java:16636)在android.view.ViewGroup.layout(ViewGroup.java:5437)在android.widget.FrameLayout.layoutChildren(FrameLayout.java: 336)在android.widget.FrameLayout.onLayout(FrameLayout.java:273)在android.view.View.layout(View.java:16636)在android.view.ViewGroup.layout(ViewGroup.java:5437)在机器人。在android.view.ViewGroup.layout上的android.view.View.layout(View.java:16636)android.widget.FrameLayout.onLayout(FrameLayout.java:273)上的widget.FrameLayout.layoutChildren(FrameLayout.java:336) (ViewGroup.java:5437)在android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)在android.widget.FrameLayout.onLayout(FrameLayout.java:273)在android.view.View.layout(View.java: 16636)在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)在android.view.ViewGroup的android.view.View.layout(View.java:16636)处的android.widget.FrameLayout.onLayout(FrameLayout.java:273)。在android.widget.LinearLayout.java:1586 android.widget.LinearLayout.onLayout(LinearLayout.java)android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)的布局(ViewGroup.java:5437) :1495)在android.view.View.layout(View.java:16636)在android.view.ViewGroup.layout(ViewGroup.java:5437)在android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)在机器人.widget.FrameLayout.onLayout(FrameLayout.java:273)在com.android.internal.policy.PhoneWindow $ DecorView.onLayout(PhoneWindow.java:2678)在android.view.View.layout(View.java:16636)在android.view.ViewGroup.l 在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1931)android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171)上的ayout(ViewGroup.java:5437) :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)在android.os.Looper.loop(Looper.java:148)在android.app.ActivityThread.main(ActivityThread.java:5417)在java.lang.reflect中.Method.invoke(NAT
我碰到了同样的问题...我解决它通过充气ViewHolder
的View
使用Context
从parent.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/93159.html
上一篇: Added View has RecyclerView as parent but view is not a real child