package zhangphil.book;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView mRecyclerView = findViewById(R.id.recycler_view);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(linearLayoutManager);
RecyclerViewAdapter adapter = new RecyclerViewAdapter();
mRecyclerView.setAdapter(adapter);
RVItemDecoration mRVItemDecoration = new RVItemDecoration(getApplicationContext());
mRecyclerView.addItemDecoration(mRVItemDecoration);
}
private class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolder> {
@NonNull
@Override
public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(getApplicationContext()).inflate(android.R.layout.simple_list_item_1, parent, false);
RecyclerViewHolder holder = new RecyclerViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull RecyclerViewHolder holder, int position) {
holder.text1.setText(position + "");
}
@Override
public int getItemCount() {
return Integer.MAX_VALUE;
}
}
private class RecyclerViewHolder extends RecyclerView.ViewHolder {
public TextView text1;
public RecyclerViewHolder(View itemView) {
super(itemView);
text1 = itemView.findViewById(android.R.id.text1);
}
}
private class RVItemDecoration extends RecyclerView.ItemDecoration {
private Drawable mDrawable;
//轴线距离左侧的距离,单位pix。
private int marginLeft = 20;
//轴线的宽度,单位pix。
private int lineSize = 10;
private Paint mPaint;
public RVItemDecoration(Context context) {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setColor(Color.RED);
mDrawable = new ColorDrawable(ContextCompat.getColor(getApplicationContext(), android.R.color.holo_blue_light));
}
@Override
public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
canvas.save();
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
//先画线。
mDrawable.setBounds(marginLeft, child.getTop(), marginLeft + lineSize, child.getBottom());
mDrawable.draw(canvas);
//后画覆盖在线上的圆球。
canvas.drawCircle(marginLeft + lineSize / 2, (child.getTop() + child.getBottom()) / 2, (lineSize + marginLeft) / 2, mPaint);
}
canvas.restore();
}
/**
* outRect.set()函数相当于给RecyclerView的每一个item的View四周设置margin值。
* 此处的margin值不同于onDraw里面的margin,onDraw里面给mDrawable设置的margin,仅仅是针对分割条而言。
*
* @param outRect
* @param view
* @param parent
* @param state
*/
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
outRect.set((marginLeft + lineSize) * 2, 0, 0, 0);
//测试。给RecyclerView的每一个item设置一个浅灰色背景。
//便于观察绘制结果。
view.setBackgroundColor(Color.LTGRAY);
}
}
}
so: