Skip to content Skip to sidebar Skip to footer

Android Listview Lazy Loading

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 l

Solution 1:

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.

Solution 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)

Solution 3:

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 :

publicabstractclassLazyLoaderimplementsAbsListView.OnScrollListener {

    privatestaticfinalintDEFAULT_THRESHOLD=10 ;

    privatebooleanloading=true  ;
    privateintpreviousTotal=0 ;
    privateintthreshold= DEFAULT_THRESHOLD ;

    publicLazyLoader() {}

    publicLazyLoader(int threshold) {
        this.threshold = threshold;
    }

    @OverridepublicvoidonScrollStateChanged(AbsListView view, int scrollState) {
    }

    @OverridepublicvoidonScroll(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 dataif(!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.publicabstractvoidloadMore(AbsListView view, int firstVisibleItem,
                                  int visibleItemCount, int totalItemCount);
}

Activity :

publicclassMainActivityextendsAppCompatActivity {

        @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

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

            listView.setOnScrollListener(newLazyLoader() {
                @OverridepublicvoidloadMore(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                    // Fetch your data here !!!
                }
            });
        }
    }

You can find the complete implementation at this link

Solution 4:

//put in calling function :

@Overridepublic View onCreateView(android.view.LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
  ScrollListenerscrollListener=newScrollListener();
  listView.setOnScrollListener(scrollListener);
}

and inner class:

classScrollListenerimplementsOnScrollListener
{

    @OverridepublicvoidonScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
    {
        intsize= searchedPeople.size();
        if(isScrolled && totalItemCount != 0 && size < totalPeoples)
        {

            if(firstVisibleItem + visibleItemCount >= totalItemCount)
            {

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

                isScrolled = false;
            }
        }

    }

    @OverridepublicvoidonScrollStateChanged(AbsListView view, int scrollState)
    {

    }

}

Post a Comment for "Android Listview Lazy Loading"