我正在尝试创建自己的CustomDivider类,该类扩展为RecyclerView
。 ItemDecoration基于来自Google的具有分隔符的样本。幸运的是,我尝试过的代码有效,但我只是在Lollipop
中运行的模拟器中尝试过,我发现它在我的Kitkat
设备中无效。
以下是模拟器和结果的屏幕截图。
这是我运行Kitkat的Asus Zenfone 4的截图。
这是我的代码:
public class CustomDivider extends RecyclerView.ItemDecoration {
public static final int LINEAR_HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
public static final int LINEAR_VERTICAL_LIST = LinearLayoutManager.VERTICAL;
public static final int LINE_FLAT = 0;
private Paint paint = new Paint();
private int color;
private int orientation;
private int top, bottom, left, right;
private int lineType; //TODO LineTypes
private View child;
public CustomDivider(Context context, int orientation, int resourceColor) {
color = context.getResources().getColor(resourceColor);
paint.setColor(color);
paint.setStrokeWidth(3);
checkOrientation(orientation);
lineType = LINE_FLAT;
}
private void checkOrientation(int orientation) {
if (orientation != LINEAR_HORIZONTAL_LIST && orientation != LINEAR_VERTICAL_LIST) {
throw new IllegalArgumentException("Invalid Orientation");
} else
this.orientation = orientation;
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDraw(c, parent, state);
if (orientation == LINEAR_HORIZONTAL_LIST) {
//drawLinearHorizontalDivider(c, parent);
} else if (orientation == LINEAR_VERTICAL_LIST) {
drawLinearVerticalDivider(c, parent);
}
}
private void drawLinearVerticalDivider(Canvas c, RecyclerView parent) {
left = parent.getLeft();
right = parent.getWidth();
for (int i = 0; i < parent.getChildCount(); i++) {
child = parent.getChildAt(i);
top = child.getTop();
bottom = child.getBottom();
Log.d("Dimensions: ", "Top is: " + top + ", Bottom is " + bottom);
if (i != parent.getChildCount() - 1) {
c.drawLine(left, bottom, right, bottom, paint);
}
}
}
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDrawOver(c, parent, state);
}
}
任何人都可以提供有关我是否错过了某些内容的更多信息吗?
答案 0 :(得分:2)
以下是一个很好的例子,请看一下:
public class DividerItemDecoration extends RecyclerView.ItemDecoration {
private static final int[] ATTRS = new int[]{
android.R.attr.listDivider
};
public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
private Drawable mDivider;
private int mOrientation;
public DividerItemDecoration(Context context, int orientation) {
final TypedArray a = context.obtainStyledAttributes(ATTRS);
mDivider = a.getDrawable(0);
a.recycle();
setOrientation(orientation);
}
public void setOrientation(int orientation) {
if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
throw new IllegalArgumentException("invalid orientation");
}
mOrientation = orientation;
}
@Override
public void onDraw(Canvas c, RecyclerView parent) {
if (mOrientation == VERTICAL_LIST) {
drawVertical(c, parent);
} else {
drawHorizontal(c, parent);
}
}
public void drawVertical(Canvas c, RecyclerView parent) {
final int left = parent.getPaddingLeft();
final int right = parent.getWidth() - parent.getPaddingRight();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int top = child.getBottom() + params.bottomMargin;
final int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
public void drawHorizontal(Canvas c, RecyclerView parent) {
final int top = parent.getPaddingTop();
final int bottom = parent.getHeight() - parent.getPaddingBottom();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int left = child.getRight() + params.rightMargin;
final int right = left + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
@Override
public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
if (mOrientation == VERTICAL_LIST) {
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
} else {
outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
}
}
}
这是我的片段,它运作良好,分隔线清晰而薄:
public static class RecyclerViewFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_recyclerview, container, false);
RecyclerView recyclerView = (RecyclerView) root.findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
RecyclerViewAdapter adapter = new RecyclerViewAdapter(getActivity(), getResources()
.getStringArray(R.array.countries));
recyclerView.setAdapter(adapter);
return root;
}
}
我希望你会受到启发。
答案 1 :(得分:1)
阅读此答案后:https://stackoverflow.com/a/27608549/2598247
还表示要覆盖onDrawOver我编辑了我的代码,并将我在onDraw中的代码放入onDrawOver,并且碰巧找到了现在在Pre-Lollipop设备中显示的分隔符。
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDrawOver(c, parent, state);
if (orientation == LINEAR_HORIZONTAL_LIST) {
//drawLinearHorizontalDivider(c, parent);
} else if (orientation == LINEAR_VERTICAL_LIST) {
drawLinearVerticalDivider(c, parent);
}
}