0
我有一個自定義BaseAdapter,該行中有一些視圖。
主要是一個複選框(ImageView)和按鈕(ImageView)和隱藏文本(TextView)。
每當我點擊複選框,我想改變它的可繪製,它似乎工作,但只有當我notifyDataSetChanged
- 哪些IMO應該不重要。如果我刪除這條線,那麼它有時會起作用,有時不起作用。
更重要的部分是我希望按鈕顯示/隱藏holder.expansion
但它不起作用。無論v.getTag()
我也試圖使用holder.expansion
,但它也沒有工作。OnClickListener不會更改自定義BaseAdapter中的視圖
我怎樣才能使它工作?要更改複選框可繪製並顯示/隱藏擴展文本?
@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
}
});
是的,應該插入此項以設置ListView項目中的CheckBox圖像。要在單擊後設置CheckBox圖像,應將其插入onClick方法中並更改爲:drug.setChecked(!drug.isChecked()),然後再轉到上面的代碼。 – Alexandre
同樣用於顯示擴展?在這種情況下,我應該運行notifyDataSetChanged(),對吧? –
在監聽器中,您將setCheck在藥物中並調用notifyDataSetChanged –