Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Leanback VerticalGridFragment remove top spacing

I am using VerticalGridFragment to display items in a grid-like layout I don't need to show search or title and I want the rows to start from top of the screen without any margins. Any help?example in the image below

like image 403
nadine87 Avatar asked Aug 18 '16 09:08

nadine87


Video Answer


3 Answers

You need create new class with name CustomVerticalGridPresenter and put followig code in it.

public class CustomVerticalGridPresenter extends VerticalGridPresenter {

    VerticalGridView gridView;

    CustomVerticalGridPresenter(int zoom, boolean val){
        super(zoom, val);
    }

    @Override
    protected void initializeGridViewHolder(ViewHolder vh) {
        super.initializeGridViewHolder(vh);
        gridView = vh.getGridView();
        int top = 20;//this is the new value for top padding
        int bottom = gridView.getPaddingBottom();
        int right = gridView.getPaddingRight();
        int left = gridView.getPaddingLeft();
        gridView.setPadding(left,top,right,bottom);
    }
}

Then in verticalgridfragment class use

CustomVerticalGridPresenter videoGridPresenter = new 
CustomVerticalGridPresenter(ZOOM_FACTOR, false);

instead of

VerticalGridPresentervideoGridPresenter = new VerticalGridPresenter(ZOOM_FACTOR, false);
like image 58
mehmoodnisar125 Avatar answered Oct 18 '22 04:10

mehmoodnisar125


I found a way to do it by overriding the VerticalGridPresenter of the VerticalGridFragment then getting the VerticalGridView, and setting top padding to a smaller value. In the CustomVerticalGridPresenter class (that extends VerticalGridPresenter), override this method:

@Override
protected void initializeGridViewHolder(ViewHolder vh) {
    super.initializeGridViewHolder(vh);
    gridView = vh.getGridView();
    int top= 20;//this is the new value for top padding
    int bottom = gridView.getPaddingBottom();
    int right = gridView.getPaddingRight();
    int left = gridView.getPaddingLeft();
    gridView.setPadding(left,top,right,bottom);
}

Then in the VerticalGridFragment, assign the new CustomVerticalGridPresenter as following:

 CustomVerticalGridPresenter gridPresenter = new CustomVerticalGridPresenter();
    gridPresenter.setNumberOfColumns(NUM_COLUMNS);
    setGridPresenter(gridPresenter);
like image 43
nadine87 Avatar answered Oct 18 '22 04:10

nadine87


Another way over this problem is to set the windowAlignment attributes of the VerticalGridView. Most notably ...

verticalGridView.windowAlignment = BaseGridView.WINDOW_ALIGN_LOW_EDGE //this will remove the top margin
verticalGridView.windowAlignmentOffset = 0
verticalGridView.windowAlignmentOffsetPercent = 25f //find correct values for your case
like image 3
Mapar Avatar answered Oct 18 '22 03:10

Mapar