简体   繁体   中英

Snackbar inside Recycler view

My logic doesn't work only when there is not permission for Call, If i have provided permission it works perfectly fine.

I am trying to invoke Snackbar inside my recycler view, Reason being one of my button in recycler view has to invoke Call facility. Hence i am checking for Manifest.permission.CALL_PHONE permission and displaying a snakbar if there is no permission provided. While invoking the below code

private void showSnackbar(MyViewHolder v , final int mainTextStringId, final int actionStringId,
                          View.OnClickListener listener) {
    Snackbar.make(v.snackBarLayout,
            context.getString(mainTextStringId),
            Snackbar.LENGTH_INDEFINITE)
            .setAction(context.getString(actionStringId), listener).show();
}

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

However i am trying to parse my Viewholder form

 @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, int position) {
....
....
....
  showSnackbar(holder,R.string.permission_rationale, android.R.string.ok,
                                    new View.OnClickListener() {
                                        @Override
                                        public void onClick(View view) {
                                            // Request permission
                                            startCallPermissionRequest();
                                        }
                                    });
}

Response to suggested duplicate

I don't think this is a duplicate question, I tried changing snackbar to findviewbyId, It is not able to fetch that view to display. view comes only from ViewHolder.

You are putting holder as parent view for Snackbar

 showSnackbar(holder,R.string.permission_rationale, android.R.string.ok,
                                new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        // Request permission
                                        startCallPermissionRequest();
                                    }
                                });

Instead of holder you need object of parent view(LinearLayout /RelativeLayout or whatever you have given)inside xml connected with this adapter class

Finally i was able to solve it, The issue was with getting the parent view i did this to make it work view.getRootView()

  private void showSnackbar(View v , final int mainTextStringId, final int actionStringId,
                              View.OnClickListener listener) {
        Snackbar.make(view.getRootView().findViewById(android.R.id.content),
                context.getString(mainTextStringId),
                Snackbar.LENGTH_INDEFINITE)
                .setAction(context.getString(actionStringId), listener).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