0

我正在尝试寻找另一种替代解决方案来检查用户是否在帖子上评分。

问题是,如果我使用 OnRatingBarChangeListener 另一个用户可能会投票并更改将触发事件的评级。

我尝试过 onClickListener 和 OnTouchListener 并没有真正成功,因为它们覆盖了初始方法并且无法与评级栏正确交互......

有什么建议么?

 if (ratingSimplePost.getDidRate() == true) {

         ratingViewHolder.starRating.setRating((float)ratingSimplePost.getAverageRating());
         ratingViewHolder.starRating.isIndicator();

        }else if(ratingSimplePost.getDidRate() == false){

            ratingSimplePost.setDidRate(true);
            ratingViewHolder.starRating.setOnRatingBarChangeListener(new SimpleRatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(SimpleRatingBar simpleRatingBar, float rating, boolean fromUser) {
    if (ratingViewHolder.starRating.getRating() == 0) {

    } else {
        ratingViewHolder.starRating.setRating(ratingViewHolder.starRating.getRating());

        double avgSum = ratingSimplePost.getAverageRating() * ratingSimplePost.getParticipantCount();
        double myRating = ratingViewHolder.starRating.getRating();

        myRating = (Math.round(myRating * 100));
        myRating = myRating / 100;


        double newRating = (avgSum + myRating) / (ratingSimplePost.getParticipantCount() + 1);
        newRating = (Math.round(newRating * 100));
        newRating = newRating / 100;


        ParseQuery<ParseObject> updateRating = ParseQuery.getQuery("Posts");
        updateRating.whereEqualTo("objectId", ratingSimplePost.getPostId());

        final double finalNewRating = newRating;
        final double finalMyRating = myRating;
        ratingSimplePost.setAverageRating(newRating);

        updateRating.findInBackground(new FindCallback<ParseObject>() {
            public void done(List<ParseObject> objects, ParseException e) {
                if (e == null) {
                    for (int i = 0; i < objects.size(); i++) {
                        ParseObject object = objects.get(i);
                        object.put("AvgRating", finalNewRating);
                        ArrayList<Double> ratingValues = (ArrayList<Double>) object.get("Ratings");
                        ratingValues.add((double) finalMyRating);
                        object.put("Ratings", ratingValues);
                        ratingSimplePost.setNewRating(finalNewRating);
                        ArrayList<String> partList = (ArrayList<String>) object.get("Participants");
                        partList.add(ParseUser.getCurrentUser().getUsername());
                        object.put("Participants", partList);
                        object.saveInBackground();
                        ratingViewHolder.starRating.setRating((float) finalNewRating);
                    }

                }
            }
        });
    }
   }
 });
4

1 回答 1

1

我是您正在使用的库的创建者:SimpleRatingBar

您需要boolean fromUseronRatingChanged方法中使用 的值。这与您对股票 android RatingBar 的期望具有相同的含义:

  • 如果评分因用户操作而改变(通过触摸评分栏),则为真
  • 如果评分以编程方式更改(通过调用 setRating 或使用动画),则为 False

因此,在您的代码中:

@Override
public void onRatingChanged(SimpleRatingBar simpleRatingBar, float rating, boolean fromUser) {
    if (fromUser) {
        // user changed the rating by touching the rating bar

        double avgSum = ratingSimplePost.getAverageRating() * ratingSimplePost.getParticipantCount();
        double myRating = simpleRatingBar.getRating();

        myRating = (Math.round(myRating * 100));
        myRating = myRating / 100;


        double newRating = (avgSum + myRating) / (ratingSimplePost.getParticipantCount() + 1);
        newRating = (Math.round(newRating * 100));
        newRating = newRating / 100;


        ParseQuery<ParseObject> updateRating = ParseQuery.getQuery("Posts");
        updateRating.whereEqualTo("objectId", ratingSimplePost.getPostId());

        final double finalNewRating = newRating;
        final double finalMyRating = myRating;
        ratingSimplePost.setAverageRating(newRating);

        updateRating.findInBackground(new FindCallback<ParseObject>() {
            public void done(List<ParseObject> objects, ParseException e) {
                if (e == null) {
                    for (int i = 0; i < objects.size(); i++) {
                        ParseObject object = objects.get(i);
                        object.put("AvgRating", finalNewRating);
                        ArrayList<Double> ratingValues = (ArrayList<Double>) object.get("Ratings");
                        ratingValues.add((double) finalMyRating);
                        object.put("Ratings", ratingValues);
                        ratingSimplePost.setNewRating(finalNewRating);
                        ArrayList<String> partList = (ArrayList<String>) object.get("Participants");
                        partList.add(ParseUser.getCurrentUser().getUsername());
                        object.put("Participants", partList);
                        object.saveInBackground();
                        ratingViewHolder.starRating.setRating((float) finalNewRating);
                    }

                }
            }
        });
      }
    }
   }

另外,请记住,有一个错误导致库始终返回 false 的值fromUser。这将在版本中得到纠正0.1.1,我将在几分钟后发布。

您可以从jcenter或下载此版本Maven Central。只需添加到您的build.gradle文件中:

dependencies {
    compile 'com.iarcuschin:simpleratingbar:0.1.+'
}
于 2016-08-20T03:20:07.690 回答