Android dynamic loading list view onScrollListener issues

两盒软妹~` 提交于 2019-12-29 08:51:36

问题


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?


回答1:


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;



回答2:


The above implementaion may cause errors at edge conditions. The sum of first visible item & items in page may be 1 count more or less than expected. I was using the same thing untill i switched to a new method. What i did was implemented a listener to the Adapter of listview and in the getview check if(position== totalItemCount of items in array to be displayed), if yes i call up my listener from adapter. Quite simple




回答3:


    lv_best.setOnScrollListener(new OnScrollListener() {
                @Override
                public void onScrollStateChanged(AbsListView view, int scrollState) {
                    switch (scrollState) {
                    case OnScrollListener.SCROLL_STATE_IDLE:
                                         // when list scrolling stops

                                        manipulateWithVisibleViews(view);

                        break;
                    case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
                        break;
                    case OnScrollListener.SCROLL_STATE_FLING:
                        break;
                    }
                }

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

private void manipulateWithVisibleViews(AbsListView view){
        int count = view.getChildCount(); // visible views count
        int lastVisibleItemPosition = view.getLastVisiblePosition();

        for (int i = 0; i < count; i++) {
            View convertView = view.getChildAt(i);
            // update views
            }
        }

}


来源:https://stackoverflow.com/questions/13761639/android-dynamic-loading-list-view-onscrolllistener-issues

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!