Skip to content Skip to sidebar Skip to footer

Set First Visible Item In Android Gridview Completely Seen

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

Solution 1:

you can try this

grid.setOnScrollListener(newOnScrollListener() {

            @OverridepublicvoidonScrollStateChanged(AbsListView arg0, int scrollState) {
                //this is the state when scroll finishif(scrollState==SCROLL_STATE_IDLE)
                    //the first visible item is the first (even partially) visbile item//setSelection will scroll the grid to the beginning of such item
                    grid.setSelection(firstVisibleItem);
            }

            @OverridepublicvoidonScroll(AbsListView arg0, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            }
        });

Solution 2:

I use RecyclerView, and i hope it's good for you!

mLayoutManager = newGridLayoutManager(getActivity(), 2);
mListRV= (RecyclerView).findViewById(R.id.list_rv);
mListRV.setLayoutManager(mLayoutManager);

mListRV.setOnScrollListener(newRecyclerView.OnScrollListener() {
                @OverridepublicvoidonScrollStateChanged(RecyclerView recyclerView, int newState) {
                    if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                        Viewv= mLayoutManager.getChildAt(0);
                        intoffsetTop= v.getTop();
                        intoffsetBottom= v.getBottom();
                        intheight= (offsetBottom - offsetTop);
                        if(offsetTop >= -height/2) {
                            mListRV.smoothScrollBy(0, offsetTop);
                        } else {
                            mListRV.smoothScrollBy(0, height + offsetTop);
                        }
                    } 
                }
            });

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

But it is so fast, the user can be confused. And you will not need use customLayoutManager to edit scroll's properties.

Hope that helps for you!

Post a Comment for "Set First Visible Item In Android Gridview Completely Seen"