1

我在 NestedScrollView 中尝试这个 ListView 并向其中添加 onScroll 侦听器,但是当我尝试获取 listView.getLastVisiblePosition() 时,它给了我列表的最大项目而不是最后一个可见的项目。如何获得与没有 NestedScrollView 的列表视图相同的结果?

我感谢各种帮助,谢谢

我的 onScrollListener 代码

listView.setOnScrollListener(new AbsListView.OnScrollListener() {
     @Override
     public void onScrollStateChanged(AbsListView absListView, int i) {}

     public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) 
     {
                Log.d("scroll", "scroll: " + listView.getLastVisiblePosition());
     }
});

我的 xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_merchant">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

            <ListView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/ListView">
            </ListView>

    </RelativeLayout>
</android.support.v4.widget.NestedScrollView>

这里的解决方案:

*至少现在适用于我的情况

**从使用 listview.setonscrolllistener 更改为 nested.setOnScrollChangeListener

nest.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
      @Override
      public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) 
            {
                //get current height of screen and total height
                Display display = getWindowManager().getDefaultDisplay();
                final int stageHeight = display.getHeight();
                int currentheight = stageHeight+scrollY;
                int maxheight = listView.getHeight();

                Log.d("myarc","aaa "+ currentheight +" bbb "+ maxheight);
                if(currentheight>=maxheight && (update))
                {
                    //turn off autoupdate
                    update=false;
                    Log.d("myarc","masuk bro ");
                    //add new item
                    RowItem item = new RowItem(R.drawable.wp, "GG", "GGWP");
                    rowItems.add(item);

                    //refresh listview
                    dataAdapter.notifyDataSetChanged();
                    setListViewHeightBasedOnItems(listView);
                }
            }
        });
4

0 回答 0