2017-06-02 134 views
10

我在每行中使用RecyclerViewRatingBar。現在,有時候RatingBar的一部分被錯誤地繪製。離開一個屏幕後,回去一切都恢復正常。我不知道爲什麼會發生這種情況,我甚至從RatingBar中刪除了任何樣式,所以它應該具有默認外觀。RatingBar變得半透明

這是它的外觀:

enter image description here

測試在Nexus 6P(安卓7.1.1)。 也在三星Galaxy J3(2016)(Android 5.1.1)上測試過,這裏沒有問題。

我也onBindViewHolder()添加

holder.rbRating.requestLayout(); 

。它減少了一些問題,但仍然存在。當我重複使用時,將一個「壞」行滾出屏幕時,它看起來很好。

這裏的排佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:paddingTop="@dimen/main_margin_top" 
    android:paddingBottom="@dimen/main_margin_top" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <TextView 
     android:id="@+id/tvRatingTitle" 
     android:text="Czystość wody" 
     android:textSize="16sp" 
     android:textStyle="bold" 
     android:layout_centerHorizontal="true" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <RatingBar 
     android:id="@+id/rbRating" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="4dp" 
     android:isIndicator="true" 
     android:layout_centerHorizontal="true" 
     android:layout_below="@+id/tvRatingTitle" 
     android:numStars="5" 
     android:rating="2.5" 
     android:stepSize="0.1" /> 

    <CheckBox 
     android:id="@+id/chkMissing" 
     android:text="Zaznacz jeśli nie ma" 
     android:layout_centerHorizontal="true" 
     android:layout_below="@+id/rbRating" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

</RelativeLayout> 

我也嘗試切換到android.support.v7.widget.AppCompatRatingBar,但它並沒有任何區別。

編輯:

@Muhib皮拉尼的解決方案似乎是工作,但我有2個細小的問題:在Nexus 6P

1)明星的第一層是由幾個像素抵消(放大查看):

enter image description here

2)三星Galaxy J3(2016),它看起來是這樣的:

enter image description here

我很喜歡邊框,但我希望它們在空的星星上是綠色的(不是灰色的,背景應該是灰色的)。

+0

捕獲視圖層次通過'工具 - > Android的 - >佈局Inspector',看看什麼是那些'RatingBar's之間的關鍵區別。一旦找到導致此行爲的屬性,找到實際原因將更容易。它看起來像是改變了alpha通道,或者以某種方式應用了色彩。 – azizbekian

回答

3

我也遇到了同樣的問題。我不得不以編程方式設置的顏色RatingBar這樣,它的工作:

LayerDrawable layerDrawable = (LayerDrawable) ratingBar.getProgressDrawable(); 
layerDrawable.getDrawable(2).setColorFilter(ContextCompat.getColor(context, R.color.colorPrimaryDark), PorterDuff.Mode.SRC_ATOP); 
layerDrawable.getDrawable(1).setColorFilter(ContextCompat.getColor(context, R.color.colorPrimaryDark), PorterDuff.Mode.SRC_ATOP); 
layerDrawable.getDrawable(0).setColorFilter(ContextCompat.getColor(context, R.color.editTextBor), PorterDuff.Mode.SRC_ATOP);//when not selected color. 

保持layerDrawable.getDrawable(2)layerDrawable.getDrawable(1)相同的顏色。

+0

嘗試設置layerDrawable.getDrawable(0).setColorFilter(ContextCompat.getColor(context,android.R.color.transparent),PorterDuff.Mode.SRC_ATOP); –

+0

然後它看起來像這樣:http://imgur.com/a/czcJC(Nexus 6P)和http://imgur.com/a/wUvgH(Samsung Galaxy J3) - 不是我期望的。 – Makalele

+0

我在Oneplus x設備上檢查它,它的工作正常,我猜測評級欄需要一些改進。 –

1

我也有問題與原生RatingBar,所以我決定使用MaterialRatingBar。它解決了我所有的問題。

希望它可以幫助

+1

真正的解決方案,迄今爲止沒有問題。 – Makalele