Git Product home page Git Product logo

expandablerecyclerview's Introduction

ExpandableRecyclerView

Use recycler view to create expandable List View.

To complete solution of Expandable list View and Section View.
Multiple View attach in single recycler view.

How to use it

Step 1

Extends ExpandableRecyclerViewAdapter

Make adapter class and extends ExpandableRecyclerViewAdapter to Override getViewHolder method that returns the object of ExpandableViewHolder.

here the example of adapter:-
    ExpandableRecyclerViewAdapter adapter = new ExpandableRecyclerViewAdapter(addChild()) {
         @Override
         protected ExpandableViewHolder getViewHolder(int resource, ViewGroup parent) {
            switch (resource) {
                 case R.layout.list_item:
                     return new MainViewHolder(parent, resource);
                 case R.layout.sub_list1:
                     return new SubItemOneHolder(parent, resource);
                 case R.layout.sub_list2:
                     return new SubItemTwoHolder(parent, resource);
               }
            return null;
            }
        };

Step 2

Make Multiple Holder for Multiple Layouts

Extends the ExpandableViewHolder and override methods
MainViewHolder
public class MainViewHolder extends ExpandableViewHolder {
    private TextView itemName;
    private TextView itemCount;

    public MainViewHolder(@NonNull ViewGroup parent, int resource) {
        super(parent, resource);
    }

    @Override
    public void bindIds(View itemView) {
        itemName = (TextView) itemView.findViewById(R.id.item_name);
        itemCount = (TextView) itemView.findViewById(R.id.item_count);
    }

    @Override
    public void setData(ExpandableAttribute data) {
        itemName.setText(((ExpandableParent) data).getItemName());
        itemCount.setText(((ExpandableParent) data).getItemCount());
    }

    @Override
    public void onCollapse(ExpandableAttribute data) {
        // do on Collapse the view
    }

    @Override
    public void onExpand(ExpandableAttribute data) {

    }
}
SubItemOneHolder
public class SubItemOneHolder extends ExpandableViewHolder {
    private CheckBox checkBox;

    public SubItemOneHolder(@NonNull ViewGroup parent, int resource) {
        super(parent, resource);
    }

    @Override
    public void bindIds(View itemView) {
        checkBox = (CheckBox) itemView.findViewById(R.id.checkbox);
    }

    @Override
    public void setData(ExpandableAttribute data) {
        checkBox.setText(((ExpandableChild) data).getValue());
    }

    @Override
    public void onCollapse(ExpandableAttribute data) {

    }

    @Override
    public void onExpand(ExpandableAttribute data) {

    }
}
SubItemTwoHolder
public class SubItemTwoHolder extends ExpandableViewHolder {
    private RadioButton radioButton;

    public SubItemTwoHolder(@NonNull ViewGroup parent, int resource) {
        super(parent, resource);
    }

    @Override
    public void bindIds(View itemView) {
        radioButton = (RadioButton) itemView.findViewById(R.id.radio_btn);
    }

    @Override
    public void setData(ExpandableAttribute data) {
        radioButton.setText(((ExpandableChild) data).getValue());
    }

    @Override
    public void onCollapse(ExpandableAttribute data) {

    }

    @Override
    public void onExpand(ExpandableAttribute data) {

    }
}

Step 3

Make Model According to layouts.

Extend ExpandableAttribute and store data as need.

Use following method to handle RecyclerView

  1. setExpand(boolean isExpand)
    to define for expand or collapse the child views.
    if isExpand == true then its need to expand childs.

  2. hasChild(bolean hasChild)
    to define if ExpandableAttribute has any Child.

  3. doNotCollapse()
    to define if parent or child doesn't need to collapse on click.

ExpandableParent
public class ExpandableParent extends ExpandableAttribute {

    private String itemName;
    private String itemCount;

    public ExpandableParent(int resourceId, String itemName, String itemCount) {
        super(resourceId);
        this.itemCount = itemCount;
        this.itemName = itemName;
    }

    protected ExpandableParent(int resourceId, ArrayList<ExpandableAttribute> expandableChildren) {
        super(resourceId, expandableChildren);
    }

    public String getItemName() {
        return itemName;
    }

    public String getItemCount() {
        return itemCount;
    }
}
ExpandableChild
public class ExpandableChild extends ExpandableAttribute {
    private String name;

    public ExpandableChild(int resourceId) {
        super(resourceId);
    }

    public void setValue(String name) {
        this.name = name;
    }

    public String getValue() {
        return name;
    }
}

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.