Android listview lazy loading
Asked Answered
D

4

16

I want to do some stuff. I wanna do lazy loading in ListView. My ListView contain more than 10,000 data & only in TextView. so i can't load all that data in first time when i launch list activity. it's not efficient so i can load first 20 or 30 items in list. further the rows of the ListView are loaded when I scroll over them. so when i reach at last index of ListView on last index, i will put progressbar n it will note that the new data are loaded, so at that time new data will be load with last + 1 index. How can i do this?

Desolation answered 22/5, 2012 at 6:2 Comment(3)
could you show what you have tried?Pastose
ya i have added image here as example ..check this oneDesolation
How about picasso? Does this meet your need? square.github.io/picassoDeduction
D
17

You can achieve this by using endless Adapter implementation. This exactly does what you want. You can also restrict the number of rows to be refreshed per scroll. Here is a link to it.,

Android: Implementing progressbar and "loading..." for Endless List like Android Market

https://github.com/commonsguy/cwac-endless

To use it, you extend EndlessAdapter to provide details about how to handle the endlessness. Specifically, you need to be able to provide a row View, independent from any of the rows in your actual adapter, that will serve as a placeholder while you, in another method, load in the actual data to your main adapter. Then, with a little help from you, it seamlessly transitions in the new data.

Dipteral answered 22/5, 2012 at 6:12 Comment(4)
Thanks dude , let me try this one.Desolation
Link is broken now! :(Toddy
I'm gonna have to flag this as not an answer, because as of right now, it does not provide an answer to the question. Sorry about that!Rail
github.com/commonsguy/cwac-endless is not working 404Immix
H
2

Make your lists scroll faster:-

  1. Reduce the number of conditions used in the getView of your adapter.
  2. Reduce the number of garbage collection warnings that you get in the logs
  3. Add scroll function (restrict the number of rows to be refreshed per scroll)
Hyperion answered 12/7, 2015 at 6:3 Comment(0)
H
2

Add an onScrollListener to the ListView. When the user scrolls, check if the ListView is nearing its end. If yes, then fetch more data. As an example :

public abstract class LazyLoader implements AbsListView.OnScrollListener {

    private static final int DEFAULT_THRESHOLD = 10 ;

    private boolean loading = true  ;
    private int previousTotal = 0 ;
    private int threshold = DEFAULT_THRESHOLD ;

    public LazyLoader() {}

    public LazyLoader(int threshold) {
        this.threshold = threshold;
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
                         int visibleItemCount, int totalItemCount) {
        if(loading) {
            if(totalItemCount > previousTotal) {
                // the loading has finished
                loading = false ;
                previousTotal = totalItemCount ;
            }
        }

        // check if the List needs more data
        if(!loading && ((firstVisibleItem + visibleItemCount ) >= (totalItemCount - threshold))) {
            loading = true ;

            // List needs more data. Go fetch !!
            loadMore(view, firstVisibleItem,
                    visibleItemCount, totalItemCount);
        }
    }

    // Called when the user is nearing the end of the ListView
    // and the ListView is ready to add more items.
    public abstract void loadMore(AbsListView view, int firstVisibleItem,
                                  int visibleItemCount, int totalItemCount);
}

Activity :

public class MainActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.main_layout);
            ListView listView = (ListView) findViewById(R.id.listView);

            listView.setOnScrollListener(new LazyLoader() {
                @Override
                public void loadMore(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                    // Fetch your data here !!!
                }
            });
        }
    }

You can find the complete implementation at this link

Haematogenesis answered 26/5, 2016 at 19:11 Comment(0)
D
0

//put in calling function :

@Override
public View onCreateView(android.view.LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
  ScrollListener scrollListener = new ScrollListener();
  listView.setOnScrollListener(scrollListener);
}

and inner class:

class ScrollListener implements OnScrollListener
{

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
    {
        int size = searchedPeople.size();
        if(isScrolled && totalItemCount != 0 && size < totalPeoples)
        {

            if(firstVisibleItem + visibleItemCount >= totalItemCount)
            {

                    yourfunction(size, size + limit); // call service in your functioin

                isScrolled = false;
            }
        }

    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState)
    {

    }

}
Discreditable answered 7/10, 2014 at 10:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.