如何获取多个膨胀视图的ID?

时间:2019-06-08 08:03:21

标签: java android android-layout gradle android-adapter

使用膨胀视图,我正在添加和删除按钮。 我正在获取多个视图。onclick按钮仅获取最后一个视图ID。我需要循环获取所有视图ID,但是我无法获取多个添加视图的ID。如何获取多个膨胀视图的ID。 / p>

addBuildingSuper.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                final View addView = inflater.inflate(R.layout.add_edittext, null);
                final FrameLayout frame = addView.findViewById(R.id.frame);
                  btnCustomSuper = addView.findViewById(R.id.btnCustomSuper);
                ImageView remove = addView.findViewById(R.id.remove);

                remove.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ((LinearLayout) addView.getParent()).removeView(addView);
                    }
                });
                customSupersLayout = new LinearLayout(getApplicationContext());
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                customSupersLayout.setLayoutParams(layoutParams);
                framer.addView(customSupersLayout);
                customSupersLayout.setVisibility(View.GONE);
                customSupersLayout.setClickable(true);
                View child = getLayoutInflater().inflate(R.layout.supers_dropdown, null);
                customSupersLayout.addView(child);
                LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                child.setLayoutParams(layoutParams2);
                listSupers = child.findViewById(R.id.listSupers);

                LinearLayout supers_dropdown=child.findViewById(R.id.spin_super);
                supers_dropdown.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        customSupersLayout.setVisibility(View.GONE);
                    }
                });
                btnCustomSuper.setOnClickListener(new View.OnClickListener() {
                    @Override

                    public void onClick(View v) {
                        if (customSupersLayout.getVisibility()==View.GONE){
                            btnCustomSuper = (Button) v;


                            try {
                                for (int i=0;i<jsonArray1.length();i++){
                                    JSONObject jsonObject = jsonArray1.getJSONObject(i);
                                    Log.i("supersadd",jsonObject.getString("name"));
                                    msupers.add(jsonObject.getString("name"));
                                    sArrayAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.text_dropdown, msupers);
                                    synchronized (sArrayAdapter){
                                        sArrayAdapter.notifyDataSetChanged();
                                        //sArrayAdapter.notifyAll();
                                        listSupers.invalidate();
                                    }

                                    listSupers.setAdapter(sArrayAdapter);
                                }
                            }catch (Exception e){
                                e.printStackTrace();
                            }



                            customSupersLayout.setVisibility(View.VISIBLE);
                        }else {
                            customSupersLayout.setVisibility(View.GONE);
                        }


                    }
                });

                listSupers.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {



                          listSupers = (ListView)parent;

                        synchronized (sArrayAdapter){
                            sArrayAdapter.notifyDataSetChanged();
                              sArrayAdapter.notifyAll();
                            listSupers.invalidate();
                        }


                        btnCustomSuper.setText(msupers.get(position));
                        listSupers.setTag(position);
                        JSONObject json = null;
                        try {
                            json = jsonArray1.getJSONObject(position);
                            getSuperId=json.getString("id");
                            superId.add(getSuperId);
                            Log.i("msg","btnCustomSupersID"+superId);
                             jsonObject = jsonArray1.getJSONObject(position);

                               Log.i("msg","listID"+listID);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        Log.i("msg","btnCustomSupers"+btnCustomSuper.getText().toString());
                            customSupersLayout.setVisibility(View.GONE);




                    }
                });


                layout.addView(addView);
            }
        });

这是膨胀视图的代码

1 个答案:

答案 0 :(得分:1)

在添加setTag的同时尝试对View使用rootView

addView.setTag(counter);
rootView.addView(addView);

并使用相同的标签获取View

View addView = rootView.findViewWithTag(counter)

所有视图的计数器都不能相同。

编辑

仅在获取后删除ImageView:

counter++;
ImageView remove = addView.findViewById(R.id.remove);
remove.setTag("r"+counter);
remove.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String tag = v.getTag();
                    View viewToRemove = layout.findViewWithTag(tag.replace("r",""));
                    layout.removeView(viewToRemove);
                }
            });

在将addView添加到布局之前,请执行以下操作:

addView.setTag(counter);
layout.addView(addView);