如何使用NumberPicker

时间:2016-10-11 18:49:51

标签: java android

我正在使用带有多个数字选择器的动态出现的回收器视图。我想要OnValueChange上每个数字选择器的值。

如何实现这一目标?

  

我的适配器类

public class MedicationCounterAdapter extends RecyclerView.Adapter<MedicationCounterAdapter.MyViewHolder> {

private List<MedicationCounter> medicationCounterList;
public Context context;

public class MyViewHolder extends RecyclerView.ViewHolder {
    public TextView medicationTitle;
    public TextView medicationType;
    public NumberPicker numberPickerNP;

    public MyViewHolder(View view) {
        super(view);
        medicationTitle = (TextView) view.findViewById(R.id.med_day_type_tv);
        medicationType = (TextView) view.findViewById(R.id.med_type_tv);
        numberPickerNP = (NumberPicker) view.findViewById(R.id.number_picker_medication);
    }
}


public MedicationCounterAdapter(List<MedicationCounter> medicationCounterList, Context iContext) {
    this.medicationCounterList = medicationCounterList;
    this.context = iContext;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.include_medication_counter_components, parent, false);

    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
    final MedicationCounter medicationCounter = medicationCounterList.get(position);
    holder.medicationTitle.setText(medicationCounter.getMedicationName());
    holder.medicationType.setText(medicationCounter.getMedicationDayType());
    //Set the minimum value of NumberPicker
    holder.numberPickerNP.setMinValue(1);
    //Specify the maximum value/number of NumberPicker
    holder.numberPickerNP.setMaxValue(10);
    //Gets whether the selector wheel wraps when reaching the min/max value.
    holder.numberPickerNP.setWrapSelectorWheel(true);
    //Set a value change listener for NumberPicker
    holder.numberPickerNP.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker numberPicker, int i, int i1) {
            Log.e("numberp", "old value" + i + "' " + "new val" + i1 + ", " + numberPicker);
        }
    });

}

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

}
  我的片段中的

和recycleler视图是

RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.medication_details_recycler_view);
mAdapter = new MedicationCounterAdapter(medicationCounterList, getContext());
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);

1 个答案:

答案 0 :(得分:0)

在MedicationCounter类中的

定义一个int memeber,并在onValueChange方法中为特定位置的i1赋值。