-
Notifications
You must be signed in to change notification settings - Fork 554
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[5.0.0-b6] Crash ClassCastException on addItem (with sticky headers) #79
Comments
Yes. It looks like related to my problem. I'm using this code
It doesn't happen every time but very very often. |
But it works OK when I turn off sticky headers support. |
I have the same problem,but not every time. |
I also, faced the problem , but not every time. |
Hello, I confirm that this bug exists, the technique used is to have a FrameLayout on the top of the RV and so the itemView is moved from RV.ViewHolder to the FrameLayout. I got this exception in my tests, but only with expandables sections with sticky headers enabled. Maybe I've found a solution, swapping the header in post so that LayoutManager can finish its elaboration. I'm having some improvements using the next 2 fixes: private void swapHeader(final RecyclerView.ViewHolder newHeader) {
mRecyclerView.post(new Runnable() {
@Override
public void run() {
if (mStickyHeaderViewHolder != null) {
resetHeader(mStickyHeaderViewHolder);
}
mStickyHeaderViewHolder = newHeader;
if (mStickyHeaderViewHolder != null) {
mStickyHeaderViewHolder.setIsRecyclable(false);
ensureHeaderParent();
}
onStickyHeaderChange(mHeaderPosition);
}
});
} private static void resetHeader(RecyclerView.ViewHolder header) {
final View view = header.itemView;
removeViewFromParent(view);
//Reset transformation on removed header
view.setTranslationX(0);
view.setTranslationY(0);
//Restore LayoutParams
view.setLayoutParams(new RecyclerView.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
header.setIsRecyclable(true);
} I commit these changes in dev branch and creating a new SNAPSHOT, if somebody want test it... |
I'm now observing that, using this fix, a little glitch is unfortunately visible onSwap header item. |
@davideas
This error is not there previously. |
So the fix is not ok. |
Yes, this fix is not ok. I didn't changed anything in the sample you provided. At the time of scrolling it's crashing sometimes. I am not sure, when its crashing. |
@francescopedronomnys, to initialize the list not empty, you should use the constructor not The new items seem to be added at the end, if so, how is possible you get exception on the sticky header? @tprochazka, Also when refreshing the full list, the method I'm now facing the issue of the class cast exception, but it's quite difficult to resolve. |
As workaround, please disable sticky header before any deletion that has an impact of changing the current sticky header, meaning, any deletion that also implies an automatic swap header due to the layout change. |
@davideas I'm overriding FlexibleAdapter and providing to super() a new empty ArrayList in my constructor. Is it correct? Here's the full adapter code, probably I'm doing something wrong: https://gist.github.com/francescopedronomnys/4ebea6da6e7b1070de21a0ada7b3fd83 I'm using addItems() because I have multiple callback from my db/network layer, each one for each section (a section is composed of one AbstractHeaderItem and many AbstractSectionableItem), so I do not have a single point when I can create my adapter with a list of items. Actually I'm not adding items always at the end, I've multiple sections with different items (actually different header and item classes) that I should keep in a specific order, and so I'm using this code to get the correct insertion position:
I get the ClassCastException removing/adding items, but only when I do a second insert/remove iteration; this is because I get data from local DB on the first iteration and than from network on the second one. Using the current dev SNAPSHOT I'm now getting this exception:
Adding disableStickyHeaders() and enableStickyHeaders() I'm getting this exception: Thanks for the padding thing, I'll look into it. Let me know if I can provide you more information, I understand that my usecase is not standard. |
@francescopedronomnys, adding empty list at the start should be ok. At first glance, the class cast on same Try to use disable->remove->add->enable with beta6, if it is not enough add a delay of few milliseconds from the removal OR use the post to queue the operations ( Until I don't find a new technique, the current solution is that the PS. It might be easier to handle sections with expandables (with expansion disabled) and subItems. |
I think this is urgen issue, did you have solution or a |
@mdtuyen, few days ago I found a new and good LayoutManager with sticky headers functionality, I will try to import and adapt based on my implementation. Unfortunately, it requires lot of time, and in this period it's really impossible for me to continue heavy development on this library. The library I found is: StickyHeaders. |
Thanks for your good news and your useful library. It is difficult to make a recycler with many feature as you see. I spend a lot of time to found a library combine many feature for recyclerview, there is some library but them have many and many bug. Your library is very best and easy to custom it. I'm making a fragment contain a recyclerview use your library to use it for many my projects. Thanks for good lib again. |
I fixed this issue by change method animateTo in FlexibleAdapter to:
|
@mdtuyen, well, then However, I got an illumination now, I could create a sub-layout in the itemView at the moment of the creation and change the parent of this layout not the entire itemView, in this way the main view is unaltered and RV should not complain anymore with its internal processing. |
So I think now I have resolved all these bugs by changing the constructor as following. But this is a temporary code and it should be adapted, because it must not interfere if one doesn't want sticky headers and it must easy to extend if one wants it instead. Instead of using directly public View stickyView;
/**
* Default constructor.
*
* @param view The {@link View} being hosted in this ViewHolder
* @param adapter Adapter instance of type {@link FlexibleAdapter}
*/
public FlexibleViewHolder(View view, FlexibleAdapter adapter) {
super(new FrameLayout(view.getContext()));
itemView.setLayoutParams(new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
view.getLayoutParams().height));
stickyView = view;
((ViewGroup) itemView).addView(stickyView);
this.mAdapter = adapter;
this.stickyView.setOnClickListener(this);
this.stickyView.setOnLongClickListener(this);
} Any suggestion to how extend/implement a new method/class considering the
|
I've committed the changes in dev branch, please make some experiments for the definitive fix, I wait some suggestions :-) |
Alright, I've checked the example app - issue #92 is fixed. It would be hard to test it in my app though, |
I tried some final solutions: 1 - Keep the above solution as final, means that, ALL ViewHolders have this extra layout created most of the time when not necessary. I can probably check public FlexibleViewHolder(View view, FlexibleAdapter adapter) {
super(adapter.areHeadersSticky() ? new FrameLayout(view.getContext()) : view);
this.mAdapter = adapter;
if (adapter.areHeadersSticky()) {
itemView.setLayoutParams(new FrameLayout.LayoutParams(
view.getLayoutParams().width,
view.getLayoutParams().height));
contentView = view;
((FrameLayout) itemView).addView(contentView);//Add View after setLayoutParams
contentView.setOnClickListener(this);
contentView.setOnLongClickListener(this);
} else {
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
}
} 2 - I can add a new Constructor in public FlexibleViewHolder(View view, FlexibleAdapter adapter, boolean stickyHeader) {
super(stickyHeader ? new FrameLayout(view.getContext()): view);
this.mAdapter = adapter;
if (stickyHeader) {
itemView.setLayoutParams(new FrameLayout.LayoutParams(
view.getLayoutParams().width,
view.getLayoutParams().height));
contentView = view;
((FrameLayout) itemView).addView(contentView);//Add View after setLayoutParams
contentView.setOnClickListener(this);
contentView.setOnLongClickListener(this);
} else {
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
}
} 3 - Create a new 4 - The new Suggestions? |
I'm not an expert on this, but I think that the second option with boolean parameter is the best way. |
The itemView with FrameLayout lost the elevation, how to show the shadow of the elevation?? I used |
@davideas, I had a problem with no shadows under cardview items in my list when I switched to your lib,
|
Hi,
thanks in advance for the library.
I'm using v 5.0.0-b6 to implement a list with multiple sections (different AbstractFlexibleItem classes for each section) and sticky headers.
At first I initialize the list with data from the DB (done using addItem() on the FlexibleAdapter).
Then as soon as updated data come from my network layer I proceed doing removeItemsOfType() and addItem() again.
I often get this exception that do not happen if I disable the network layer and so no further removeItemsOfType() and addItem() is performed before the first one.
This is the method I call when data is available:
Sometimes (rarely) I've seen the same exception as reported by tprochazka on issue #78, I don't know if they're related.
I don't know if I'm doing something wrong or it is a library issue.
Let me know if I can provide further info.
Here's the exception stacktrace:
Thanks,
Francesco
The text was updated successfully, but these errors were encountered: