Android Expandable RecyclerView

Android Expandable RecyclerView – In this article I will explain how to implement the expandable recyclerview android example on android studio platform. When our application meet the recyclerview its look like very awesome. So developers are create their app in recyclerview format. Suppose you have develop quiz app or something related to that you need expandable options for view the answers. Its mandatory for some android and iOS mobile application.

android expandable recyclerview

Through this application you have to modify and customize as your app need. If you want to develop online examination based application then this source code perfectly matched. No need to help database help. Data are stored and accessed within the java file. We can also integrate database when we use more storage like add more questions.

Create Project

Okay lets start to implement our project for make expandable recyclerview android project. After creating the project and package name, first we need to design the choice based layout in XML code. When end user click first button its showing some options. For example when user click one question its show the the answer in expandable ways. Already i have one article about Android RecyclerView check this to learn more about android recyclerview concept.

dependencies

Add the project dependency for execute the expandable app. Before start this, you have to manually add the library in build.gradle file.

dependencies {
    compile 'com.github.ayalma:ExpandableRecyclerView:0.4.0'
}

Android Expandable RecyclerView

To order the data in our application like add the list of data stored and accessed in local file. When we call the data its automatically showing from using the help of java. Open your default code of MainActivity.java file and add the following below code,

public class PurchaseItemRecyclerViewAdapter extends ExpandableRecyclerView.Adapter
{


    @Override
    public int getGroupItemCount() {
        return 10;
    }

    @Override
    public int getChildItemCount(int i) {
        return 3;
    }

    @Override
    public String getGroupItem(int i) {
        return "group :" + i;
    }

    @Override
    public String getChildItem(int group, int child) {
        return "group: " + group +"item: "+ child;
    }

    @Override
    protected ExpandableRecyclerView.SimpleGroupViewHolder onCreateGroupViewHolder(ViewGroup parent)
    {
        return new ExpandableRecyclerView.SimpleGroupViewHolder(parent.getContext());
    }

    @Override
    protected ChildViewHolder onCreateChildViewHolder(ViewGroup parent, int viewType)
    {
        View rootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.purchase_list_content,parent,false);
        return new ChildViewHolder(rootView);
    }

    @Override
    public void onBindGroupViewHolder(ExpandableRecyclerView.SimpleGroupViewHolder holder, int group) {
        super.onBindGroupViewHolder(holder, group);
        holder.setText(getGroupItem(group));

    }

    @Override
    public void onBindChildViewHolder(ChildViewHolder holder, int group, int position)
    {
        super.onBindChildViewHolder(holder, group, position);
        holder.name.setText(getChildItem(group,position));
    }

    @Override
    public int getChildItemViewType(int i, int i1) {
        return 1;
    }

    public class ChildViewHolder extends RecyclerView.ViewHolder
    {
        private TextView name;
        public ChildViewHolder(View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.item_name);
        }
    }
}

After implementing the java files we need to design the user interfaces. Just drag and drop the property or you can manually implement the design through the below code. Open the XML file, then copy the below code and paste from your XML file.

Android Expandable RecyclerView Source Code

Here get the full source code of this project. When you working on this project, face any error just comment below i will try to solve your queries as soon as possible. I share the github code, click below button and get from github. The project source code credits from ayalma.

Leave a Reply