在滚动Recycler View时,RatingBar Listener正在调用

时间:2017-11-09 06:48:23

标签: android adapter ratingbar

我正在使用RecyclerView,我的观点包含了ratindBar。问题是,当我向下滚动时,我的RatingBar监听器一次又一次地调用。我在下面提供适配器类代码。我有一个方法,当我滚动时多次调用,实际上只有当我触摸评级栏时才需要调用。

提前致谢。

public class Order_Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements Filterable {



    public void setiCartListener(ICart iCartListener) {
        this.iCartListener = iCartListener;
    }


    private OnItemClickListener onItemClickListener;



    @Override
    public int getItemViewType(int position) {



        if (isPositionFooter(position)) {
            return VIEW_TYPE_LOADING;
        }else if(filter.equals("") && position == 0 && isWorkshop ){
            ParseObject parseObject = arrayList.get(0);
            if(parseObject.has("isBanner")&& parseObject.getBoolean("isBanner")== true){
                return TYPE_HEADER;
            }
          }
        return VIEW_TYPE_ITEM;
    }

    private boolean isPositionFooter(int position) {
        return position == arrayList.size();
    }

    @Override
    public RecyclerView.ViewHolder  onCreateViewHolder(ViewGroup parent, int viewType) {


            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.order_view_list, parent, false);
            return new MemberViewHolder(view, onItemClickListener);

    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder  holder, int position) {

        final ParseObject parseObject = arrayList.get(position);
            final CartUtils cartUtils = cartUtilsItemsList.get(position);
        if(holder instanceof  MemberViewHolder){

            if (arrayList.size() > 0) {


                // ----------------  Rating Block  Start -----------------

                ((MemberViewHolder) holder).ratingBar.setEnabled(true);


                RatingBar.OnRatingBarChangeListener l=
                        new RatingBar.OnRatingBarChangeListener() {
                            public void onRatingChanged(RatingBar ratingBar,
                                                        float rating, boolean fromTouch) {
// This method is calling again and again when I am scrolling
                                dialogBoxRating(parseObject,rating);

                            }
                        };

                ((MemberViewHolder) holder).ratingBar.setOnRatingBarChangeListener(l);

                // Listeners Block  End

            }
        }
    }

    public void dialogBoxRating(final ParseObject pObject, final double ratingStar){
        DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int choice) {
                switch (choice) {
                    case DialogInterface.BUTTON_POSITIVE:
                      Toast.message("saved");
                        break;
                    case DialogInterface.BUTTON_NEGATIVE:
                        break;
                }
            }
        };

        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setMessage(R.string.do_you_want_to_save_the_rating).setTitle(R.string.cancel)
                .setPositiveButton(R.string.yes, dialogClickListener)
                .setNegativeButton(R.string.no, dialogClickListener).show();
    }


    @Override
    public int getItemCount() {
        return arrayList == null ? 0 : arrayList.size();
    }


    static class MemberViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        RatingBar ratingBar;

        public MemberViewHolder(View itemView, OnItemClickListener onItemClickListener) {
            super(itemView);

            ratingBar= (RatingBar) itemView.findViewById(R.id.ratingBar);
            itemView.setOnClickListener(this);
            this.onItemClickListener = onItemClickListener;
        }

        @Override
        public void onClick(View v) {

        }


    }

}

1 个答案:

答案 0 :(得分:0)

我的问题是通过检查布尔'fromTouch'来解决的。它是onTouch事件的评级栏监听器的变量。没有触摸它会给出错误的价值和触摸真实。我做了以下代码,现在终于工作了。

ratingBar= (RatingBar) itemView.findViewById(R.id.ratingBar);
    itemView.setOnClickListener(this);
    this.onItemClickListener = onItemClickListener;

    //should set listener here
    RatingBar.OnRatingBarChangeListener l=
            new RatingBar.OnRatingBarChangeListener() {
                public void onRatingChanged(RatingBar ratingBar,
                                            float rating, boolean fromTouch) {

if(fromTouch == true){
                            dialogBoxRating(parseObject,rating);
                        }

                }
            };
    ratingBar.setOnRatingBarChangeListener(l);