【问题标题】:RatingBar onClickRatingBar onClick
【发布时间】:2011-03-27 12:21:37
【问题描述】:

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

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

我的问题是 RatingBar 的 onClick 处理程序永远不会被执行。更有趣的是,我使用相同的代码来制作可点击的 LinearLayout 并且它工作正常。谁能告诉我为什么?

我的 Adapter 的 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;
}

【问题讨论】:

  • 有人在这方面有任何额外的 cmets 吗?我仍然无法执行 onClick 处理程序。如您所见,我对 LinearLayout 使用相同的代码,它工作正常。我的 RatingBar 设置为可点击。我在 onClick 侦听器中为 LinearLayout 和 RatingBar 设置了断点,并且只有 LinearLayout 处理程序被执行。注意:RatingBar 不在 LinearLayout 内。如您所见,它们位于两个不同的 XML 文件中。

标签: android onclick rating


【解决方案1】:

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

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

【讨论】:

  • 我不希望整个列表项都可以点击,但我希望列表项中的单个项目可以点击。我已经通过 LinearLayout 项目实现了这一点。代码完全相同,并且 onClick 处理程序完美执行。我不明白为什么 RatingBar 没有。我会更新我的主要帖子以反映这一点,1 分钟
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    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, 乔纳斯

    【讨论】:

      【解决方案4】:

      Andrew,您可以尝试使用另一个可用于 RatingBarOnRatingBarChangeListener 的事件。看这个例子,它对我有用!

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

      因此,您不需要使用 onClickEvent,但您具有相同的效果,但只有在您实际更改值时才有效。 (如果点击实际值,如果没有效果)

      【讨论】:

        【解决方案5】:
        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);
                }
            });             
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多