自定义视图 RatingBar 调用了 setOnTouchListener 但没有覆盖 performClick

Custom view RatingBar has setOnTouchListener called on it but does not override performClick

我在布局中使用 RatingBar 作为 1 星,如下所示 -

<RatingBar
            android:id="@+id/ratingBar"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_below="@id/textViewReleaseDate"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="16dp"
            android:layout_toRightOf="@id/imageViewPoster"
            android:numStars="1"
            android:stepSize="1.0" />

并在我的 activity 中设置 setOnTouchListener,如下所示 -

ratingBar.setOnTouchListener(new View.OnTouchListener() {

        int ratingAtActionDown;

        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {

            if (motionEvent.getAction() == MotionEvent.ACTION_DOWN)
                ratingAtActionDown = (int) ratingBar.getRating();
            else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                ratingBar.setRating(ratingAtActionDown == 0 ? 1 : 0);
            }

            return true;
        }
    });

对于上面的代码片段,我收到了这个警告 -

Custom view 'RatingBar' has setOnTouchListener called on it but does not override performClick

Android Studio 2.3.3 ratingBar.setOnTouchListener 上没有生成警告但是在升级到 Android Studio 之后3.0 稳定 它开始警告。

应该怎么做才能消除警告?

Lint 似乎错误地认为任何未实现 performClick() 方法的视图都是自定义视图。知道这一点,我们可以猜测受此警告影响的视图实际上缺少该实现。

现在回答您的问题,如果您希望警告消失,您可能需要扩展要设置 onTouchListener 的视图:

class TouchableRatingBar extends android.support.v7.widget.AppCompatRatingBar{

    public TouchableRatingBar(Context context) {
        super(context);
    }
    @Override
    public boolean performClick() {
        return true;
    }
}

覆盖 performClick() 方法,您应该可以开始了。

请注意,我使用了 AppCompatRatingBar,因为 Lint 似乎不喜欢这样做。

您可能还需要双投评分栏或更改其在布局中的类型。

双重演员:

TouchableRatingBar ratingBar = (TouchableRatingBar)(RatingBar)findViewById(R.id.ratingBar);

我个人不会使用双演员,但如果您需要有一个替代方法来简单地更改布局中的类型,它可能会完成这项工作。

类型更改:

<yourcompany.yourproject.TouchableRatingBar
            android:id="@+id/ratingBar"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_below="@id/textViewReleaseDate"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="16dp"
            android:layout_toRightOf="@id/imageViewPoster"
            android:numStars="1"
            android:stepSize="1.0" />