此处我的布局数量夸大,但当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;
}
}
答案 0 :(得分:1)
您需要将视图回收考虑在内。 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)