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);
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);
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With