[开发日记-SHUCheck]RecyclerView使用记录

需求:

  1. ListView滑动效果
  2. 控件包涵:TextView、CheckBox

    实现:

mainView.java

public class MainView extends AppCompatActivity {

    private RecyclerView root;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        root = new RecyclerView(this);
        setContentView(root);

        root.setLayoutManager(new LinearLayoutManager(this));

        root.setAdapter(new AdapterNameList());
    }

ViewHolder

public class ViewHolder extends RecyclerView.ViewHolder {

    private View myView;

    //Widgets集合
    public TextView textViewName;
    public CheckBox checkBoxOk;
    public CheckBox checkBoxNotOk;
    public CheckBox checkBoxLeave;
    public boolean checkBoxChange = false;

    public ViewHolder(View myView) {
        super(myView);

        //控件实例化
        textViewName = (TextView) myView.findViewById(R.id.textViewName);
        checkBoxOk = (CheckBox) myView.findViewById(R.id.checkboxOk);
        checkBoxNotOk = (CheckBox) myView.findViewById(R.id.checkboxNotOkOk);
        checkBoxLeave = (CheckBox) myView.findViewById(R.id.checkboxLeave);
    }
}

AdapterNameList

public class AdapterNameList extends RecyclerView.Adapter implements CompoundButton.OnCheckedChangeListener {

    //存放ViewHolde
    List<ViewHolder> arrayViewHolder = new ArrayList<ViewHolder>();

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //使用资源文件自定义列表
        return new ViewHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.activity_main_name_list_view, null));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        //对布局控件进行操作
        ViewHolder viewHolder = (ViewHolder) holder;
        if (viewHolder.checkBoxChange != true){
            viewHolder.textViewName.setText(String.format("Name" + position));

            viewHolder.checkBoxOk.setId(position);
            viewHolder.checkBoxOk.setChecked(false);
            viewHolder.checkBoxOk.setOnCheckedChangeListener(this);

            viewHolder.checkBoxNotOk.setId(position);
            viewHolder.checkBoxNotOk.setChecked(false);
            viewHolder.checkBoxNotOk.setOnCheckedChangeListener(this);

            viewHolder.checkBoxLeave.setId(position);
            viewHolder.checkBoxLeave.setChecked(false);
            viewHolder.checkBoxLeave.setOnCheckedChangeListener(this);

            arrayViewHolder.add(viewHolder);
        }

    }

    @Override
    public int getItemCount() {
        //渲染控件的个数
        return 50;
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        //判断哪一个复选框被选中

        arrayViewHolder.get(buttonView.getId()).checkBoxChange = true;
        if (buttonView.getText().equals("签到")) {
            Log.w("TEST", String.format("buttonView ID = " + buttonView.getId()));
        }
    }
}

步骤分析以后写,直接说结果

本想通过XML自定义一个列表项,然后就用RecyclerView来实现,但是发现使用RecycleerView时Item被回收后重新填充进列表时,CheckBox还是属于被回收的Item,也就是说第一个Item的CheckBox被选中后,第N个Item(第一个回收后重新填充进列表)的CheckBox同样是被选中的。

你可能感兴趣的:(ListView,checkbox,Review)