Android中的GridView设置第一个可见项目完全可见 [英] Set first visible item in Android gridview completely seen
问题描述
我的GridView有两个柱
用户可以通过垂直滚动看到GridView的项目
问题是,当用户滚动完成
第一个可见行并没有完全看到。我想设置一个可见行的顶部的GridView的顶部,第一行是comletely可见
i have gridview with two column user can scroll vertically to see gridview items the problem is when user scrolling is finished the first visible row is not completely seen .i want to set top of first visible row to top of gridview so first row is comletely visible
谁能帮助我?
更新:
中的第一项是
第二项是
Update: the first item is and the second item is
但滚动后,我们看到
but after scroll we see
推荐答案
我用RecyclerView,我希望这是对你有好处!
I use RecyclerView, and i hope it's good for you!
mLayoutManager = new GridLayoutManager(getActivity(), 2);
mListRV= (RecyclerView).findViewById(R.id.list_rv);
mListRV.setLayoutManager(mLayoutManager);
mListRV.setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
View v = mLayoutManager.getChildAt(0);
int offsetTop = v.getTop();
int offsetBottom = v.getBottom();
int height = (offsetBottom - offsetTop);
if(offsetTop >= -height/2) {
mListRV.smoothScrollBy(0, offsetTop);
} else {
mListRV.smoothScrollBy(0, height + offsetTop);
}
}
}
});
有了这个code,当你完成滚动,如果第一个可见项目仅显示recyclerView的一部分。它会自动滚动显示完整的项目。您可以使用:
With this code, when you finish scroll, if the first visible item only show a part in recyclerView. It will auto scroll to show full item. You can use:
mLayoutManager.scrollToPositionWithOffset(position, offset);
但它是如此之快,用户可以混淆。而你将不再需要使用customLayoutManager编辑滚动的属性。
But it is so fast, the user can be confused. And you will not need use customLayoutManager to edit scroll's properties.
希望帮助你!
这篇关于Android中的GridView设置第一个可见项目完全可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!