我使用ArrayAdapter创建了一个自定义ListView。 ListView的行有两个视图,一个是主视图&其他是隐藏的&单击主视图上的TextView时出现。
为了显示行数据,我创建了一个Object。我在ListView中遇到了一些问题。
我在对象中使用了一个标记来隐藏/显示隐藏的视图。但有时它看起来并不好。检查图片2
有时隐藏隐藏视图后,它仍显示在主视图下方。检查图片3
请帮帮我。
class ShippingAdapter extends ArrayAdapter<ShippingOption>
{
ShippingAdapter(Context context, ArrayList<ShippingOption> list)
{
super(context, R.layout.row_shipping, R.id.row_shipping_tv_product_name, list);
}
public View getView(final int position, View convertView, ViewGroup parent)
{
View row = super.getView(position, convertView, parent);
ShippingViewHolder holder = (ShippingViewHolder) row.getTag();
if (holder == null)
{
holder = new ShippingViewHolder(row);
row.setTag(holder);
}
final ShippingOption shippingOption = getShippingOption(position);
holder.tvRemove.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
showRemoveDialog(shippingOption.getProductId(), position);
}
});
holder.tvProductName.setText(shippingOption.getProductName());
imageLoader.DisplayImage(shippingOption.getProductImageUrl(), holder.ivProduct);
ArrayAdapter<String> adapterShippingOption = createAdapter(holder.spinnerShippingOption, shippingOption.getListShipping(), R.layout.spinner_textview_blue);
holder.spinnerShippingOption.setSelection(adapterShippingOption.getPosition(shippingOption.getSelectedOption()));
if (shippingOption.getSelectedOption().equals(Constants.PICK_UP_AT_STORE))
{
HashMap<String, Integer> mapPickupAddress = shippingOption.getMapPickupAddress();
Collection<String> collection = mapPickupAddress.keySet();
ArrayList<String> listAddress = new ArrayList<String>();
listAddress.addAll(collection);
ArrayAdapter<String> adapterAddress = createAdapter(holder.spinnerAddress, listAddress, R.layout.spinner_textview_blue_small);
holder.spinnerAddress.setSelection(adapterAddress.getPosition(shippingOption.getSelectedAddress()));
holder.relativeAddress.setVisibility(View.VISIBLE);
holder.spinnerAddress.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
String selectedAddress = parent.getItemAtPosition(position).toString();
String oldAddress = shippingOption.getSelectedAddress();
shippingOption.setSelectedAddress(selectedAddress);
if (oldAddress == null || !oldAddress.equals(selectedAddress))
notifyDataSetChanged();
}
@Override
public void onNothingSelected(AdapterView<?> arg0)
{
}
});
} else
{
holder.relativeAddress.setVisibility(View.GONE);
}
holder.spinnerShippingOption.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
String selectedOption = parent.getItemAtPosition(position).toString();
// if (!selectedOption.equals(Constants.FREE_SHIPPING))
// {
String oldOption = shippingOption.getSelectedOption();
shippingOption.setSelectedOption(selectedOption);
if (!oldOption.equals(selectedOption))
notifyDataSetChanged();
// }
}
@Override
public void onNothingSelected(AdapterView<?> parent)
{
}
});
holder.relativeLayoutGift.setVisibility(shippingOption.isGiftDetailsVisible() ? View.VISIBLE : View.GONE);
if (shippingOption.isGiftWrapAvailable())
{
imageLoader.DisplayImage(shippingOption.getGiftImageUrl(), holder.ivGift);
holder.layoutGift.setVisibility(View.VISIBLE);
ArrayList<String> listGift = new ArrayList<String>();
listGift.add("Not a Gift");
listGift.add("Yes it's a Gift $" + shippingOption.getGiftServiceCharge());
ArrayAdapter<String> adapterGift = createAdapter(holder.spinnerGift, listGift, R.layout.spinner_textview_blue);
holder.tvGiftDetails.setVisibility(shippingOption.getSelectedGiftOption().equals("Not a Gift") ? View.GONE : View.VISIBLE);
holder.spinnerGift.setSelection(adapterGift.getPosition(shippingOption.getSelectedGiftOption()));
holder.spinnerGift.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
String selectedOption = parent.getItemAtPosition(position).toString();
String oldOption = shippingOption.getSelectedGiftOption();
shippingOption.setSelectedGiftOption(selectedOption);
if (!oldOption.equals(selectedOption))
notifyDataSetChanged();
}
@Override
public void onNothingSelected(AdapterView<?> parent)
{
}
});
holder.tvGiftDetails.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
shippingOption.setGiftDetailsVisible(true);
notifyDataSetChanged();
}
});
holder.relativeGiftDetails.setVisibility(shippingOption.isGiftDetailsVisible() ? View.VISIBLE : View.GONE);
holder.relativeLayoutGift.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
}
});
holder.btnCancel.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
shippingOption.setGiftDetailsVisible(false);
notifyDataSetChanged();
}
});
holder.btnClose.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
shippingOption.setGiftDetailsVisible(false);
notifyDataSetChanged();
}
});
final EditText editLocal = holder.editGiftMessage;
holder.btnApply.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
String message = editLocal.getText().toString();
shippingOption.setGiftMessageApplied(true);
shippingOption.setGiftMessage(message);
shippingOption.setGiftDetailsVisible(false);
notifyDataSetChanged();
}
});
holder.editGiftMessage.setText(shippingOption.getGiftMessage());
holder.editGiftMessage.addTextChangedListener(new TextWatcher()
{
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
@Override
public void afterTextChanged(Editable s)
{
}
});
} else
{
holder.layoutGift.setVisibility(View.GONE);
holder.relativeGiftDetails.setVisibility(View.GONE);
}
return row;
}
}
答案 0 :(得分:1)
在这里,我将展示一个视图标记。
ShippingViewHolder holder;
if (holder == null)
{
holder = new ShippingViewHolder(row);
row.setTag(holder);
}
holder = (ShippingViewHolder) row.getTag();
ShippingOption shippingOption = getShippingOption(position);
holder.tvRemove.setTag(shippingOption);
holder.tvRemove.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
ShippingOption shippingOption1 =(ShippingOption) v.getTag();
int pos = getPosition(shippingOption1); // this is the function that will return you the postion of your object from your passed list of object; write it your self.
showRemoveDialog(shippingOption1.getProductId(), pos);
}
});