![](/img/trans.png)
[英]Java android google maps clustering , change icon in marker after click on them
[英]Java android open snackBar after click on marker
gMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
for(PGO pgo : pgoList.pgos){
if(pgo.getFull().equalsIgnoreCase(marker.getTitle())){
currentPGO = pgo;
view = getCurrentFocus();
mySnackbar(pgo,view);
}
}
return false;
}
});
public void mySnackbar(PGO pgo, View view) {
LinearLayout.LayoutParams objLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
Snackbar snackbar = Snackbar.make(view, "", Snackbar.LENGTH_INDEFINITE);
Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) snackbar.getView();
layout.setPadding(0, 0, 0, 0);
LayoutInflater mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View snackView = getLayoutInflater().inflate(R.layout.my_snackbar, null);
Button textViewOne = (Button) snackView.findViewById(R.id.txtOne);
TextView tvName = (TextView) snackView.findViewById(R.id.name);
tvName.setText(pgo.getFull());
textViewOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("One", "First one is clicked");
snackbar.dismiss();
}
});
Button textViewTwo = (Button) snackView.findViewById(R.id.txtTwo);
textViewTwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("Two", "Second one is clicked");
}
});
layout.addView(snackView, objLayoutParams);
snackbar.show();
}
但我的應用程序崩潰了,在日志中我看到:
java.lang.IllegalArgumentException:從給定視圖中找不到合適的父級。 請提供有效的觀點。
好的,我這樣做,它的工作原理:
getWindow().getDecorView(),
但是現在當我展示Snackbar
我想看到整個地圖時,這個Snackbar
覆蓋了我的地圖
如果您沒有焦點視圖,可以使用默認的android視圖,如下所示:
view = findViewById(android.R.id.content);
嘗試這個
使用此功能顯示消息。
public void showMessage(Activity context, String message) {
Snackbar snackbar = Snackbar.make(context.getWindow().getDecorView().findViewById(android.R.id.content), message, Snackbar.LENGTH_LONG);
View snackBarView = snackbar.getView();
snackBarView.setBackgroundColor(context.getResources().getColor(R.color.colorPrimary));
TextView textView = (TextView) snackBarView.findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(context.getResources().getColor(R.color.white));
snackbar.show();
}
對於您的UseCase
gMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
for(PGO pgo : pgoList.pgos){
if(pgo.getFull().equalsIgnoreCase(marker.getTitle())){
currentPGO = pgo;
showMessage(MainActivity.this,pgo.getFull());
}
}
return false;
}
});
我創建了一個自定義快餐欄,其中視圖包含在Fragment的CoordinatorLayout中。
@Override
public boolean onMarkerClick(Marker marker) {
if (marker.getTag() != null) {
marker.showInfoWindow();
int tag = (int) marker.getTag();
showCustomSnackBar(marker, 10000, tag);
}
return false;
private void showCustomSnackBar(Marker marker, int duration, int tag) {
if (getActivity() != null && !(getActivity()).isFinishing()) {
CoordinatorLayout rootCoordinatorLayout = view.findViewById(R.id.parent_coordinator_layout);
Snackbar snackbar = Snackbar.make(rootCoordinatorLayout, "", duration);
Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) snackbar.getView();
layout.setBackgroundColor(getResources().getColor(R.color.white));
LayoutInflater objLayoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View snackView = objLayoutInflater.inflate(R.layout.snackbar_layout_view, null);
ImageView someImgView = snackView.findViewById(R.id.someImageView);
TextView sometxt = snackView.findViewById(R.id.snackbar_msg);
someImgView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
snackbar.dismiss();
}
});
layout.addView(snackView, 0);
snackbar.show();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.