我有一个活动,其中包含RecyclerView和透明状态栏(包含wifi信号的那个等等)和软键栏。当然,RecyclerView中的项目数量是未定义的,我需要第一个具有标准上边距加上状态栏高度,最后一个标准下边距加上软键条的高度。这是必要的,因为否则当滚动分别位于顶部或底部时,我会得到条形部分覆盖的第一个和最后一个项目。
这是一个示例代码,说明我如何以编程方式处理边距:
DisplayMetrics metrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
topSpace = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
bottomSpace = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
standardSpace = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
topSpace.setMargins( (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, metrics),
(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8 + 24, metrics),
(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, metrics),
(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, metrics));
bottomSpace.setMargins( (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, metrics),
(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, metrics),
(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, metrics),
(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8 + 56, metrics) +
getSoftkeysHeight(activity));
standardSpace.setMargins((int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, metrics),
(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, metrics),
(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, metrics),
(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, metrics));
我试图使用those answers,但他们似乎没有帮助我,因为屏幕的可绘制表面确实是整个屏幕...
答案 0 :(得分:1)
您可以使用RecyclerView.ItemDecoration
修改特定项目的边距。
创建一个扩展RecyclerView.ItemDecoration
的类:
public static class SimpleItemDecorator extends RecyclerView.ItemDecoration {
private int regularMargin;
private int lastPosition;
private int statusBarHeight;
private int navigationBarHeight;
public SimpleItemDecorator(Activity activity, int recyclerViewItemsCount) {
// get the regular margin
regularMargin = activity.getResources().getDimensionPixelSize(R.dimen.regularMargin);
// determine the last position
lastPosition = recyclerViewItemsCount - 1;
// get the height of the status bar
final Rect rectangle = new Rect();
final Window window = activity.getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
statusBarHeight = rectangle.top;
// get the height of the navigation bar
final int redId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
navigationBarHeight = (redId > 0) ? resources.getDimensionPixelSize(resId) : 0;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
// modify the margins
final int position = parent.getChildAdapterPosition(view);
if (position == 0) {
outRect.top = statusBarHeight + regularMargin;
} else if (position == lastPosition) {
outRect.bottom = navigationBar + regularMargin;
}
}
}
将创建的ItemDecorator
添加到RecyclerView:
final int recyclerViewItemsCount = 6;
final SimpleItemDecorator itemDecorator = new SimpleItemDecorator(this, recyclerViewItemsCount);
recyclerView.addItemDecorator(itemDecorator);