添加和删​​除多个视图

时间:2015-03-01 18:09:15

标签: android views

目前我正在尝试将同一视图的多个实例添加到视图组,我想让我的用户能够在必要时删除这些添加的视图,但我不确定如何单独引用每个视图。

如何分别跟踪每个视图,如有必要,请参考每个视图以从编辑文本/微调器中检索单个数据?

onCreateView()

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    view = inflater.inflate(R.layout.fragment_create, container, false);
    initValues();
    return view;
}

在init()

这就是我尝试使用将要添加的视图以及我如何获得对按钮的访问权限。

    inflater = getActivity().getLayoutInflater();
    singleWorkout = inflater.inflate(R.layout.single_workout_layout, workoutLayout, false);
    btnRemoveWorkout = (Button) singleWorkout.findViewById(R.id.btnRemoveWorkout);
    btnRemoveWorkout.setOnClickListener(this);

的OnClick()

这是我以编程方式添加视图的方式。

 inflater = getActivity().getLayoutInflater();
 singleWorkout = inflater.inflate(R.layout.single_workout_layout, workoutLayout, false);
 workoutLayout.addView(singleWorkout);


 spinnerWorkoutGroup = (Spinner) singleWorkout.findViewById(R.id.spinnerWorkoutGroup);
 array = getActivity().getResources().getStringArray(R.array.string_array_muscle_groups);
 TextSpinnerAdapter adapter2 = new TextSpinnerAdapter(getActivity(), R.layout.spinner_text_item, array);
            // Specify the layout to use when the list of choices appears
            adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            // Apply the adapter to the spinner
            spinnerWorkoutGroup.setAdapter(adapter2);

OnClick()removeWorkout Button

这根本不起作用。

 case R.id.btnRemoveWorkout:
                Log.e("BTN PRESSED", "BTN PRESSED");
                inflater = getActivity().getLayoutInflater();
                singleWorkout = inflater.inflate(R.layout.single_workout_layout, workoutLayout, false);
                workoutLayout.removeView(singleWorkout);
                break;

1 个答案:

答案 0 :(得分:0)

你需要的是ListView&适配器模式,您可以添加,删除适配器和项目的项目需要时致电yourAdapter.notifyDataSetChanged();。它会刷新你自己的观点。