这是之前在ListView类中使用divider和dividerHeight参数实现的一个例子:
<ListView
android:id="@+id/activity_home_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@android:color/transparent"
android:dividerHeight="8dp"/>
然而,在RecyclerView类中我没有看到这样的可能性。
<android.support.v7.widget.RecyclerView
android:id="@+id/activity_home_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"/>
在这种情况下,是否可以定义边距和/或直接添加自定义分隔符视图到列表项的布局中,或者是否有更好的方法来实现我的目标?
使用RecyclerView itemdecoration -一个基本的分隔符样本在Kotlin android
一个完整的示例,包括一个构建器,添加边缘或使用资源颜色的可能性
class SeparatorDecoration constructor(ctx: Context, @ColorRes dividerColor: Int, heightDp: Float) :
RecyclerView.ItemDecoration() {
private val mPaints = Paint()
init {
mPaints.color = dividerColor
val thickness = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
heightDp,
ctx.resources.displayMetrics
)
mPaints.strokeWidth = thickness
}
override fun getItemOffsets(
outRect: Rect,
view: View,
parent: RecyclerView,
state: RecyclerView.State
) {
val params = view.layoutParams as RecyclerView.LayoutParams
// and add a separator to any view but the last one
val p = params.viewAdapterPosition
// and add a separator to any view but the last one
if (p < state.itemCount) outRect[0, 0, 0] =
mPaints.strokeWidth.toInt() // left, top, right, bottom
else outRect.setEmpty() // 0, 0, 0, 0
}
override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
// we set the stroke width before, so as to correctly draw the line we have to offset by width / 2
val offset = (mPaints.strokeWidth / 2).roundToInt()
// this will iterate over every visible view
for (i in 0 until parent.childCount) {
// get the view
val view: View = parent.getChildAt(i)
val params = view.layoutParams as RecyclerView.LayoutParams
// get the position
val position = params.viewAdapterPosition
// and finally draw the separator
if (position < state.itemCount) {
c.drawLine(
view.left.toFloat(),
(view.bottom + offset).toFloat(),
view.right.toFloat(), (view.bottom + offset).toFloat(), mPaints
)
}
}
}
}
设置很简单。只需将您的装饰与其余的初始设置一起添加到您的recyclerView。
val decoration = SeparatorDecoration(context, R.color.primaryColor, 1.5f)
recyclerview.addItemDecoration(decoration)
public class CommonItemSpaceDecoration extends RecyclerView.ItemDecoration {
private int mSpace = 0;
private boolean mVerticalOrientation = true;
public CommonItemSpaceDecoration(int space) {
this.mSpace = space;
}
public CommonItemSpaceDecoration(int space, boolean verticalOrientation) {
this.mSpace = space;
this.mVerticalOrientation = verticalOrientation;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
outRect.top = SizeUtils.dp2px(view.getContext(), mSpace);
if (mVerticalOrientation) {
if (parent.getChildAdapterPosition(view) == 0) {
outRect.set(0, SizeUtils.dp2px(view.getContext(), mSpace), 0, SizeUtils.dp2px(view.getContext(), mSpace));
} else {
outRect.set(0, 0, 0, SizeUtils.dp2px(view.getContext(), mSpace));
}
} else {
if (parent.getChildAdapterPosition(view) == 0) {
outRect.set(SizeUtils.dp2px(view.getContext(), mSpace), 0, 0, 0);
} else {
outRect.set(SizeUtils.dp2px(view.getContext(), mSpace), 0, SizeUtils.dp2px(view.getContext(), mSpace), 0);
}
}
}
}
这将增加每个项目的顶部和底部(或左右)的空间。然后你可以将它设置为你的recyclerView。
recyclerView.addItemDecoration(new CommonItemSpaceDecoration(16));
文件SizeUtils.java
public class SizeUtils {
public static int dp2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
}
RecyclerView和ListView有点不同。实际上,RecyclerView需要一个类似ListView的结构。例如,线性布局。LinearLayout有用于划分每个元素的参数。
在下面的代码中,我有一个RecyclerView由一个线性布局中的CardView对象组成,其中有一个“填充”,将在项目之间放置一些空间。把空间缩小,就能得到一条线。
下面是recyclerview_layout.xml文件中的Recycler视图:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".ToDoList">
<!-- A RecyclerView with some commonly used attributes -->
<android.support.v7.widget.RecyclerView
android:id="@+id/todo_recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
这里是每个项目看起来像(它显示为分割由于android:填充在线性布局包围一切)在另一个文件:cards_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
**android:padding="@dimen/activity_vertical_margin"**>
<!-- A CardView that contains a TextView -->
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="100dp"
android:elevation="30dp"
card_view:cardElevation="3dp">
<TextView
android:id="@+id/info_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</android.support.v7.widget.CardView>
</LinearLayout>
这很简单,你不需要这么复杂的代码:
DividerItemDecoration divider = new DividerItemDecoration(
mRVMovieReview.getContext(), DividerItemDecoration.VERTICAL
);
divider.setDrawable(
ContextCompat.getDrawable(getBaseContext(), R.drawable.line_divider)
);
mRVMovieReview.addItemDecoration(divider);
将它添加到你的drawable: line_divide .xml中
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size android:height="1dp" />
<solid android:color="@android:color/black" />
</shape>
我认为使用一个简单的分隔器会对你有帮助
为每个项目添加分隔符:
1. 将其添加到可绘制目录line_divider.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size
android:width="1dp"
android:height="1dp" />
<solid android:color="#999999" />
</shape>
2. 创建SimpleDividerItemDecoration类
我用这个例子来定义这个类:
https://gist.github.com/polbins/e37206fbc444207c0e92
package com.example.myapp;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import com.example.myapp.R;
public class SimpleDividerItemDecoration extends RecyclerView.ItemDecoration{
private Drawable mDivider;
public SimpleDividerItemDecoration(Resources resources) {
mDivider = resources.getDrawable(R.drawable.line_divider);
}
public void onDrawOver(Canvas c, 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 child = parent.getChildAt(i);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
int top = child.getBottom() + params.bottomMargin;
int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
}
3.在使用RecyclerView的活动或片段中,在onCreateView中添加以下内容:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
RecyclerView myRecyclerView = (RecyclerView) layout.findViewById(R.id.my_recycler_view);
myRecyclerView.addItemDecoration(new SimpleDividerItemDecoration(getResources()));
....
}
4. 添加项目之间的间距
你只需要添加padding属性到你的项目视图
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:padding="4dp"
>
..... item structure
</RelativeLayout>