简体   繁体   中英

Java android open snackBar after click on marker

在此输入图像描述 I want to open a snackBar after that I clicked on marker I tr do this :

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

But my application is crashed and in the log I see :

java.lang.IllegalArgumentException: No suitable parent found from the given view. Please provide a valid view.

Ok I do this and it works:

getWindow().getDecorView(),

But now when show Snackbar I want to see the whole map, this Snackbar cover my map

如果您没有焦点视图,可以使用默认的android视图,如下所示:

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

try this

Use this function to show the message.

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

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

I created a custom snackbar , where the views are wrapped within a CoordinatorLayout for a Fragment.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM