Android自定义自定义View实现公交成轨迹图实现公交成轨迹图
主要为大家详细介绍了Android自定义View实现公交成轨迹图,具有一定的参考价值,感兴趣的小伙伴们可以参
考一下
本文实例为大家分享了Android自定义View实现公交成轨迹图的具体代码,供大家参考,具体内容如下
总体分析下:水平方向recyclewview,item包含定位点,站台位置和站台名称。
下面看实现:
1.继承framelayout,实现构造方法:
public class BusStopPlateView extends FrameLayout {
...
public BusStopPlateView(@NonNull Context context) {
super(context);
initView(context);
}
public BusStopPlateView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initView(context);
}
public BusStopPlateView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context);
}
private void initView(Context context) {
...
//设置recycleview
LayoutInflater.from(context).inflate(R.layout.xxx, this, true);
mRecyclerView = (RecyclerView) findViewById(R.id.recycle);
mRecyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
mBusStopPlateAdapter = new BusStopPlateAdapter(mStationList);
mRecyclerView.setAdapter(mBusStopPlateAdapter);
...
}
...
}
2.recycleview适配器:初始化的时候设置起点设置终点设置车道设置当前车位置的下标
/**
* 设置车道
*/
private void setDriveway(BaseViewHolder helper, BusStopPlateStationInfo item) {
if (helper.getAdapterPosition() <= adminCurrentIndex) {
helper.getView(R.id.v_daolu).setSelected(true);
helper.getView(R.id.iv_jiantou).setSelected(true);
} else {
helper.getView(R.id.v_daolu).setSelected(false);
helper.getView(R.id.iv_jiantou).setSelected(false);
}
}
/**
* 设置起点
*/