我正在关注this教程,并尝试为每个用户而不是相册创建个人资料页面。但是,我使用片段而不是活动,并意识到我的代码将与教程不同。
我已经使用Android开发人员文档和我自己的知识来修复从活动转移到片段的大多数错误,但我似乎无法找到解决方案,任何帮助都表示赞赏。
我有以下代码可以正常工作:
recyclerView = (RecyclerView) getActivity().findViewById(R.id.recycler_view);
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 2);
recyclerView.setLayoutManager(mLayoutManager);
但是当我尝试添加到回收站视图时,我收到一个错误:
recyclerView.addItemDecoration(new GridSpacingItemDecoration(2, dpToPx(10), true));
无法解析符号GridSpacingItemDecoration
无法解析方法dpToPx(int)
这是按要求提供的课程
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
}
}
}
}
答案 0 :(得分:0)
将此方法设为私有