Loading only 10 items in listview

2019-04-17 06:10发布

The listview is loaded 100 pictures from the internet using a thread pool. How load only elements 10 to the extent necessary? And when scrolldown loading another 10 items? I'm using Lru cache for this

4条回答
甜甜的少女心
2楼-- · 2019-04-17 06:44
  1. Use an adapter
  2. Use LruCache for image storage

It's all there hope you can take it from there.

查看更多
小情绪 Triste *
3楼-- · 2019-04-17 07:02

no need to call all the data at once for your problem. you have to modify the api . make a webservice in such a way that only 10 data is shown at a time and this can be done by passing value from your app eg. during api calling pass 1 and server side should check if the value passed from android is 1 or 2 or... if the value is 1 (say) send 10 data using limit and if the value is 2 send another 10 data. and in android side during scrolling your list view call api passing value and append those fetched data from api to your local list and show those to the list view. It saves the time and userdata aswell

查看更多
你好瞎i
4楼-- · 2019-04-17 07:03

Its just matter of changing the data source

when the first time list view is loaded

Fetch some 10 images and add that in data source for list view adapter

If scrolling is detected then fetch next 10 images and clear the listview adapete and add this new 10 again

or you can fetch all the image urls in one shot then according to the scrolling you can change the contents of data source of list view adapter

查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-04-17 07:06

You can use limit and offset for this:

Example:

suppose i want to fetch 10 records and after end the scroll will fetch another 10 records:

Define the offset variable:

private static int offset = 0;

when you bind the adapter at that pass the offset variable:

Select * from table limit 10 offset 0

So this will fetch only 10 records from 0-9 after that write below code for listview:

    listview.setOnScrollListener(new OnScrollListener() {
        private int mLastFirstVisibleItem;

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

        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {

            mLastFirstVisibleItem = firstVisibleItem;

            final int lastItem = firstVisibleItem + visibleItemCount;
            if (lastItem == totalItemCount) {

                if (preLast != lastItem) { 
                    preLast = lastItem;

                    offset = offset + 10;

                    new fetchrecords().execute();

                } else {

                }
            }
        }

    });

so when go you to last position of list view item at that time it will call the method fetchrecords() and your offset value is 10.

So when this method calls at that time your offset value is 10.

so for next time your query is like:

select * from table limit 10 offset 10

So it will fetch other next 10 records.

Its done!!

查看更多
登录 后发表回答