Search code examples
androidandroid-recyclerviewexpandablerecyclerview

Filling the Child items when expanding the parent of recycler view


In my project I have to use the Expandable feature in listview or a recyclerview. I selected the recyclerview as It is better in performance.

But I have to make the Expandable version So I got very nice library over here and it really works good. But then I have a problem and that is as under

  • I want to fill the Child views info from web service which should be called when user will click on the parent item. I have no Idea How it could be done using the same library
  • I want to make the never ending RecyclerView so its mean I have to use some thing like pull to refresh type of thing but that will happen when user will arrive to the end of RecyclerView. Any idea how I can I implement these two functionalities using the same library ?

Please help me and show me the right way of doing this . At least for the first confusion . Thanks in advance

Update 1:

The Source Code I am sharing below is the code from the demo of this library In this code snippt you can see he is setting child list (Item List)while creating the object of the ParentList (group list). Where as I want to fill child list after fetching from the webservice

public class Recipe implements ParentListItem {

private String mName;
private List<Ingredient> mIngredients;

public Recipe(String name, List<Ingredient> ingredients) {
    mName = name;
    mIngredients = ingredients;
}

public String getName() {
    return mName;
}

@Override
public List<?> getChildItemList() {
    return mIngredients; // How can I return list after getting data from the web service 
}

@Override
public boolean isInitiallyExpanded() {
    return false;
}

}

and he set it as

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recycler_view_sample);

    Ingredient beef = new Ingredient("beef");
    Ingredient cheese = new Ingredient("cheese");
    Ingredient salsa = new Ingredient("salsa");
    Ingredient tortilla = new Ingredient("tortilla");
    Ingredient ketchup = new Ingredient("ketchup");
    Ingredient bun = new Ingredient("bun");

    Recipe taco = new Recipe("taco", Arrays.asList(beef, cheese, salsa, tortilla));
    Recipe quesadilla = new Recipe("quesadilla", Arrays.asList(cheese, tortilla));
    Recipe burger = new Recipe("burger", Arrays.asList(beef, cheese, ketchup, bun));
    final List<Recipe> recipes = Arrays.asList(taco, quesadilla, burger);

    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
    mAdapter = new RecipeAdapter(this, recipes);
    mAdapter.setExpandCollapseListener(new ExpandableRecyclerAdapter.ExpandCollapseListener() {
        @Override
        public void onListItemExpanded(int position) {
            Recipe expandedRecipe = recipes.get(position);

            String toastMsg = getResources().getString(R.string.expanded, expandedRecipe.getName());
            Toast.makeText(VerticalLinearRecyclerViewSampleActivity.this,
                    toastMsg,
                    Toast.LENGTH_SHORT)
                    .show();
        }

        @Override
        public void onListItemCollapsed(int position) {
            Recipe collapsedRecipe = recipes.get(position);

            String toastMsg = getResources().getString(R.string.collapsed, collapsedRecipe.getName());
            Toast.makeText(VerticalLinearRecyclerViewSampleActivity.this,
                    toastMsg,
                    Toast.LENGTH_SHORT)
                    .show();
        }
    });

    recyclerView.setAdapter(mAdapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

error Log This error occur when i try to populate ingredient list from webservice. What I did I made the ingredient list static and from webservice I set it statically and retrieve it from the override function

Process: com.naziraschool.nazraschoolsystem, PID: 484 java.lang.IndexOutOfBoundsException: Invalid index 4, size is 4 at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) at java.util.ArrayList.remove(ArrayList.java:403) at com.bignerdranch.expandablerecyclerview.Adapter.ExpandableRecyclerAdapter.collapseParentListItem(ExpandableRecyclerAdapter.java:604) at com.bignerdranch.expandablerecyclerview.Adapter.ExpandableRecyclerAdapter.onParentListItemCollapsed(ExpandableRecyclerAdapter.java:274) at com.bignerdranch.expandablerecyclerview.ViewHolder.ParentViewHolder.collapseView(ParentViewHolder.java:164) at com.bignerdranch.expandablerecyclerview.ViewHolder.ParentViewHolder.onClick(ParentViewHolder.java:124)


Solution

  • Reading the docs for this library, i see the following snippet that could allow you to listen for the parent expansion

    MyAdapter adapter = new MyAdapter(this, recipes);
    
    adapter.setExpandCollapseListener(new ExpandableRecyclerAdapter.ExpandCollapseListener() {
        @Override
        public void onListItemExpanded(int position) {
            Recipe expandedRecipe = recipes.get(position);
            // ...
        }
    
        @Override
        public void onListItemCollapsed(int position) {
            Recipe collapsedRecipe = recipes.get(position);
            // ...
        }
    });
    
    mRecyclerView.setAdapter(adapter);
    

    Inside the onListItemExpanded method you should do a network call and load data in your adapter and then call notifyDataSetChanged() to refresh the data.