我有一个与 common 的自定义适配器创建的视图生命周期有关的问题ListView
。我创建了一个自定义视图,如下所示:
public class NSViewZoneLarge extends RelativeLayout
{
private long mZoneId;
private ToggleButton mOnOffButton;
public NSViewZoneLarge(Context context, boolean showSwitch, long zoneId)
{
super(context);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(R.layout.view_zone_large, this);
mOnOffButton = (ToggleButton) findViewById(R.id.toggleButtonOnOff);
ImageView imgView = (ImageView)findViewById(R.id.imageViewZoneType);
imgView.setFocusable(false);
if (showSwitch)
{
mOnOffButton.setVisibility(View.VISIBLE);
}
else
{
mOnOffButton.setVisibility(View.INVISIBLE);
mOnOffButton.setEnabled(false);
mOnOffButton.setFocusable(false);
mOnOffButton.setFocusableInTouchMode(false);
}
mZoneId = zoneId;
}
// ---------------------------------------------------------------------------------------
public NSViewZoneLarge(Context context, AttributeSet attrs)
{
super(context, attrs);
}
// ---------------------------------------------------------------------------------------
public void setZoneName(String zoneName)
{
TextView tVZoneName = (TextView) findViewById(R.id.textViewZoneName);
tVZoneName.setText(zoneName);
}
// ---------------------------------------------------------------------------------------
public String getZoneName()
{
TextView tVZoneName = (TextView) findViewById(R.id.textViewZoneName);
return tVZoneName.getText().toString();
}
// ---------------------------------------------------------------------------------------
public void setOnOffButton(boolean isOn)
{
ToggleButton onOffButton = (ToggleButton) findViewById(R.id.toggleButtonOnOff);
onOffButton.setChecked(isOn);
}
// ---------------------------------------------------------------------------------------
public boolean isOnOffButtonChecked()
{
ToggleButton onOffButton = (ToggleButton) findViewById(R.id.toggleButtonOnOff);
return onOffButton.isChecked();
}
// ---------------------------------------------------------------------------------------
public long getZoneId()
{
return mZoneId;
}
}
getView()
然后我在我的子类的方法中实例化这个视图BaseAdapter
:
// ---------------------------------------------------------------------------------------
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
NSDestinationContent destinationContent = mDestinationContents.get(position);
NSViewZoneLarge viewZone = new NSViewZoneLarge(mContext, mShowSwitches, destinationContent.mDestinationContentId);
viewZone.setZoneName(destinationContent.mName);
if (mShowSwitches)
viewZone.setOnOffButton(mOnOffStates.get(position));
if (destinationContent.mDestinationTypeId.equals(DestinationTypeId.SPEAKERS_GROUP))
{
NSSpeakerGroup speakerGroup = (NSSpeakerGroup) destinationContent;
viewZone.setZoneType(speakerGroup.mZoneTypeId);
}
return viewZone;
}
最后,我设置OnItemLongClickListener
为listView
:
mZonesListView.setOnItemLongClickListener(new OnItemLongClickListener()
{
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)
{
NSViewZoneLarge viewZoneLarge = (NSViewZoneLarge)view;
long destinationId = viewZoneLarge.getZoneId();
ClipData data = ClipData.newPlainText(NSConstants.DRAG_KEY_ZONE_ID, String.valueOf(destinationId));
view.startDrag(data, new DragShadowBuilder(view), null, 0);
Intent intent = new TrashBinIntent(true);
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(ApplicationNetSpeaker.getInstance());
localBroadcastManager.sendBroadcast(intent);
return true;
}
});
问题是尽管我通过方法中的构造函数正确getZoneId()
设置了该值,但该方法始终返回 null 。NSViewZoneLarge
getView()
我知道为什么它返回 null;这是因为给了我一个不是由方法创建OnItemLongClickListener
的实例。在某处,不知何故,实例是使用第二个 - 仅默认构造函数创建的。NSViewZoneLarge
getView()
NSViewZoneLarge
我还尝试使用setTag()
方法将数据与视图相关联,但该getTag()
方法总是返回 null ——同样,原始实例在某个地方丢失了,可能由 GC 释放,而我得到了一个没有设置标签的新实例。
有人可以解释一下为什么会发生这种情况,以及如何将任何类型的数据与ListView
项目的特定视图相关联?