radiogroup处理回收器视图适配器内的radiobutton选择

时间:2015-10-19 19:07:17

标签: android

我正在尝试RecyclerView,其中每个recyclerItem都有3个单选按钮。一旦我选择了其中一个,我想获得与单选按钮选择一致的对象。我的适配器在下面...当我点击radiobutton没有任何反应。似乎永远不会传递onCheckedChanged()方法......我做错了什么?我如何处理radioLike.OnCheckedChangeListener?或方法onBindViewHolder缺少什么?

public class LikeListAdapter extends RecyclerView.Adapter<LikeListAdapter.LikeItemViewHolder> {

    private List<Goals> goalsList;

    public LikeListAdapter(List<Goals> goalsList) {
        this.goalsList = goalsList;
    }

    @Override
    public LikeItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.recycler_item_like, parent, false);
        return new LikeItemViewHolder(view);
    }


    @Override
    public void onBindViewHolder(LikeItemViewHolder holder, int position) {
        Goals goals = goalsList.get(position);
        if (goals != null && getItemCount() > 0) {
            holder.goalsDescriptionTextView.setText(goals.getDescription());
        }

    }

    @Override
    public int getItemCount() {
        return goalsList.size();
    }




    public class LikeItemViewHolder extends RecyclerView.ViewHolder implements RadioGroup.OnCheckedChangeListener {
        @Bind(R.id.description_goalsTextView)
        TextView goalsDescriptionTextView;
        @Bind(R.id.happy_radio)
        RadioButton happyRadioButton;
        @Bind(R.id.soso_radio)
        RadioButton sosoRadioButton;
        @Bind(R.id.angry_radio)
        RadioButton angryRadioButton;
        @Bind(R.id.radio_like)
        RadioGroup radioLike;


        public LikeItemViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            radioLike.setOnCheckedChangeListener(this); ???

        }


        @Override 
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            Log.e("group","" + group);
            Log.e("checkedId","" + checkedId);
            switch (group.getId()) {
                case (R.id.happy_radio):
                    Log.e("aqui", "happy");
                    break;
                case (R.id.soso_radio):
                    Log.e("aqui", "soso");
                    break;
                case (R.id.angry_radio):
                    Log.e("aqui", "angry");
                    break;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我想你想要哪个单选按钮

 public class LikeItemViewHolder extends RecyclerView.ViewHolder implements CompoundButton.OnCheckedChangeListener {
.......
  happyRadioButton.setOnCheckedChangeListener(this);
        sosoRadioButton.setOnCheckedChangeListener(this);
        angryRadioButton.setOnCheckedChangeListener(this);
  @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case (R.id.happy_radio):
                Log.e("aqui", "happy");
                break;
            case (R.id.soso_radio):
                Log.e("aqui", "soso");
                break;
            case (R.id.angry_radio):
                Log.e("aqui", "angry");
                break;
        }
    }