对于分割线来说RecyclerView是不同于ListView的,它没有类似ListView的driverheight的属性,需要手动实现。RecyclerView为我们提供了一个可扩展的ItemDecoration的类,用来绘制分割线以及其他方面的布局实现。
ItemDecoration有三个主要的方法我们需要去重写:
1、getItemOffsets(@NonNull Rect outRect, int itemPosition,
@NonNull RecyclerView parent):此方法的第一个参数outRect,用来给itemview的上下左右预留空间,例如outRect.set(0, 20, 0, 0); 就是在每一项的上方预留出来一个20px的空间高度。
2、onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull State state) 在getItemOffsets方法预留出一个位置后就可以在该方法中绘制任意布局,例如我们我们可以使用Canvas的drawRect方法绘制一个20px高度的分割线。
3、onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull State state) 此方法是最后执行的,是在item都绘制完成后,遮挡在上层的一个绘制方法,吸顶效果就可以借助此方法来实现。
实现一个简单的例子
public class MyDecoration extends RecyclerView.ItemDecoration {
private Context context;
private int dividerHeight = 10;//默认的分割线的高度
private int headerHeight;//分组的头部的高度
private Paint dividerPaint;//分割线的画笔
private Paint headerPaint;//每一组 的画笔
private Paint headerXdPaint;//吸顶的画笔
private Paint textPaint;//绘制文字的画笔
private Rect textRect;
public MyDecoration(Context context) {
this.context = context;
init();
}
private void init() {
headerHeight = dp2px(50);
dividerPaint = new Paint();
dividerPaint.setColor(Color.BLACK);
headerPaint = new Paint();
headerPaint.setColor(Color.RED);
headerXdPaint = new Paint();
headerXdPaint.setColor(Color.RED);
textPaint = new Paint();
textPaint.setTextSize(sp2px(16));
textPaint.setAntiAlias(true);
textPaint.setColor(Color.BLUE);
textRect = new Rect();
}
@Override
public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
super.onDraw(c, parent, state);
//在这里面绘制每一组的header布局 就是outRect.set(0, 10, 0, 0); 给我们预留的空间上绘制
if (parent.getAdapter() instanceof MyAdapter) {
MyAdapter myAdapter = (MyAdapter) parent.getAdapter();
//此count是当前屏幕中显示的所有的item个数,不是list集合中所有的个数
int childCount = parent.getChildCount();
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();
for (int i = 0; i < childCount; i++) {
View view = parent.getChildAt(i);
//此position是该view在所有list集合中的位置
int position = parent.getChildLayoutPosition(view);
int top = view.getTop();
boolean isGroupHeader = myAdapter.isGroupHeader(position);
if(view.getTop() - headerHeight < parent.getPaddingTop()){
continue;
}
if (isGroupHeader) {
//先绘制一个矩形框框,在绘制文字
c.drawRect(left, top - headerHeight, right, top, headerPaint);
//在矩形里面绘制文字
String groupName = myAdapter.getGroupName(position);
textPaint.getTextBounds(groupName, 0, groupName.length(), textRect);
c.drawText(groupName, 0, groupName.length(), left + 20, top - headerHeight / 2 + textRect.height() / 2, textPaint);
} else {
c.drawRect(left, top - dividerHeight, right, top, dividerPaint);
}
}
}
}
@Override
public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
super.onDrawOver(c, parent, state);
//此方法时在onDraw方法执行完毕后才执行,也就是说在onDrawOver方法中绘制的任何布局都会遮挡item项
//所以吸顶效果我们也可以绘制在顶部
if (parent.getAdapter() instanceof MyAdapter) {
MyAdapter myAdapter = (MyAdapter) parent.getAdapter();
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();
int top = parent.getPaddingTop();
//屏幕中显示的第一个item
int position = ((LinearLayoutManager) parent.getLayoutManager()).findFirstVisibleItemPosition();
//该position下对应的view
View view = parent.findViewHolderForAdapterPosition(position).itemView;
//下一个view是否是头部
boolean nextViewIsGroupHeader = myAdapter.isGroupHeader(position + 1);
String groupName = myAdapter.getGroupName(position);
textPaint.getTextBounds(groupName, 0, groupName.length(), textRect);
if (nextViewIsGroupHeader) {
int bottom = Math.min(headerHeight, view.getBottom() - top);
c.drawRect(left, top, right, top+bottom, headerXdPaint);
Log.e("onDrawOver---", view.getTop() + "&&&" + top);
if(top + bottom - headerHeight / 2 - textRect.height() / 2 < top){
return;
}
c.drawText(groupName, 0, groupName.length(), left + 20, top + bottom - headerHeight / 2 + textRect.height() / 2, textPaint);
} else {
c.drawRect(left, top, right, top + headerHeight, headerXdPaint);
c.drawText(groupName, 0, groupName.length(), left + 20, top + headerHeight / 2 + textRect.height() / 2, textPaint);
}
}
}
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
//此方法参数的作用是 将每一个item项的左上右下 预留一个空间出来
//outRect.set(0, 10, 0, 0); 就是在item项的顶部预留出来一个10px的位置,可以是分割线预留的高度,我们只需要在
//onDraw 里面绘制这个分割线就ok了 也可以是分组使用的groupheader使用配合onDrawOver就可以实现吸顶效果
if (parent.getAdapter() instanceof MyAdapter) {
MyAdapter myAdapter = (MyAdapter) parent.getAdapter();
//当前view在RecyclerView中的位置
int position = parent.getChildLayoutPosition(view);
boolean isGroupHeader = myAdapter.isGroupHeader(position);
if (isGroupHeader) {
outRect.set(0, headerHeight, 0, 0);
} else {
outRect.set(0, dividerHeight, 0, 0);
}
}
}
private int dp2px(int dp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, Resources.getSystem().getDisplayMetrics());
}
private float sp2px(int sp) {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, Resources.getSystem().getDisplayMetrics());
}
}
在Activity中使用:
public class RecyclerViewActivity extends AppCompatActivity {
RecyclerView recycler_view;
MyAdapter myAdapter;
List<TestInfo> testInfoList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recyclerview);
recycler_view = findViewById(R.id.recycler_view);
init();
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recycler_view.setLayoutManager(linearLayoutManager);
myAdapter = new MyAdapter(this, testInfoList);
recycler_view.addItemDecoration(new MyDecoration(this));
recycler_view.setAdapter(myAdapter);
}
void init() {
testInfoList = new ArrayList<>();
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 15; j++) {
testInfoList.add(new TestInfo("第" + i + "组测试数据" + j, i, "第" + i + "组"));
}
}
}
}
实体类:
public class TestInfo {
private String name;
private int groupId;
private String groupName;
public TestInfo(String name, int groupId, String groupName) {
this.name = name;
this.groupId = groupId;
this.groupName = groupName;
}
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getGroupId() {
return groupId;
}
public void setGroupId(int groupId) {
this.groupId = groupId;
}
}
Adapter的实现:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
Context context;
List<TestInfo> testInfoList;
public MyAdapter(Context context, List<TestInfo> testInfoList) {
this.context = context;
this.testInfoList = testInfoList;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View convertView = LayoutInflater.from(context).inflate(R.layout.rv_item_layout, parent, false);
return new MyViewHolder(convertView);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.tv_item.setText(testInfoList.get(position).getName());
}
@Override
public int getItemCount() {
return testInfoList == null ? 0 : testInfoList.size();
}
public boolean isGroupHeader(int position) {
if (position == 0) {
return true;
}
int currentGroupId = testInfoList.get(position).getGroupId();
int preGroupId = testInfoList.get(position - 1).getGroupId();
return currentGroupId != preGroupId;
}
public String getGroupName(int position) {
return testInfoList.get(position).getGroupName();
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView tv_item;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
tv_item = itemView.findViewById(R.id.tv_item);
}
}
}
布局文件就比较简单了,一个RecyclerView的布局和一个item:
<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary">
<TextView
android:id="@+id/tv_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="20dp"
android:textSize="16sp"/>
</LinearLayout>