我有一个自定义的BaseAdapter,该行中有一些视图
主要是一个复选框(ImageView)和按钮(ImageView)和隐藏文本(TextView)
每当我点击复选框时,我想改变它的绘图,它似乎有效,但只有当我notifyDataSetChanged
时 - 哪个IMO无关紧要。如果我删除该行,它有时会工作,有时不起作用
更重要的部分是我希望按钮显示/隐藏holder.expansion
但它不起作用。无论v.getTag()
我还尝试使用holder.expansion
,但它也无效。
我怎样才能让它发挥作用?要更改复选框drawable和显示/隐藏扩展文本吗?
@Override
public View getView(int position, View view, ViewGroup parent)
{
final DrugHolder holder;
if (view == null)
{
holder = new DrugHolder();
view = inflater.inflate(R.layout.drug_list_row, null);
holder.name = (TextView) view.findViewById(R.id.drug_list_name);
holder.checkbox = (ImageView) view.findViewById(R.id.drug_list_checkbox);
holder.arrow = (LinearLayout) view.findViewById(R.id.drug_list_arrow_button);
holder.expansion = (LinearLayout) view.findViewById(R.id.drug_list_expansion);
holder.fullName = (TextView) view.findViewById(R.id.drug_list_full_name);
holder.quantity = (TextView) view.findViewById(R.id.drug_list_quantity);
holder.unitDose = (TextView) view.findViewById(R.id.drug_list_unit_dose);
view.setTag(holder);
}
else
{
holder = (DrugHolder) view.getTag();
}
int colorPos = position % colors.length;
view.setBackgroundColor(colors[colorPos]);
Drug drug = allItems.get(position);
holder.name.setText(drug.Name);
holder.fullName.setText(drug.Name);
holder.quantity.setText(String.format(Locale.GERMANY, "%.2f", drug.Quantity));
holder.unitDose.setText(drug.DoseType);
holder.arrow.setTag(holder.expansion);
holder.checkbox.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
ImageView view = (ImageView) v;
if (view.isSelected())
{
view.setSelected(false);
view.setImageDrawable(context.getDrawable(R.drawable.ic_icon_checkbox_unchecked));
}
else
{
view.setSelected(true);
view.setImageDrawable(context.getDrawable(R.drawable.ic_icon_checkbox_checked));
}
notifyDataSetChanged();
}
});
holder.arrow.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
LinearLayout view = (LinearLayout) v.getTag();
if (view.getVisibility() == View.VISIBLE)
{
view.setVisibility(View.GONE);
}
else
{
view.setVisibility(View.VISIBLE);
}
notifyDataSetChanged();
}
});
return view;
}
根据改变的提议我做了这个:
if (drug.isChecked()) //Here breakpoint - enters only when entering activity
{
holder.checkbox.setImageDrawable(context.getDrawable(R.drawable.ic_icon_checkbox_checked));
}
else
{
holder.checkbox.setImageDrawable(context.getDrawable(R.drawable.ic_icon_checkbox_unchecked));
}
if (drug.isExpanded())
{
holder.fullName.setVisibility(View.VISIBLE);
}
else
{
holder.fullName.setVisibility(View.GONE);
}
holder.checkbox.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
drug.setChecked(!drug.isChecked()); // Breakpoint - enters when clicks
notifyDataSetChanged(); // After F8 goes to this line but never goes to getView again
}
});
holder.arrow.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
drug.setExpanded(!drug.isExpanded()); // Breakpoint - enters when click
notifyDataSetChanged(); // Never goes to this line after F8 - instead it jumps to View.java and shows red block with 'Source code does not match byte code
}
});
答案 0 :(得分:2)
您需要在Drug中保存有关已检查状态的信息,并在将数据设置为视图时检查此状态。例如
if (drug.isChecked()) {
holder.checkbox.setImageDrawable(context.getDrawable(R.drawable.ic_icon_checkbox_checked));
} else {
holder.checkbox.setImageDrawable(context.getDrawable(R.drawable.ic_icon_checkbox_unchecked));
}