如何以分值显示评级栏评级?

时间:2017-02-14 08:52:30

标签: android android-layout android-studio

我使用星形图像创建了评分栏。但是我将设置评级值设置为浮动(如3.2)它无法正常工作。它会选择4星。我能做什么?

xml:

<RatingBar
                        android:id="@+id/rb_ratingBar"
                        style="@style/CustomRating"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:isIndicator="true"
                        android:numStars="5"
                        android:stepSize="0.5" />

styles.xml:

<style name="CustomRating" parent="android:style/Widget.Material.RatingBar.Small">

        <item name="android:progressDrawable">@drawable/custom_ratingbar</item>
        <item name="android:minHeight">40dp</item>
        <item name="android:maxHeight">40dp</item>
</style>

customratingbar.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@android:id/background"
        android:drawable="@drawable/ic_star_normal"

        ></item>

    <item
        android:id="@android:id/secondaryProgress"
        android:drawable="@drawable/ic_star_selected"></item>

    <item
        android:id="@android:id/progress"
        android:drawable="@drawable/ic_star_selected"></item>

</layer-list> 

output preview image

代码:

  @Override
    public void onBindViewHolder(final MyViewHolders holder, final int listPosition) {

        TextView TVProject = holder.mTvProjectName;
        TextView TvDescription = holder.mTvDescription;
        RatingBar RbRating = holder.mRatingBar;
        ImageView IvIcon = holder.IvIcon;
        TextView TvRatings = holder.mTvRatingNumber;
        final ProgressBar progressBar = holder.mProgressBar;

     /*   LayerDrawable stars = (LayerDrawable) RbRating.getProgressDrawable();
        stars.getDrawable(2).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);    //background
        stars.getDrawable(0).setColorFilter(Color.GREEN, PorterDuff.Mode.SRC_ATOP);    //empty
        stars.getDrawable(1).setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);      //border*/



        String receivedImage = mContext.getString(R.string.WS_DIRECTORY_IMAGE) +
                mRatingReviewModelArrayList.get(listPosition).getCompanyImage();
        String receivedProjectName = mRatingReviewModelArrayList.get(listPosition).getProj_Name();
        String receivedDescription = mRatingReviewModelArrayList.get(listPosition).getReview();
        Float receivedRating = mRatingReviewModelArrayList.get(listPosition).getAverage();

        ImageLoader.getInstance().displayImage(receivedImage, IvIcon, new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String imageUri, View view) {
                progressBar.setVisibility(View.VISIBLE);
            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                progressBar.setVisibility(View.GONE);
            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                progressBar.setVisibility(View.GONE);
            }

            @Override
            public void onLoadingCancelled(String imageUri, View view) {
                progressBar.setVisibility(View.GONE);
            }
        });
        if (receivedDescription.equals("")) {
            TvDescription.setVisibility(View.INVISIBLE);
        }
        if (receivedProjectName.equals("")) {
            TVProject.setVisibility(View.INVISIBLE);
        }

        TVProject.setText(receivedProjectName);
        TvDescription.setText(receivedDescription);
        RbRating.setRating(receivedRating);
        RbRating.setRating((float) 3.5);
        TvRatings.setText(String.valueOf(receivedRating));
    }

    @Override
    public int getItemCount() {
        return mRatingReviewModelArrayList.size();
    }

    static class MyViewHolders extends RecyclerView.ViewHolder {

        private final TextView mTvProjectName, mTvDescription, mTvRatingNumber;
        private final RatingBar mRatingBar;
        private final ImageView IvIcon;
        private final ProgressBar mProgressBar;

        MyViewHolders(View itemView) {
            super(itemView);
            this.mProgressBar = (ProgressBar) itemView.findViewById(R.id.progressBar_review);
            this.mTvProjectName = (TextView) itemView.findViewById(R.id.tv_projects);
            this.mTvDescription = (TextView) itemView.findViewById(R.id.tv_review_description);
            this.mRatingBar = (RatingBar) itemView.findViewById(R.id.rb_ratingBar);
            this.IvIcon = (ImageView) itemView.findViewById(R.id.img_rating_icon);
            this.mTvRatingNumber = (TextView) itemView.findViewById(R.id.tv_rating_number);

        }
    }

2 个答案:

答案 0 :(得分:0)

将RatingBar更改为以下

<RatingBar
                        android:id="@+id/rb_ratingBar"
                        style="@style/CustomRating"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:isIndicator="true"
                        android:numStars="5"
                        android:stepSize="0.1" />

它对我有用..

答案 1 :(得分:0)

以此替换您的xml代码。

              <RatingBar
                android:id="@+id/ratingBar"
                style="?android:attr/ratingBarStyleSmall"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:stepSize="0.01"
                android:numStars="5" />