Search code examples
androidandroid-recyclerviewadapter

Displaying recycler view items one by one instead of all after loading


I have a list with multiple objects and a custom adapter.

ExampleAdapter adapter = new ExampleAdapter((ArrayList<MatchItem>) outputList, GamesActivity.this);
recyclerView.setAdapter(adapter);

The problem is that the recycler view is displayed after all the views, for every item in the list are done, but I want to display every item as soon as it is loaded.

I am not sure if I have to create the adapter so it takes as parameter only one item at a time and update the data after every item added.

I am populating the list in an async task and set the adapter in the post execute method


Solution

  • Instead of passing list to the constructor of the adapter, try to add a method in the adapter that accepts one item at a time and then populates the list in the adapter:

    public class ExampleAdapter extends......{
    
    private List< MatchItem > list = new ArrayList<MatchItem>();
    
    public ExampleAdapter(GamesActivity.this){
    
    
    }
    
    //this method will accept items one by one
    public void addNewItem(MatchItem item){
    
    list.add(item);
    
    this.notifyDataSetChanged();
    
    }
    
    }
    

    And start passing items one by one when the item is ready

    //put these as a one time thing
    ExampleAdapter adapter = new ExampleAdapter(GamesActivity.this);
    recyclerView.setAdapter(adapter);
    
    //when you read items
    adapter.addNewItem(item);