RatingBar onClick

后端 未结 5 877
孤街浪徒
孤街浪徒 2020-12-25 13:59

I have a ListView that uses different XML files to create Views and make items out of. One of these XML files contains a RatingBar. Everything displays and looks excellent

相关标签:
5条回答
  • 2020-12-25 14:26

    The reason for setOnClickListener() not working is that RatingBar overrides onTouchEvent() (actually its super class, AbsSeekBar, does) and never let View take care of it, so View#performClick() is never called (which would have called the OnClickListener).

    Two possible workarounds:

      derive from RatingBar and override onTouchEvent()
      use OnTouchListener instead, like so:
      ratingBar.setOnTouchListener(new OnTouchListener() {
          @Override
          public boolean onTouch(View v, MotionEvent event) {
              if (event.getAction() == MotionEvent.ACTION_UP) {
                  // TODO perform your action here
              }
              return true;
          }
      

    HTH, Jonas

    0 讨论(0)
  • 2020-12-25 14:26

    In a ListView, you generally have to decide if you want the list view items to be clickable, or an item contained within the listview. Does r.isClickable() return true?

    You might have better luck setting clickable to false on the RatingBar, and instead using onClickListener of the listView to deal with the click events on the listView row.

    0 讨论(0)
  • 2020-12-25 14:32
    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);
            }
        });             
    
    0 讨论(0)
  • 2020-12-25 14:38

    I've hacked around this problem by wrapping the RatingBar in a LinearLayout and attaching an onClick listener to the LinearLayout. I don't like doing this, but it works. For some mysterious reason onClick will not execute for a RatingBar.

    0 讨论(0)
  • 2020-12-25 14:47

    Andrew, you can just try to use another event available for RatingBar, OnRatingBarChangeListener. See this example, it works for me!

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

    So you don't need to use the onClickEvent but you have the same effect, but only work, off course, if you change actually the value. (If you click on the actual value, if has no effect)

    0 讨论(0)
提交回复
热议问题