Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VerticalGridView scrolls horizontally

I finally had success adding a VerticalGridView from android support library to my project. Now the problem is the grid scrolls both horizontally and vertically!

Here is my source :

<android.support.v17.leanback.widget.VerticalGridView
            android:id="@+id/my_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical" />

and java code:

mRecyclerView = (VerticalGridView) rootView.findViewById(R.id.my_recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setNumColumns(4);
mRecyclerView.setVerticalMargin(24);
like image 471
AVEbrahimi Avatar asked Jan 19 '26 14:01

AVEbrahimi


2 Answers

I have found a fix this. You just have to set android.support.v7.widget.GridLayoutManager as the layout manager for the VerticalGridView, as in the following (notice that the number of columns is passed in the constructor):

mRecyclerView = (VerticalGridView) rootView.findViewById(R.id.my_recycler_view);
mRecyclerView.setLayoutManager(new GridLayoutManager(context, 4));
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setVerticalMargin(24);
like image 97
cybersam Avatar answered Jan 22 '26 04:01

cybersam


I had the same issue when using a fragment, which extends VerticalGridSupportFragment. The solution is it use a custom VerticalGridPresenter, and in this apply a custom GridLayoutManager.

In your fragment:

VerticalGridPresenter gridPresenter = new CustomVerticalGridPresenter(FocusHighlight.ZOOM_FACTOR_SMALL, false);

CustomVerticalGridPresenter class:

public class CustomVerticalGridPresenter extends VerticalGridPresenter {

    public CustomVerticalGridPresenter(int focusZoomFactor, boolean useFocusDimmer) {
        super(focusZoomFactor, useFocusDimmer);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected ViewHolder createGridViewHolder(ViewGroup parent) {
        ViewHolder vh = super.createGridViewHolder(parent);
        VerticalGridView gridView = vh.getGridView();
        // Apply our custom LayoutManager that disables horizontal scrolling
        NoHorizontalScrollGridLayoutManager layoutManager = new NoHorizontalScrollGridLayoutManager(
                parent.getContext(), getNumberOfColumns());
        gridView.setLayoutManager(layoutManager);
        return vh;
    }
}

NoHorizontalScrollGridLayoutManager class:

public class NoHorizontalScrollGridLayoutManager extends GridLayoutManager {

    public NoHorizontalScrollGridLayoutManager(Context context, int spanCount) {
        super(context, spanCount);
    }

    @Override
    public boolean canScrollHorizontally() {
        // Return false to prevent horizontal scrolling
        return false;
    }

    @Override
    public boolean canScrollVertically() {
        // Allow vertical scrolling
        return true;
    }
}
like image 22
DerTeufel Avatar answered Jan 22 '26 02:01

DerTeufel