从列表视图中删除动态添加的布局扩展基本适配器

Remove Dynamically added layouts from listview extends base adapter

这里我有一些布局被夸大了,但是当 ei.duedate.size() 的长度是 0 时,视图不会被删除。

我搜索了很多,但不知道如何从 ListView 中删除视图。

我已经成功地膨胀了 5-6 个子布局,但是当尺寸变为 1-2 时,膨胀的视图无法移除。

如果你们有任何想法,请帮助我。

下面是我的 Adapter 代码:

   public class ImmunisationAdapter extends BaseAdapter {

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return ImmuMainArraylist.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return ImmuMainArraylist.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holder = null;
         final ImmuniModel ei = ImmuMainArraylist.get(position);
        if (view == null) {
            holder = new ViewHolder();
            view = LayoutInflater.from(getActivity()).inflate(R.layout.raw_main_immunisation, parent, false);
            holder.layout = (ViewGroup) view.findViewById(R.id.ll);
            holder.imageView = (ImageView) view.findViewById(R.id.optin);
            holder.txt_count = (TextViewPlus)view.findViewById(R.id.txt_circle);
            holder.mg_type = (ImageView) view.findViewById(R.id.mg_type);
            holder.txt_main = (TextViewPlus) view.findViewById(R.id.txt_main);
            holder.txt_new_elder = (TextViewPlus) view.findViewById(R.id.txt_new_elder);

            Log.e("SIZE OF CHILDIMMUNISATION",ei.duedate.size()+"");
            for (int i = 0; i < ei.duedate.size(); i++) {

                View Chiledview = LayoutInflater.from(getActivity()).inflate(R.layout.raw_child_immunisation, null);
                ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                CheckBox Jabchk = (CheckBox) Chiledview.findViewById(R.id.chk);
                TextViewPlus txt_date = (TextViewPlus) Chiledview.findViewById(R.id.txt_date);
                TextViewPlus txt_lbl = (TextViewPlus) Chiledview.findViewById(R.id.text_admin);
                Jabchk.setText("Jab " + (i+1));
                Chiledview.setTag(i);
                Chiledview.setLayoutParams(layoutParams);
                txt_date.setText(ei.duedate.get(i).due_on);

                if (ei.duedate.get(i).status.equals("Administered")) {
                    Jabchk.setChecked(true);

                    txt_lbl.setText("Administered on ");
                }else if (ei.duedate.get(i).status.equals("Planned")) {
                    Jabchk.setChecked(false);
                    txt_lbl.setText("Estimated due date  ");
                }
                holder.layout.addView(Chiledview, i);
                Chiledview.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub

                        int pos = (Integer) v.getTag();

                        Intent intent = new Intent(getActivity(), EditJabImmunisationActivity.class);
                        intent.putExtra("pos", (pos+1) + "");
                        intent.putExtra("status", ei.duedate.get(pos).status);
                        editor.putString(Utils.JABNAME, ei.Name);
                        editor.putString(Utils.JABID, ei.duedate.get(pos).id);
                        editor.putString(Utils.JABDATE, ei.duedate.get(pos).due_on);
                        editor.commit();
                        intent.putExtra("location", ei.duedate.get(pos).jab_location);
                        intent.putExtra("type", ei.duedate.get(pos).jab_type);
                        intent.putExtra("note", ei.duedate.get(pos).note);
                        intent.putExtra("doc", ei.duedate.get(pos).doc_name);
                        intent.putExtra("facility", ei.duedate.get(pos).facility);

                        startActivity(intent);
                        getActivity().overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
                    }
                });

            }

            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        ((ViewGroup)holder.layout.getParent()).removeView(holder.layout);
        holder.txt_main.setText(ImmuMainArraylist.get(position).Name);
        if (ImmuMainArraylist.get(position).category.equals("Drops")) {
            holder.mg_type.setImageResource(R.drawable.drop);
        }else if (ImmuMainArraylist.get(position).category.equals("Injection")) {
            holder.mg_type.setImageResource(R.drawable.syring);
        }else if (ImmuMainArraylist.get(position).category.contains("Pill")) {
            holder.mg_type.setImageResource(R.drawable.pill);
        }
        holder.imageView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                popupWindowDogs = popupwindow.popupWindowDogs(position);
                popupWindowDogs.showAsDropDown(v, -5, 0);
            }
        });
        holder.txt_new_elder.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Log.e("helof", ImmuMainArraylist.get(position).vaccin_id);
                try {
                    AddnewJab(ImmuMainArraylist.get(position).vaccin_id, token);
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        return view;
    }

}

您需要考虑视图回收。 holder.layout ViewGroup 中可能已经有子项,因此需要先将其删除。

@Override
public View getView(final int position, View view, ViewGroup parent) {
    // TODO Auto-generated method stub
    ViewHolder holder = null;
     final ImmuniModel ei = ImmuMainArraylist.get(position);
    if (view == null) {
        holder = new ViewHolder();
        view = LayoutInflater.from(getActivity()).inflate(R.layout.raw_main_immunisation, parent, false);
        holder.layout = (ViewGroup) view.findViewById(R.id.ll);
        holder.imageView = (ImageView) view.findViewById(R.id.optin);
        holder.txt_count = (TextViewPlus)view.findViewById(R.id.txt_circle);
        holder.mg_type = (ImageView) view.findViewById(R.id.mg_type);
        holder.txt_main = (TextViewPlus) view.findViewById(R.id.txt_main);
        holder.txt_new_elder = (TextViewPlus) view.findViewById(R.id.txt_new_elder);
        view.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
        holder.layout.removeAllViews();
    }

    Log.e("SIZE OF CHILDIMMUNISATION",ei.duedate.size()+"");
    for (int i = 0; i < ei.duedate.size(); i++) {

        View Chiledview = LayoutInflater.from(getActivity()).inflate(R.layout.raw_child_immunisation, null);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        CheckBox Jabchk = (CheckBox) Chiledview.findViewById(R.id.chk);
        TextViewPlus txt_date = (TextViewPlus) Chiledview.findViewById(R.id.txt_date);
        TextViewPlus txt_lbl = (TextViewPlus) Chiledview.findViewById(R.id.text_admin);
        Jabchk.setText("Jab " + (i+1));
        Chiledview.setTag(i);
        Chiledview.setLayoutParams(layoutParams);
        txt_date.setText(ei.duedate.get(i).due_on);

        if (ei.duedate.get(i).status.equals("Administered")) {
            Jabchk.setChecked(true);

            txt_lbl.setText("Administered on ");
        } else if (ei.duedate.get(i).status.equals("Planned")) {
            Jabchk.setChecked(false);
            txt_lbl.setText("Estimated due date  ");
        }
        holder.layout.addView(Chiledview, i);
        Chiledview.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                 int pos = (Integer) v.getTag();

                Intent intent = new Intent(getActivity(), EditJabImmunisationActivity.class);
                intent.putExtra("pos", (pos+1) + "");
                intent.putExtra("status", ei.duedate.get(pos).status);
                editor.putString(Utils.JABNAME, ei.Name);
                editor.putString(Utils.JABID, ei.duedate.get(pos).id);
                editor.putString(Utils.JABDATE, ei.duedate.get(pos).due_on);
                editor.commit();
                intent.putExtra("location", ei.duedate.get(pos).jab_location);
                intent.putExtra("type", ei.duedate.get(pos).jab_type);
                intent.putExtra("note", ei.duedate.get(pos).note);
                intent.putExtra("doc", ei.duedate.get(pos).doc_name);
                intent.putExtra("facility", ei.duedate.get(pos).facility);

                startActivity(intent);
                    getActivity().overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
            }
        });

    }

    holder.txt_main.setText(ImmuMainArraylist.get(position).Name);
    if (ImmuMainArraylist.get(position).category.equals("Drops")) {
        holder.mg_type.setImageResource(R.drawable.drop);
    }else if (ImmuMainArraylist.get(position).category.equals("Injection")) {
        holder.mg_type.setImageResource(R.drawable.syring);
    }else if (ImmuMainArraylist.get(position).category.contains("Pill")) {
        holder.mg_type.setImageResource(R.drawable.pill);
    }
    holder.imageView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            popupWindowDogs = popupwindow.popupWindowDogs(position);
            popupWindowDogs.showAsDropDown(v, -5, 0);
        }
    });
    holder.txt_new_elder.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Log.e("helof", ImmuMainArraylist.get(position).vaccin_id);
            try {
                AddnewJab(ImmuMainArraylist.get(position).vaccin_id, token);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });
    return view;
}

我刚刚向您展示了快速简便的方法。更好的方法是不调用 removeAllViews(),而是在循环 ei.duedate 时调用 getChildAt(i) 查看是否已经存在 raw_child_immunisation 视图并使用那而不是夸大新观点。您还必须使用 removeViews(start, count) 清除循环结束时任何残留的 raw_child_immunisation 视图。

以下是该策略的代码更改:

@Override
public View getView(final int position, View view, ViewGroup parent) {
    // TODO Auto-generated method stub
    ViewHolder holder = null;
    final ImmuniModel ei = ImmuMainArraylist.get(position);
    if (view == null) {
        holder = new ViewHolder();
        view = LayoutInflater.from(getActivity()).inflate(R.layout.raw_main_immunisation, parent, false);
        holder.layout = (ViewGroup) view.findViewById(R.id.ll);
        holder.imageView = (ImageView) view.findViewById(R.id.optin);
        holder.txt_count = (TextViewPlus)view.findViewById(R.id.txt_circle);
        holder.mg_type = (ImageView) view.findViewById(R.id.mg_type);
        holder.txt_main = (TextViewPlus) view.findViewById(R.id.txt_main);
        holder.txt_new_elder = (TextViewPlus) view.findViewById(R.id.txt_new_elder);
        view.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
        int childCount = holder.layout.getChildCount();
        if (childCount > ei.duedate.size()) {
            // remove any extra views
            holder.layout.removeViews(ei.duedate.size(), childCount - ei.duedate.size());
        }
    }

    Log.e("SIZE OF CHILDIMMUNISATION",ei.duedate.size()+"");
    for (int i = 0; i < ei.duedate.size(); i++) {

        // if a view already exists, use it
        View Chiledview = holder.layout.getChildAt(i);
        if (Chiledview == null) {
            // otherwise create a new view and add it to the view group
            Chiledview = LayoutInflater.from(getActivity()).inflate(R.layout.raw_child_immunisation, null);
            holder.layout.addView(Chiledview, i);
        }
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        CheckBox Jabchk = (CheckBox) Chiledview.findViewById(R.id.chk);
        TextViewPlus txt_date = (TextViewPlus) Chiledview.findViewById(R.id.txt_date);
        TextViewPlus txt_lbl = (TextViewPlus) Chiledview.findViewById(R.id.text_admin);
        Jabchk.setText("Jab " + (i+1));
        Chiledview.setTag(i);
        Chiledview.setLayoutParams(layoutParams);
        txt_date.setText(ei.duedate.get(i).due_on);

        if (ei.duedate.get(i).status.equals("Administered")) {
            Jabchk.setChecked(true);

            txt_lbl.setText("Administered on ");
        } else if (ei.duedate.get(i).status.equals("Planned")) {
            Jabchk.setChecked(false);
            txt_lbl.setText("Estimated due date  ");
        }
        Chiledview.setOnClickListener(new View.OnClickListener() {
        .
        .
        .
        (rest of code the same)