2017-06-02 140 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