Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
486 views
in Technique[技术] by (71.8m points)

listview - Android dynamic loading list view onScrollListener issues

I have implemented a list view, every user scroll to bottom screen, it auto add new data to list view Once the scroll has completed, onScroll() call for every new item that enters the displaying view, from the beginning to the end of the scrolling.

lv_best = (ListView) findViewById(R.id.lv_best);
bestadapter = new LazyAdapter(this, lst_tab_best);
lv_best.setAdapter(bestadapter);
lv_best.setOnScrollListener(new AbsListView.OnScrollListener() {

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

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

    Log.d("onScroll ", firstVisibleItem+"-"+visibleItemCount+"-"+totalItemCount);
    boolean loadMore = firstVisibleItem + visibleItemCount >= totalItemCount;
    if(loadMore){
            new ChangeTab_best().execute();
     }
     }
    });

I run in emulator, list above display with 5 items but I can't detect the end of scrolling in a ListView, ChangeTab_best() is called many time.When I see Logcat android , all variables firstVisibleItem,visibleItemCount,totalItemCount always equal 0

So what's happen here?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To detect the item at the bottom of the screen you have to work with firstVisibleItem and visibleItemCount. I had created a demo example for the same using AsyncTask that will loading items in background and update the UI.

Total of firstVisibleItem and visibleItemCount will give you the number of items that are loaded and then you can implement the logic of loading more items.

int loadedItems = firstVisibleItem + visibleItemCount;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...