单击标记

时间:2019-04-03 08:59:42

标签: java android google-maps android-snackbar

enter image description here我想在打开标记后点击以下标记打开一个快餐栏:

  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遮住了我的地图

3 个答案:

答案 0 :(得分:0)

如果您没有关注焦点,则可以使用默认的android系统,如下所示:

view = findViewById(android.R.id.content);

答案 1 :(得分:0)

尝试

使用此功能显示消息。

 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();
    }

针对您的用例

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;
            }
        });

答案 2 :(得分:0)

我创建了一个自定义的快餐栏,其中的视图包裹在 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();
    }
  }
}