I am following this tutorial, and and trying to create a profile page for each individual user instead of albums. However, I am using fragments instead of activities and realise my code will be abit different to the tutorial.
I have used the Android developer documentation and my own knowledge to fix the majority of bugs transferring from an activity to a fragment, however I can not seem to find the solution to this one, any help is appreciated.
I have the below code that works fine:
recyclerView = (RecyclerView) getActivity().findViewById(R.id.recycler_view);
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 2);
recyclerView.setLayoutManager(mLayoutManager);
But when I try to add to the recycler view I get an error:
recyclerView.addItemDecoration(new GridSpacingItemDecoration(2, dpToPx(10), true));
Cant resolve symbol GridSpacingItemDecoration
Cant resolve method dpToPx(int)
Here is its class as requested
public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {
private int spanCount;
private int spacing;
private boolean includeEdge;
public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) {
this.spanCount = spanCount;
this.spacing = spacing;
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
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
}
}
}
}