所以我有一个包含不同类型行的ListView。我想为第一行禁用分隔符,并为第二行设置另一种分隔符样式。
最佳做法是什么?
我目前通过以下方式添加分隔符:
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),DividerItemDecoration.VERTICAL);
dividerItemDecoration.setDrawable(getDrawable(R.drawable.divider));
recyclerView.addItemDecoration(dividerItemDecoration);
答案 0 :(得分:2)
我是通过以下方式完成的。
注意:在我的解决方案中,分隔符是项目视图的一部分,您可以设置背景或绘制线条,或添加底部边距并让空间显示背景因此创建分隔符等我推荐查看ItemDecoration api。我只是向您展示如何在ItemDecoration中确定您想要做什么。
首先,在适配器中:
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
...
if (mData.get(postion) instanceof ObjectA)
holder.itemView.setTag(R.id.noSpaceAfter, Boolean.TRUE);
else
holder.itemView.setTag(R.id.noSpaceAfter, Boolean.FALSE);
...
}
然后在RecyclerView
中,声明ItemDecoration
mRecyclerView.addItemDecoration(new MyDividerItemDecoration());
最后,定义您的MyDividerItemDecorator
public class MyDividerItemDecoration extends RecyclerView.ItemDecoration {
public MyDividerItemDecoration () {
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
RecyclerView.State state) {
Boolean noSpace = (Boolean) view.getTag(R.id.noSpaceAfter);
View v = view.findViewById(R.id.view_sep);
if (v != null) {
if (noSpace == null || !noSpace) {
v.setVisibility(View.VISIBLE);
} else {
v.setVisibility(View.INVISIBLE);
}
}
}
}
考虑到这一点,您只需复制DividerItemDecoration的来源,然后在其drawVertical
方法中,当您在循环中获取视图时,请检查是否{{ 1}}是真或假,并相应地跳过绘图。
答案 1 :(得分:1)
尝试在布局底部添加行xml文件并定义所需的layout_height并设置layout_width =“match_parent”并隐藏您想要的特定索引的视图
例如 -
`<View
android:id="@+id/view_divider"
android:layout_width="match_parent"
android:layout_height="@dimen/one"
android:background="@color/grey_divider_color"/>`
答案 2 :(得分:1)
@Ali我喜欢你的解决方案。但是,您的代码需要布局中的分隔符。我通过隐藏常规分隔符来解决它。无论如何,谢谢你的提示。这是我的班级:
public class DividerItemDecoration extends RecyclerView.ItemDecoration {
private static final int[] ATTRS = new int[]{android.R.attr.listDivider};
private Drawable divider;
/**
* Default divider will be used
*/
public DividerItemDecoration(Context context) {
final TypedArray styledAttributes = context.obtainStyledAttributes(ATTRS);
divider = styledAttributes.getDrawable(0);
styledAttributes.recycle();
}
/**
* Custom divider will be used
*/
public DividerItemDecoration(Context context, int resId) {
divider = ContextCompat.getDrawable(context, resId);
}
@Override
public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View row = parent.getChildAt(i);
Boolean noDivider = (Boolean) row.getTag(R.id.noSpaceAfter);
if (noDivider == null || !noDivider) {
drawDivider(canvas, row, left, right);
}
}
}
private void drawDivider(Canvas canvas, View row, int left, int right) {
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) row.getLayoutParams();
int top = row.getBottom() + params.bottomMargin;
int bottom = top + divider.getIntrinsicHeight();
divider.setBounds(left, top, right, bottom);
divider.draw(canvas);
}
}