Android的动态加载列表视图onScrollListener问题视图、加载、动态、问题

2023-09-05 02:19:24 作者:默

我实现了一个列表视图中,每个用户滚动到屏幕底部,它自动添加新的数据到列表视图 一旦滚动完成,onScroll()调用用于进入所述显示视图中,从开始到滚动的端每一个新的项目。

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();
     }
     }
    });

我运行模拟器,清单上面显示有5个项目 但我无法检测滚动ListView的结束,ChangeTab_best()被调用许多时间。当时我看到的logcat的Andr​​oid,所有变量firstVisibleItem,visibleItemCount,totalItemCount总是等于0

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?

推荐答案

要检测屏幕你有 firstVisibleItem 工作和 visibleItemCount 。我创造了一个 演示示例 ,在使用AsyncTask的相同即会加载项在背景和更新UI

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.

firstVisibleItem visibleItemCount 会给你加载的项数,然后您可以实现装载更多的物品的逻辑。

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;