我在NestedScrollView中尝试这个ListView并将onScroll Listener添加到它但是当我尝试获取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);
}
}
});