grid layout manger fix
This commit is contained in:
@@ -85,7 +85,7 @@ public class CampaignProductListActivity extends BaseActivity {
|
||||
startActivityForResult(productPropertiesIntent, REQUEST_CODE_CAMPAIGN_PRODUCT_PROPERTIES);
|
||||
}
|
||||
});
|
||||
campaignProductRecyclerView.addItemDecoration(new GridSpacesItemDecoration(DisplayHelper.dpToPx(12)));
|
||||
campaignProductRecyclerView.addItemDecoration(new GridSpacesItemDecoration(2, 16, true));
|
||||
campaignProductRecyclerView.setAdapter(menuProductRecyclerAdapter);
|
||||
}
|
||||
|
||||
|
||||
@@ -113,7 +113,7 @@ public class MenuFragment extends BaseFragment {
|
||||
//showBottomsheetDialog(menuProductList.get(position));
|
||||
}
|
||||
});
|
||||
menuProductRecyclerView.addItemDecoration(new GridSpacesItemDecoration(DisplayHelper.dpToPx(12)));
|
||||
menuProductRecyclerView.addItemDecoration(new GridSpacesItemDecoration(2, 16, true));
|
||||
menuProductRecyclerView.setAdapter(menuProductRecyclerAdapter);
|
||||
}
|
||||
|
||||
|
||||
@@ -113,7 +113,7 @@ public class ProductFragment extends BaseFragment {
|
||||
//showBottomsheetDialog(productList.get(position));
|
||||
}
|
||||
});
|
||||
customProductRecyclerView.addItemDecoration(new GridSpacesItemDecoration(DisplayHelper.dpToPx(12)));
|
||||
customProductRecyclerView.addItemDecoration(new GridSpacesItemDecoration(2, 16, true));
|
||||
customProductRecyclerView.setAdapter(menuProductRecyclerAdapter);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,36 +4,43 @@ import android.graphics.Rect;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.View;
|
||||
|
||||
import ch.pizzamaxx.android.helper.DisplayHelper;
|
||||
|
||||
/**
|
||||
* Created by cimenmus on 20/09/2017.
|
||||
*/
|
||||
|
||||
// This item decoration is for two column grid layout manager
|
||||
public class GridSpacesItemDecoration extends RecyclerView.ItemDecoration {
|
||||
|
||||
private int space;
|
||||
private int spanCount;
|
||||
private int spacing;
|
||||
private boolean includeEdge;
|
||||
|
||||
public GridSpacesItemDecoration(int space) {
|
||||
this.space = space;
|
||||
public GridSpacesItemDecoration(int spanCount, int spacingInDp, boolean includeEdge) {
|
||||
this.spanCount = spanCount;
|
||||
this.spacing = DisplayHelper.dpToPx(spacingInDp);
|
||||
this.includeEdge = includeEdge;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
|
||||
int position = parent.getChildAdapterPosition(view); // item position
|
||||
int column = position % spanCount; // item column
|
||||
|
||||
outRect.left = space;
|
||||
outRect.bottom = space;
|
||||
|
||||
// Add top margin only for the first item to avoid double space between items
|
||||
if (parent.getChildLayoutPosition(view) == 0 || parent.getChildLayoutPosition(view) == 1)
|
||||
outRect.top = space;
|
||||
else
|
||||
outRect.top = 0;
|
||||
|
||||
// Add right margin only for the first item in the line to avoid double space between items
|
||||
if (parent.getChildLayoutPosition(view) % 2 == 1)
|
||||
outRect.right = space;
|
||||
else
|
||||
outRect.right = 0;
|
||||
if (includeEdge) {
|
||||
outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
|
||||
outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)
|
||||
|
||||
if (position < spanCount) { // top edge
|
||||
outRect.top = spacing;
|
||||
}
|
||||
outRect.bottom = spacing; // item bottom
|
||||
} else {
|
||||
outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing)
|
||||
outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f / spanCount) * spacing)
|
||||
if (position >= spanCount) {
|
||||
outRect.top = spacing; // item top
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user