You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在RecyclerView列表行内动态添加对应每组训练次数的CheckBox

修复方案

核心问题点

  • 动态生成的CheckBox未调用addView方法挂载到对应组的LinearLayout容器中,导致控件没有显示在组行内
  • 未处理RecyclerView复用逻辑,后续滑动列表会出现CheckBox重复累加的异常
  • 存在冗余无用变量,CheckBox点击监听的强转逻辑存在崩溃风险

修正后代码

package com.example.workouttracker;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class WorkoutInterfacesAdapter extends RecyclerView.Adapter<WorkoutInterfacesAdapter.WorkoutInterfaceViewHolder> {

    Context mContext;
    private ArrayList<WorkoutInterfaceClass> sets;

    public WorkoutInterfacesAdapter(Context mContext , ArrayList<WorkoutInterfaceClass> sets) {
        this.sets = sets;
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public WorkoutInterfaceViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.workout_interface_item,parent,false);
        return new WorkoutInterfaceViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull WorkoutInterfaceViewHolder holder, int position) {
        WorkoutInterfaceClass currentSet = sets.get(position);
        holder.bind(currentSet);

        holder.itemView.setOnClickListener(v -> {
            Toast.makeText(mContext,"Set number: " + currentSet.numberOfSets + " ,Rep Number: "+ currentSet.numberOfReps,Toast.LENGTH_LONG).show();
            Intent intent = new Intent(mContext,WorkoutInterface.class);
            intent.putExtra("workout_sets", currentSet.numberOfSets);
            intent.putExtra("workout_reps", currentSet.numberOfReps);
            mContext.startActivity(intent);
        });
    }

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

    static class WorkoutInterfaceViewHolder extends RecyclerView.ViewHolder{
        TextView numberOfSets;
        LinearLayout linearLayout;

        public WorkoutInterfaceViewHolder(@NonNull View itemView) {
            super(itemView);
            numberOfSets = itemView.findViewById(R.id.edit_text_number_of_sets_item);
            linearLayout = itemView.findViewById(R.id.ll_checkbox_mother);
            linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        }

        public void bind(WorkoutInterfaceClass sets) {
            // 先清空旧的CheckBox,避免复用后重复
            linearLayout.removeAllViews();
            numberOfSets.setText("Set number:" + sets.numberOfSets);
            int numberOfReps = sets.numberOfReps;
            for (int i = 1; i <= numberOfReps; i++) {
                CheckBox checkBox = new CheckBox(linearLayout.getContext());
                checkBox.setId(View.generateViewId());
                checkBox.setText("Rep: "+ i);
                // 这里按你自己的需求写点击逻辑即可
                checkBox.setOnClickListener(v -> {
                    // 自定义CheckBox点击处理逻辑
                });
                // 核心修改:将生成的CheckBox添加到当前组的容器中
                linearLayout.addView(checkBox);
            }
        }
    }
}

补充优化建议

如果每组次数过多导致一行放不下,可以把workout_interface_item布局里的ll_checkbox_mother替换为FlexboxLayout,实现自动换行效果,更符合使用习惯。

内容的提问来源于stack exchange,提问作者levi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 20:24:04