我有两列的gridview。用户可以垂直滚动以查看gridview项目。
问题是当用户完成滚动时,第一行不完全可见。我想将第一个可见行的顶部设置为gridview的顶部,这样第一行就完全可见了。
任何人都可以帮助我吗?
答案 0 :(得分:0)
mGridView.setOnScrollListener(new OnScrollListener() {
private int currentFirstVisibleItem;
private int currentVisibleItemCount;
private int currentScrollState;
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
this.currentFirstVisibleItem = firstVisibleItem;
this.currentVisibleItemCount = visibleItemCount;
}
public void onScrollStateChanged(AbsListView view, int scrollState) {
this.currentScrollState = scrollState;
this.isScrollCompleted();
}
private void isScrollCompleted() {
if (mGridView.getLastVisiblePosition() == mGridView.getAdapter().getCount() - 1
&& mGridView.getChildAt(mGridView.getChildCount() - 1).getBottom() <= mGridView.getHeight()) {
if (this.currentVisibleItemCount > 0 && this.currentScrollState == SCROLL_STATE_IDLE) {
mGridView.smoothScrollToPosition(-1);
}
}
}
}
});