ShamylZakariya / StickyHeaders

Adapter and LayoutManager for Android RecyclerView which enables sticky header positioning.
MIT License
1.4k stars 185 forks source link

ViewHolder views must not be attached when created? #87

Open 1ok1 opened 6 years ago

1ok1 commented 6 years ago

02-28 05:16:37.431 1676-1676/com.elite.foodrepublic.dev E/AndroidRuntime: FATAL EXCEPTION: main Process: com.elite.foodrepublic.dev, PID: 1676 java.lang.IllegalStateException: ViewHolder views must not be attached when created. Ensure that you are not passing 'true' to the attachToRoot parameter of LayoutInflater.inflate(..., boolean attachToRoot) at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6687) at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5869) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5752) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5748) at org.zakariya.stickyheaders.StickyHeaderLayoutManager.onLayoutChildren(StickyHeaderLayoutManager.java:200) at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3812) at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3529) at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:4082) at android.view.View.layout(View.java:19659) at android.view.ViewGroup.layout(ViewGroup.java:6075) at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1083) at android.view.View.layout(View.java:19659) at android.view.ViewGroup.layout(ViewGroup.java:6075) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at android.view.View.layout(View.java:19659) at android.view.ViewGroup.layout(ViewGroup.java:6075) at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1083) at android.view.View.layout(View.java:19659) at android.view.ViewGroup.layout(ViewGroup.java:6075) at android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1191) at android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:876) at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:895) at android.view.View.layout(View.java:19659) at android.view.ViewGroup.layout(ViewGroup.java:6075) at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1171) at android.view.View.layout(View.java:19659) at android.view.ViewGroup.layout(ViewGroup.java:6075) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at android.view.View.layout(View.java:19659) at android.view.ViewGroup.layout(ViewGroup.java:6075) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635) at android.widget.LinearLayout.onLayout(LinearLayout.java:1544) at android.view.View.layout(View.java:19659) at android.view.ViewGroup.layout(ViewGroup.java:6075) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at android.view.View.layout(View.java:19659) at android.view.ViewGroup.layout(ViewGroup.java:6075) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635) at android.widget.LinearLayout.onLayout(LinearLayout.java:1544) at android.view.View.layout(View.java:19659) at android.view.ViewGroup.layout(ViewGroup.java:6075) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at com.android.internal.policy.DecorView.onLayout(DecorView.java:761) at android.view.View.layout(View.java:19659) at android.view.ViewGroup.layout(ViewGroup.java:6075) at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2496) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2212) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1392) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6752) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911) 02-28 05:16:37.431 1676-1676/com.elite.foodrepublic.dev E/AndroidRuntime: at android.view.Choreographer.doCallbacks(Choreographer.java:723) at android.view.Choreographer.doFrame(Choreographer.java:658) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897) at android.os.Handler.handleCallback(Handler.java:790) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

1ok1 commented 6 years ago
@Override
public ItemCategoryViewAdapter.ViewHolder onCreateItemViewHolder(ViewGroup parent, int itemType) {
    View view = LayoutInflater.from(context).inflate(R.layout.indiv_stall_detail_category, parent, false);
    return new ItemCategoryViewAdapter.ViewHolder(view);
}

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

Code reference for the above error.

Thanks in advance

Dona278 commented 6 years ago

I had the same error after updating support libraries to version 27.1.0

Dona278 commented 6 years ago

@elitelokesh seems to be caused by onCreateGhostHeaderViewHolder in SectioningAdapter.java and related to this pull request.

The parent.addView() method used here attach the view to the parent, so the problem aren't onCreateItemViewHolder or onCreateHeaderViewHolder custom implementation.

I found this workaround: override onCreateGhostHeaderViewHolder method into custom adapter like the others

@Override
public GhostHeaderViewHolder onCreateGhostHeaderViewHolder(ViewGroup parent) {
    final View ghostView = new View(parent.getContext());
    ghostView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

    return new GhostHeaderViewHolder(ghostView);
}
chaochaox1990 commented 6 years ago

@Dona278 your answer really help me Thanks. My problem is also 27.1.0 not working on that .

KennyGoers commented 6 years ago

Is there a release pending?

milanPansuriya commented 6 years ago

I am facing the same issue is there any update on this issue?

CeccoCQ commented 6 years ago

Same issue.

milanPansuriya commented 6 years ago

I had found the solution for this plz update your code `public GhostHeaderViewHolder onCreateGhostHeaderViewHolder(ViewGroup parent) { LayoutInflater inflater = LayoutInflater.from(parent.getContext());

View v = inflater.inflate(R.layout.ghost_view, parent, false); return new GhostHeaderViewHolder(v); }`

Please add an empty layout to onCreateGhostHeaderViewHolder

WeiLianYang commented 6 years ago

@Dona278 Thanks very much! I've solved the problem.

kshipraShelke commented 6 years ago

same error

rion18 commented 6 years ago

I tried @Dona278's solution but now my recyclerview is completely empty. Any ideas? Debugging execution, I noticed that after onCreateGhostHeaderViewHolder, no onCreateItemViewHolder is being called. I have a NestedScrollView, and inside is the RecyclerView.

Vishal99784 commented 6 years ago

@Dona278 Thanks very much! I've solved the problem.

Arxing commented 5 years ago

@Dona278 Thanks!!! I solved the problem.

Divala commented 5 years ago

@Dona278 thanks!!

sacdulanga commented 5 years ago

@Dona278 Thank you!

chandmohd commented 5 years ago

@Dona278 hey Thank you , I have migrated my project into AndoridX so had a problem so i tried your solution and it perfectly worked !

Dona278 commented 5 years ago

After a year seeing that my workaround still helps makes me happy! 🎉

TothSteven commented 5 years ago

@Dona278 Thank you! You saved the day for me :)

MuruganSKutty commented 5 years ago

I tried @Dona278 solution now above issue cleared but recyclerview is empty(showing nothing). I'm using 'com.android.support:appcompat-v7:27.1.1' & 'org.zakariya.stickyheaders:stickyheaders:0.7.6'. Please update if any solution.

TothSteven commented 5 years ago

Hi @MuruganSKutty I also use 'com.android.support:appcompat-v7:27.1.1' and 'org.zakariya.stickyheaders:stickyheaders:0.7.6' and for me works perfectly

MuruganSKutty commented 5 years ago

I found another cause of invisible recyclerview, i.e (Recylerview is present inside the NestedScrollView). Please any help @Dona278 , @TothSteven .

TothSteven commented 5 years ago

Hi @MuruganSKutty I'm using it in a tabLayout with fragments and initialize the sectioningAdapter in the Fragments onCreate and poputale the items in the Fragments onResume, have not tested it with NestedScrollView

ShamylZakariya commented 5 years ago

Hey I want to apologize to everybody. My job has been very demanding, and I've had to ignore all this. I've fixed the bug, and am upgrading to androidx, newer gradle etc. I will push a release in the next few days.

Crasheng commented 5 years ago

@Dona278 hey Thank you , I have migrated my project into AndoridX so had a problem so i tried your solution and it perfectly worked !

@Dona278 @chandmohd i have updated to AndroidX and in my customer Adapter the one extending the SectionAdapter i have override the same method and it still gives me the same problem

NandagopalR commented 4 years ago

@elitelokesh seems to be caused by onCreateGhostHeaderViewHolder in SectioningAdapter.java and related to this pull request.

The parent.addView() method used here attach the view to the parent, so the problem aren't onCreateItemViewHolder or onCreateHeaderViewHolder custom implementation.

I found this workaround: override onCreateGhostHeaderViewHolder method into custom adapter like the others

@Override
public GhostHeaderViewHolder onCreateGhostHeaderViewHolder(ViewGroup parent) {
    final View ghostView = new View(parent.getContext());
    ghostView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

    return new GhostHeaderViewHolder(ghostView);
}

thanks man. it's really working

divy9t commented 4 years ago

@Dona278 thanks for the answer mate, it's really helpful.