1

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)

A.s.ALI
  • 1,992
  • 3
  • 22
  • 54

1 Answers1

0

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.

cafebabe1991
  • 4,928
  • 2
  • 34
  • 42
  • I can load it from here I know but How can I set the data to child as the child items list is being sent from the ParentModel classs which implements the ParentListItem and two overrided functions . One of them is public List> getChildItemList() ,, which returns the list , – A.s.ALI Aug 17 '16 at 04:22
  • so that method is being called when you click on the any parent and it gets expended by taking the list of childs from the method i mentioned above – A.s.ALI Aug 17 '16 at 04:24
  • I am taking your ans as a hint , so what I have decided is to detect the onListItemExpended and setting the child list in the parent model – A.s.ALI Aug 17 '16 at 04:29
  • Yes correct. Make sure you update the adapter(notifyDataSetChanged) after the items are added into it. – cafebabe1991 Aug 17 '16 at 04:30
  • there is no code to share , as the demo code you can see. what I am doing is trying to get the ingredient from the webservice. Just look at the demo please – A.s.ALI Aug 17 '16 at 04:34
  • let me try it and will select your answer as a accepted answer as it gave me hint but can you do a little more favor – A.s.ALI Aug 17 '16 at 04:35
  • there is a problem – A.s.ALI Aug 17 '16 at 05:27
  • What is the problem? – cafebabe1991 Aug 17 '16 at 05:44
  • it looks like it is not being called adapter.setExpandCollapseListener(n – A.s.ALI Aug 17 '16 at 05:51
  • this method is not being called – A.s.ALI Aug 17 '16 at 06:17
  • I Have debugged every thing and the child views gets set but still do not show on expanding the parent view – A.s.ALI Aug 17 '16 at 08:01
  • Are you calling notifyDataSetChanged() on the adapter. – cafebabe1991 Aug 17 '16 at 08:02
  • yeah it is giving me null pointer exception , can you please show me how to set child view at run time ? – A.s.ALI Aug 17 '16 at 08:51
  • can you create a short snippet of what you have done else it will be tough. Anyways if its a null pointer exception, try to see that the object on which you are calling a function is not null. For example Object a = null; a.someMethod() -> This gives me a nullpointer. So see in your case are you invoking a method on an object which may be null. – cafebabe1991 Aug 17 '16 at 09:14
  • please check the updated answer and tell me if yew get any thing – A.s.ALI Aug 17 '16 at 10:10
  • Are you able to set the json response from your webservice into some data objects ? Only then can you set that response inside your Recipe constructor and only then it will get the data. – cafebabe1991 Aug 17 '16 at 10:17
  • yeah I am getting the data and putting that in the list , now I only want to set it , how can I do that – A.s.ALI Aug 17 '16 at 10:25
  • my case is that I have to set the childs in parent when the click occur before it I do not want to set the childs – A.s.ALI Aug 17 '16 at 10:27
  • i need to do it in the Onexpand listnere ??? and then after it what should I do ? – A.s.ALI Aug 17 '16 at 10:33
  • Which is the line that you get a nullpointer exception ? Can you share the logs that you get or just the line ? – cafebabe1991 Aug 17 '16 at 10:34
  • right now I am doing something , can yew please think of the possible solution and share it , just an raw idea – A.s.ALI Aug 17 '16 at 10:40
  • Are you removing an item from the list somewhere in your adapter code ? Because that is where the error is – cafebabe1991 Aug 17 '16 at 10:47
  • not removing just adding the child when the parent is clicked – A.s.ALI Aug 17 '16 at 11:23
  • The library is removing items internally and then it throws this indexoutofbounds exception. Show your adapter code for me look further – cafebabe1991 Aug 17 '16 at 11:41
  • I have done it thanks for your time I have implemented the method on the server side to get the data in the form of parent and child. – A.s.ALI Aug 17 '16 at 11:44
  • @cafebabe1991 i have to take input from user ... in expandablerecyclerview child item lets say parent Id : 0 and want to take input in parent 0 child 1 so it should enter values only in parent 0 child 1 but right now its entering values in all parent child 1 how to resolve? – Erum Jun 07 '18 at 05:25