24

我有一个 ListView,它使用不同的 XML 文件来创建视图并从中制作项目。这些 XML 文件之一包含 RatingBar。一切都显示出来,看起来很棒。

我正在尝试将 onClick 处理程序附加到 RatingBar 以启动新活动。我的 RatingBar 风格为 ?android:attr/ratingBarStyleSmall; 所以它只是一个指标(我希望小 RatingBar 点击将用户带到他们可以进行各种评分的活动)。

我的问题是 RatingBar 的 onClick 处理程序永远不会被执行。更有趣的是,我使用相同的代码使 LinearLayout 可点击,并且效果很好。谁能告诉我为什么?

我的适配器的 getView 如下所示:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    int type = getItemViewType(position);

    // get the View for this list item
    View v = convertView;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        switch (type) {
            // ...
            case TYPE_LOOKUP:
                v = vi.inflate(R.layout.layout_itemlist_itemlookup, parent, false);
                LinearLayout vLookup = (LinearLayout)v.findViewById(R.id.itemlist_lookup);
                if (vStore != null) {
                    vStore.setOnClickListener(new View.OnClickListener() {
                            public void onClick(View v) {
                                // THIS HANDLER WORKS FINE
                                Intent intentLaunchLookup = new Intent(ActivityItemList.this, ActivityLookup.class);
                                startActivity(intentLaunchLookup);
                            }
                        });
                }
                break;
            case TYPE_SEPARATOR:
                v = vi.inflate(R.layout.layout_itemlist_itemseparator, parent, false);
                RatingBar r = (RatingBar)v.findViewById(R.id.itemlist_rating);
                if (r != null) {
                    r.setOnClickListener(new View.OnClickListener() {
                            public void onClick(View v) {
                                // THIS HANDLER DOES NOT GET EXECUTED (r IS NOT NULL; SO THIS SHOULD HAVE BEEN CREATED)
                                Intent intentLaunchRating = new Intent(ActivityItemList.this, ActivityRating.class);
                                startActivity(intentLaunchRating);
                            }
                        });
                }
                break;
            // ...
        }
    }
    // …

  // return the created view
    return v;
}
4

6 回答 6

64

setOnClickListener()不工作的原因是RatingBar覆盖onTouchEvent()(实际上是它的超类,,AbsSeekBar确实)并且永远不会让View照顾它,所以View#performClick()永远不会被调用(它会调用OnClickListener)。

两种可能的解决方法:

    派生自RatingBar和覆盖onTouchEvent()
    改为使用OnTouchListener,如下所示:
    ratingBar.setOnTouchListener(new OnTouchListener() {
        @覆盖
        public boolean onTouch(View v, MotionEvent 事件) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                // TODO 在这里执行你的操作
            }
            返回真;
        }
    

在科特林

ratingBar.setOnTouchListener(View.OnTouchListener { v, event ->
                if (event.action == MotionEvent.ACTION_UP) {
                    // TODO perform your action here
                }
                return@OnTouchListener true
            })

HTH,乔纳斯

于 2010-10-24T20:48:17.677 回答
7

安德鲁,您可以尝试使用另一个可用于RatingBar的事件OnRatingBarChangeListener。看这个例子,它对我有用!

ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

        @Override
        public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
            viewMarketDetails(mContext);
            dialog.dismiss();
        }
    });

所以你不需要使用 onClickEvent 但你有同样的效果,但只有在你实际改变值的情况下才能工作。(如果点击实际值,如果没有效果)

于 2013-05-30T22:14:12.130 回答
6
ratingBar1.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

        @Override
        public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {

            String rateValue = String.valueOf(ratingbar1.getRating());
            System.out.println("Rate for Module is"+rateValue);
        }
    });             
于 2014-11-05T08:14:06.227 回答
5

我通过将 RatingBar 包装在 LinearLayout 中并将 onClick 侦听器附加到 LinearLayout 来解决这个问题。我不喜欢这样做,但它有效。由于某种神秘的原因,onClick 不会为 RatingBar 执行。

于 2010-08-11T18:19:54.760 回答
1

在 ListView 中,您通常必须决定是否希望列表视图项可点击,或者列表视图中包含的项。r.isClickable() 是否返回 true?

您可能有更好的运气在 RatingBar 上将 clickable 设置为 false,而是使用 listView 的 onClickListener 来处理 listView 行上的点击事件。

于 2010-08-09T20:57:39.497 回答
0

这将听取点击评级,并且不会对评级变化产生任何影响

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.appcompat.widget.AppCompatRatingBar
        android:id="@+id/rating_bar_current_rating"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/dp_16"
        android:maxHeight="@dimen/dp_23"
        android:minHeight="@dimen/dp_23"
        android:progressDrawable="@drawable/custom_rating_selector"
        android:stepSize="0"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_find_provider_subheader" />


    <TextView
        android:id="@+id/tv_current_rating"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        app:layout_constraintBottom_toBottomOf="@+id/rating_bar_current_rating"
        app:layout_constraintEnd_toEndOf="@+id/tv_current_rating"
        app:layout_constraintStart_toStartOf="@+id/rating_bar_current_rating"
        app:layout_constraintTop_toTopOf="@+id/rating_bar_current_rating" />

</androidx.constraintlayout.widget.ConstraintLayout>

只需在 ratingbar 顶部添加一个 textview 布局并收听 textview 布局的单击侦听器。约束布局内的两个视图。

在java代码中

Textview textview = findViewById(R.id.tv_current_rating)
textview.setOnClickListener(this)

@override
public void onClick(View v) {
  while (v.id) {
   case R.id.tv_current_rating :
   // TODO : This will show toast on click event 
   Toast.makeText(getApplicationContext(),
               "This a toast message",
               Toast.LENGTH_LONG)
     .show();
   break;
  }
}
于 2022-01-24T11:22:02.590 回答