简体   繁体   English

Java android打开snackBar点击标记后

[英]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. java.lang.IllegalArgumentException:从给定视图中找不到合适的父级。 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 但是现在当我展示Snackbar我想看到整个地图时,这个Snackbar覆盖了我的地图

如果您没有焦点视图,可以使用默认的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 对于您的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. 我创建了一个自定义快餐栏,其中视图包含在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.

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