Recyclerview中的评级栏不起作用

时间:2016-04-19 07:51:47

标签: android android-recyclerview ratingbar

我有Recyclerview在list_items

中保留ratingbar

如果我使用 rtBar.setOnRatingBarChangeListener ,那么我得不到结果,评级栏中没有任何反应,甚至无法点击

如果我使用 rtBar.setOnTouchListener ,它可以正常工作,但点击第4次启动时我的代码不起作用

public EmployeeViewHolder(View itemView) {
    super(itemView);
    ButterKnife.bind(this, itemView);
    rtBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
        @Override
        public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
            utilMethods.showToast(mContext, "" + rating);
        }
    });
}

2 个答案:

答案 0 :(得分:3)

如果您的RatingBar无法点击,请检查您的xml文件,其中RatingBar具有属性()

  

如果 isIndicator 为真,那么您无法评分

答案 1 :(得分:0)

使用那样的界面

 public interface OnRecordEventListener  {
       void onRatingBarChange(Record item,float value);
 }

您将ViewHolder传递侦听器创建为这样的构造函数。

 public ViewHolder(View itemView, OnRecordEventListener listener) {
          super(itemView);
          textViewTitle = (TextView)     itemView.findViewById(R.id.text_view_title);
          ratingBar = (RatingBar)     itemView.findViewById(R.id.rating_bar_rating);
          bar.setOnRatingBarChangeListener(new     RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating,   boolean fromUser) {
            if (fromUser) {
              listener.onRatingBarChange(mRecords.get(getLayoutPosition()),   rating);
            }
           }
          });
         }

并在onCreateViewHolder中更新

@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
   View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recyclerview_item, viewGroup, false);
            return new ViewHolder(v,mClickListener);
        }

你也可以使用其他事件的接口,你只需要在接口中添加方法,实现它并在ViewHolder构造函数中绑定。

UPDATE 考虑您在Activity

中使用 RecyclerView
public class RecyclerActivity extends Activity implements OnRecordEventListener {
    // all institutionalization, callbacks, hook methods
    @Override
    public void onCreate() {
      // Initialization .....
      mRecyclerView.setLayoutManager(...);
      // Passing this because this class implements interface
      mRecyclerView.setAdapter(new RecyclerViewAdapter(mRecord,this));
    }
    @Override
    void onRatingBarChange(Record item,float value) {
      // do Something
    }
 }