Code Monkey home page Code Monkey logo

Comments (4)

cachapa avatar cachapa commented on July 21, 2024

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.

MatthewBooth avatar MatthewBooth commented on July 21, 2024

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.

MatthewBooth avatar MatthewBooth commented on July 21, 2024

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.

cachapa avatar cachapa commented on July 21, 2024

Thanks!

from expandablelayout.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.