怎么用Androidrecyclerview实现纵向虚线时间轴
本篇内容主要讲解“怎么用Android recyclerview实现纵向虚线时间轴”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么用Android recyclerview实现纵向虚线时间轴”吧!
创新互联建站坚持“要么做到,要么别承诺”的工作理念,服务领域包括:网站建设、成都网站设计、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的兴宁网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!
效果图
代码
package com.jh.timelinedemo; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.DashPathEffect; import android.graphics.Paint; import android.util.AttributeSet; import android.view.View; /** * @Description: Android自定义虚线 * @Date 2019-07-20 10:07 * @Version */ public class DividerView extends View { static public int ORIENTATION_HORIZONTAL = 0; static public int ORIENTATION_VERTICAL = 1; private Paint mPaint; private int orientation; public DividerView(Context context) { this(context, null); } public DividerView(Context context, AttributeSet attrs) { super(context, attrs); int dashGap, dashLength, dashThickness; int color; TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.DividerView, 0, 0); try { dashGap = a.getDimensionPixelSize(R.styleable.DividerView_dashGap, 5); dashLength = a.getDimensionPixelSize(R.styleable.DividerView_dashLength, 5); dashThickness = a.getDimensionPixelSize(R.styleable.DividerView_dashThickness, 3); color = a.getColor(R.styleable.DividerView_divider_line_color, 0xff000000); orientation = a.getInt(R.styleable.DividerView_divider_orientation, ORIENTATION_HORIZONTAL); } finally { a.recycle(); } mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setColor(color); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeWidth(dashThickness); mPaint.setPathEffect(new DashPathEffect(new float[]{dashGap, dashLength,}, 0)); } public void setBgColor(int color) { mPaint.setColor(color); invalidate(); } @Override protected void onDraw(Canvas canvas) { if (orientation == ORIENTATION_HORIZONTAL) { float center = getHeight() * 0.5f; canvas.drawLine(0, center, getWidth(), center, mPaint); } else { float center = getWidth() * 0.5f; canvas.drawLine(center, 0, center, getHeight(), mPaint); } } }
package com.jh.timelinedemo; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import android.os.Bundle; import android.view.View; public class MainActivity extends AppCompatActivity { private RecyclerView rcy; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rcy = findViewById(R.id.rcy); LinearLayoutManager manager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false); rcy.setLayoutManager(manager); TimeLineAdapter adapter = new TimeLineAdapter(this); rcy.setAdapter(adapter); } }
package com.jh.timelinedemo; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; /** * * @date:on 2021/7/21 17:38 */ public class TimeLineAdapter extends RecyclerView.Adapter{ private Context context; public TimeLineAdapter(Context context) { this.context = context; } @NonNull @Override public TimeLineAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, null); ViewHolder viewHolder = new ViewHolder(inflate); return viewHolder; } @Override public void onBindViewHolder(@NonNull TimeLineAdapter.ViewHolder holder, int position) { holder.line_up.setVisibility(position == 0 ? View.INVISIBLE : View.VISIBLE);//第一条数据隐藏头部线 holder.line_down.setVisibility(position == 4 ? View.INVISIBLE : View.VISIBLE);//最后一条数据隐藏底部线 } @Override public int getItemCount() { return 5; } class ViewHolder extends RecyclerView.ViewHolder { private final DividerView line_up, line_down; public ViewHolder(@NonNull View itemView) { super(itemView); line_up = itemView.findViewById(R.id.line_up); line_down = itemView.findViewById(R.id.line_down); } } }
到此,相信大家对“怎么用Android recyclerview实现纵向虚线时间轴”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
分享名称:怎么用Androidrecyclerview实现纵向虚线时间轴
浏览路径:http://scjbc.cn/article/ggheho.html