0

How can I get positon of the selected rating bar in a list of rating bars inside rating bar listener which is in a recyclerview adapter as shown in below code? I am able to get the individual ratings of each rating bar when the user clicks it but I cant tell which rating bar the user I have clicked since there is no method of getting position of a particular rating bar in a list. I have placed the rating bar inside an adapter which populates the list in the recycler view as shown in the code below. I am using this library for the rating bar. and here is my code of the adapter where i have placed my listener, how can i get the position of the rating bar the user has selected inside my rating bar listener

    @Override
    public RateMa3Adapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
//        create a new view
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.rate, viewGroup, false);

        view.setOnClickListener(RateMa3.myOnClickListener);
        ViewHolder viewHolder = new ViewHolder(view);
        rootView = view;
        ButterKnife.bind(this, view);
        lowerRatingBar.setListener(ratingListener);

        return viewHolder;
    }

    private RatingListener ratingListener = new RatingListener() {
        @Override
        public void onRatePicked(ProperRatingBar ratingBar) {

            Snackbar.make(rootView,
                    String.format("Rating is %d " , ratingBar.getRating()),
                    Snackbar.LENGTH_SHORT).show();

            Log.e("Rate", ratingBar.getRating() + "  rating");

// HOW CAN I GET THE POSITION OF A RATING BAR THE USER HAS CLICKED IN HERE?? THATS MY QUESTION, SINCE I HAVE A LIST OF RATING BARS
        }
    };

1 Answer 1

2

The only code that should be in the onCreateViewHolder method should be:

View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.rate, viewGroup, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;

You should set the onClickListener in the onBindViewHolder(ViewHolder viewHolder, int position) method. There you can use the second parameter, which is the position in the RecyclerView

To set the onClickListener to the whole view, use viewHolder.itemView.setOnClickListener()

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.