onClickListener如何跟踪要删除的视图

时间:2015-11-27 09:53:32

标签: java android onclicklistener

我正在关注教程here - 现在我没有添加textView,而是替换了以下方法:

public void addView(LinearLayout container, String[] spin_array, String hint, int inputType, int tagger) {

        LayoutInflater layoutInflater = (LayoutInflater) getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        final View addView = layoutInflater.inflate(R.layout.add_company_fragment_two_dynamic_content, null);

        final Spinner spin_dynamic = (Spinner) addView.findViewById(R.id.email_spinner1);
        EditText edt_dynamic = (EditText) addView.findViewById(R.id.edittext_email1);
        ImageView remove_dynamic = (ImageView) addView.findViewById(R.id.btn_remove);


        edt_dynamic.setHint(hint);
        setUpSpinners(spin_array, spin_dynamic);
        edt_dynamic.setTag(container.getTag() + "edt" + tagger);
        edt_dynamic.setInputType(inputType);
        edt_dynamic.setTypeface(roboto_condenced_light);
        spin_dynamic.setTag(container.getTag() + "spin" + tagger);

        idsMap.put(spin_dynamic.getTag().toString(), edt_dynamic.getTag().toString());

        // get height from dimens
        int height = (int) getResources().getDimension(R.dimen.lin_height);
        // set this height
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, height);

        // we are only concerned about top margin here.
        layoutParams.setMargins(0, (int) getResources().getDimension(R.dimen.topMargin), 0, 0);
        container.addView(addView, 1, layoutParams);

        remove_dynamic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Remove View (LinearLayout) and also remove the key from idsMap so that we dont get a NullPointer later
                ((LinearLayout) addView.getParent()).removeView(addView);
                idsMap.remove(spin_dynamic.getTag().toString());
            }
        });
    }

这里的问题是我可以调用上面的函数多次,在LinearLayout中插入一个视图行。

当我们尝试从容器中删除视图时,我无法理解addView变量的跟踪方式:

remove_dynamic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //Remove View (LinearLayout) and also remove the key from idsMap so that we dont get a NullPointer later
                    ((LinearLayout) addView.getParent()).removeView(addView);
                    idsMap.remove(spin_dynamic.getTag().toString());
                }
            });

onClickListener如何知道要在此处删除的addView实例,如果我插入四个视图,那么onClick如何确切知道要删除哪一个?

没有错误,一切正常。但为什么它有效?

以下是表示此行为的图像:

enter image description here

2 个答案:

答案 0 :(得分:1)

在删除视图时必须使用相同的viewobject,例如在ViewGroup中添加了三个视图

PrintWriter writer = new PrintWriter(
    new OutputStreamWriter(fileOutputStream, OUTPUT_ENCODING)
);

...

writer.println(content);

所以当你想删除拳头视图,即addView1时,你必须使用该对象addView1 addView2 addView3

答案 1 :(得分:0)

由于这条线而正在工作

((LinearLayout) addView.getParent()).removeView(addView);

onClickListener()无法跟踪任何内容。