Android自定義View實(shí)現(xiàn)公交成軌跡圖
本文實(shí)例為大家分享了Android自定義View實(shí)現(xiàn)公交成軌跡圖的具體代碼,供大家參考,具體內(nèi)容如下

總體分析下:水平方向recyclewview,item包含定位點(diǎn),站臺(tái)位置和站臺(tái)名稱。
下面看實(shí)現(xiàn):
1.繼承framelayout,實(shí)現(xiàn)構(gòu)造方法:
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) {
...
//設(shè)置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適配器:初始化的時(shí)候設(shè)置起點(diǎn)設(shè)置終點(diǎn)設(shè)置車(chē)道設(shè)置當(dāng)前車(chē)位置的下標(biāo)
/**
* 設(shè)置車(chē)道
*/
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);
}
}
/**
* 設(shè)置起點(diǎn)
*/
private void setStartStation(BaseViewHolder helper, BusStopPlateStationInfo item) {
helper.setVisible(R.id.v_daolu, false)
.setBackgroundRes(R.id.iv_jiantou, R.drawable.bg_busstop_vdaolu_start);
}
/**
* 設(shè)置終點(diǎn)
*/
private void setEndStation(BaseViewHolder helper, BusStopPlateStationInfo item) {
helper.setBackgroundRes(R.id.iv_jiantou, R.drawable.bg_busstop_vdaolu_end)
.setBackgroundRes(R.id.v_daolu, R.drawable.bg_busstop_vdaolu_end)
.setVisible(R.id.v_zhanwei, true)
.setVisible(R.id.v_daoli_zhanwei, false);
}
/**
* 設(shè)置當(dāng)前所在站點(diǎn)
*/
private void setCurrentStation(BaseViewHolder helper, BusStopPlateStationInfo item) {
mCurrentView = helper.getConvertView();
helper.setVisible(R.id.bus_stop_reach, true)
.setVisible(R.id.iv_bus_stop_current, false)
.setVisible(R.id.tv_bus_stop_current_num, false)
.setVisible(R.id.iv_current_point, true)
.setVisible(R.id.iv_admin_index, true)
// 顯示占位符,用于顯示一半的灰色
.setBackgroundRes(R.id.v_daoli_zhanwei, R.drawable.bg_busstop_vdaolu)
.setVisible(R.id.v_daoli_zhanwei, true);
// .setTextColor(R.id.tv_bus_station_name, Color.parseColor("#3D93FD"));
Glide.with(mContext)
.load(R.drawable.bus_icon_fangxiang_current)
.crossFade()
.into((ImageView) helper.getView(R.id.iv_current_point));
List<AliveBusInfo> aliveBusInfos = item.getAliveBusInfos();
if (aliveBusInfos != null && aliveBusInfos.size() != 0) {
AliveBusInfo aliveBusInfo = aliveBusInfos.get(0);
if ("1".equals(aliveBusInfo.getStStatus()) && aliveBusInfo.getStName().equals(item.getStName())) {
helper.setVisible(R.id.iv_admin_index, false)
.setVisible(R.id.iv_bus_stop_current, true)
.setImageResource(R.id.iv_bus_stop_current, R.drawable.bus_stop_current);
}
} else {
Glide.with(mContext)
.load(R.drawable.icon_admin_current_station)
.crossFade()
.into((ImageView) helper.getView(R.id.iv_admin_index));
}
}
/**
* 設(shè)置公交所在站點(diǎn)
*/
private void setBusStation(BaseViewHolder helper, BusStopPlateStationInfo item) {
List<AliveBusInfo> aliveBusInfos = item.getAliveBusInfos();
if (aliveBusInfos != null && aliveBusInfos.size() != 0) {
AliveBusInfo aliveBusInfo = aliveBusInfos.get(0);
if ("0".equals(aliveBusInfo.getStStatus())) {
// 在車(chē)道上
helper.setVisible(R.id.bus_stop_not_to, true)
.setVisible(R.id.bus_stop_reach, false)
.setText(R.id.tv_stop_not_to_num, String.valueOf(aliveBusInfos.size()))
// 顯示在過(guò)道中的車(chē)
.setVisible(R.id.iv_stop_not_to, aliveBusInfos.size() != 0)
// 是否顯示數(shù)字
.setVisible(R.id.tv_stop_not_to_num, aliveBusInfos.size() > 1);
// 如果已經(jīng)過(guò)站 顯示灰色圖標(biāo)
if (aliveBusInfo.getStCount() < 0) {
GlideUtils.loadImageView(mContext, R.drawable.bus_stop_over_station_min, helper.getView(R.id.iv_stop_not_to));
} else {
GlideUtils.loadImageView(mContext, R.drawable.bus_stop_not_to, helper.getView(R.id.iv_stop_not_to));
}
} else if ("1".equals(aliveBusInfo.getStStatus())) {
// 到站
helper.setVisible(R.id.bus_stop_not_to, false)
.setVisible(R.id.bus_stop_reach, true)
.setVisible(R.id.iv_admin_index, true)
.setVisible(R.id.iv_bus_stop_current, false)
.setVisible(R.id.tv_bus_stop_current_num, aliveBusInfo.getStCount() > 1)
.setText(R.id.tv_bus_stop_current_num, String.valueOf(aliveBusInfos.size()));
// 如果已經(jīng)過(guò)站 顯示灰色圖標(biāo)
if (aliveBusInfo.getStCount() < 0) {
GlideUtils.loadImageView(mContext, R.drawable.bus_stop_over_station, helper.getView(R.id.iv_admin_index));
} else {
GlideUtils.loadImageView(mContext, R.drawable.bus_stop_not_to, helper.getView(R.id.iv_admin_index));
}
}
} else {
// 隱藏公交車(chē)
helper.setVisible(R.id.bus_stop_not_to, false)
.setVisible(R.id.bus_stop_reach, false);
}
}
3.外部activity的點(diǎn)擊事件:點(diǎn)擊文字的時(shí)候?qū)?dāng)前位置對(duì)象刷新到選擇的位置,刷新recycleview
mBusStopPlateView.setOnBusStopPlateViewItemClick(new BusStopPlateView.onBusStopPlateViewEvent() {
@Override
public void onItemClick(BusStopPlateStationInfo station) {
stationId = station.getStId();
stationName = station.getStName();
exportStationInfo(mBusStopPlateView.getStationList());
aliveBusRefresh();
//當(dāng)上車(chē)提醒保存的信息與當(dāng)前候車(chē)站點(diǎn)信息不一致時(shí)恢復(fù)為上車(chē)提醒,
// 并在點(diǎn)擊上車(chē)提醒是判斷是否更新上車(chē)提醒的站點(diǎn)
BusRemind remind = SpKeyConfig.getOnRemind();
if (remind != null) {
if (remind.getStationId().equals(stationId) &&
remind.getLineId().equals(mLineId)) {
tvOnRemind.setText("取消提醒");
ivOnRemind.setImageResource(R.drawable.bus_icon_onremind_on);
} else {
tvOnRemind.setText("上車(chē)提醒");
ivOnRemind.setImageResource(R.drawable.bus_icon_onremind_off);
}
}
}
@Override
public void onCurrentViewPosition(int x, int y, boolean isVisibility) {
mIvPoint.setTranslationX(x - mIvPoint.getWidth() / 2 + 6);
mIvPoint.setVisibility(isVisibility ? View.VISIBLE : View.INVISIBLE);
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
初學(xué)Android之網(wǎng)絡(luò)封裝實(shí)例
大家好,本篇文章主要講的是初學(xué)Android之網(wǎng)絡(luò)封裝實(shí)例,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12
Android進(jìn)階——安卓調(diào)用ESC/POS打印機(jī)打印實(shí)例
本篇文章主要介紹了Android進(jìn)階——安卓調(diào)用ESC/POS打印機(jī)打印實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-04-04
Android+SQLite數(shù)據(jù)庫(kù)實(shí)現(xiàn)的生詞記事本功能實(shí)例
這篇文章主要介紹了Android+SQLite數(shù)據(jù)庫(kù)實(shí)現(xiàn)的生詞記事本功能,結(jié)合具體實(shí)例形式分析了Android操作SQLite數(shù)據(jù)庫(kù)實(shí)現(xiàn)生詞記錄功能的操作步驟與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-09-09
android實(shí)現(xiàn)下拉菜單三級(jí)聯(lián)動(dòng)
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)下拉菜單三級(jí)聯(lián)動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10
OpenGL Shader實(shí)例分析(1)Wave效果
這篇文章主要為大家詳細(xì)介紹了OpenGL Shader實(shí)例分析第一篇,Wave效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02
詳解RxJava2 Retrofit2 網(wǎng)絡(luò)框架簡(jiǎn)潔輕便封裝
本篇文章主要介紹了詳解RxJava2 Retrofit2 網(wǎng)絡(luò)框架簡(jiǎn)潔輕便封裝,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
Android實(shí)現(xiàn)單行標(biāo)簽流式布局
這篇文章主要為大家詳細(xì)介紹了Android單行標(biāo)簽流式布局,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09
RecyclerVIew實(shí)現(xiàn)懸浮吸頂效果
這篇文章主要為大家詳細(xì)介紹了RecyclerVIew實(shí)現(xiàn)懸浮吸頂效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09

