Comments (4)
This has nothing to do with ExpandableLayout.
The issue is that RecyclerView is recycling your list item views (hence the name - ha!) and the view's state is maintained unless you reset it. In your example, your ExpandableLayout used for item 1 is being reused for item 8 when you scroll and since it was expanded for item 1, it will retain the expanded state even when the content is replaced with item 8's.
The fix is to reset the state in your adapter's bindViewHolder
method using either setExpanded
or collapse
. Be sure to use the versions where you can set the animate
flag to false, otherwise you will briefly see animations while scrolling the recycler view.
If you want to make it really simple, you can always call expandableLayout.collapse(false)
in the bind method and be done with it. This will cause previously expanded items to always appear collapsed even if the user expands it, scrolls away and then returns.
A more complex implementation would need to remember the expanded state for each item and set expandableLayout.setExpanded(state, false)
accordingly.
from expandablelayout.
Hi,
Yeah thanks for this. I figured out it was RecyclerView related, rather than this library, while Googling around.
I was going to come back with my solution once I worked it out. I still will, in the interests of helping anyone else finding this!
from expandablelayout.
I ended up going for adding a boolean to my model interface that helps track the state of cards.
/**
* Expand a card
*
* @param holder Viewholder, used to get the card items
* @param position The position of the card to expand
* @param animate Animate the expand/collapse
*/
private void expandCard(ListViewHolder holder, int position, boolean animate) {
String collapseIcon = context.getResources().getString(R.string.mc_chevron_up);
if (holder.mExpandIcon != null) {
holder.mExpandIcon.setText(collapseIcon);
}
holder.mDetailedView.expand(animate);
items.get(position).getFileModel().setExpandedState(true);
}
/**
* Collapse a card
*
* @param holder Viewholder, used to get the card items
* @param position The position of the card to collapse
* @param animate Animate the expand/collapse
*/
private void collapseCard(ListViewHolder holder, int position, boolean animate) {
String expandIcon = context.getResources().getString(R.string.mc_chevron_down);
if (holder.mExpandIcon != null) {
holder.mExpandIcon.setText(expandIcon);
}
holder.mDetailedView.collapse(animate);
items.get(position).getFileModel().setExpandedState(false);
}
/**
* Toggle the expanded state of a card at the given position
*
* @param holder The viewholder, used to get the card items
* @param position The position that was clicked
*/
public void toggleExpandedCard(ListViewHolder holder, int position) {
// If nothing is expanded, expand as normal
if (expandedPosition == RecyclerView.NO_POSITION) {
expandCard(holder, position, true);
expandedPosition = position;
return;
}
// If not clicking the currently expanded item
// Close the other first
if (expandedPosition != position) {
ListViewHolder expandedViewHolder
= (ListViewHolder) recyclerView.findViewHolderForAdapterPosition(expandedPosition);
// Collapse the currently expanded card, if it is not null/is visible
// Else set it to be closed to the bindviewholder closes it next time it's bound
if (expandedViewHolder != null) {
collapseCard(expandedViewHolder, expandedPosition, true);
} else {
items.get(expandedPosition).getFileModel().setExpandedState(false);
}
expandCard(holder, position, true);
expandedPosition = position;
return;
}
collapseCard(holder, position, true);
expandedPosition = RecyclerView.NO_POSITION;
}
@Override
public void onBindViewHolder(final ListViewHolder holder, final int position) {
// Addon Item at latest position in the list
final FileModel item = items.get(position).getFileModel();
// Set the expanded state on
if (item.getExpandedState()) {
expandCard(holder, position, false);
} else {
if (holder.mDetailedView.isExpanded()) {
collapseCard(holder, position, false);
}
}
...
}
Going to go through and clean up the logic but that's basically it. Just in case anyone finds this via Google and has the same issue as I did.
BTW, this is the best Expandable Layout library for Android. I tried the others but this one did exactly what I wanted without any fluff.
from expandablelayout.
Thanks!
from expandablelayout.
Related Issues (20)
- Does this support scroll to position when expanded? HOT 2
- Expandable Layout not working in coordinator layout, But working smoothly in linear layout HOT 6
- 3 fill parent Accordion menus HOT 7
- Resetting child view when expanding other view HOT 4
- At a time one expand HOT 2
- Can I expand multiple views at the same time? HOT 3
- Request: Linear layout as root. HOT 3
- Scroll RecyclerView to show selected item expand on top HOT 1
- Indication Markers HOT 1
- Starting expanded HOT 1
- Start expanded HOT 1
- Move away from JCenter HOT 1
- library wont expand on androidx.constraintlayout:constraintlayout:2.0.4 HOT 1
- JCenter is scheduled for closing HOT 1
- mavenCentral() support HOT 1
- mavenCentral() support HOT 1
- Toggle with out clicking on expandable view
- toped working after went to MavenCentral() HOT 2
- How can expand the fist group programatically? HOT 1
- Scrolling to item position is not working
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from expandablelayout.