简体   繁体   中英

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 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?

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.

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)

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

//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)
    {

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM