我有一个类PinnedListView
,其子类LinearLayout
。我有另一个类NonDiarisedPinnedListView
,其子类PinnedListView
。
大约90%的功能是相同的,只是布局和NonDiarisedPinnedListView
上的一些额外位。
但是当我加载它时,加载PinnedListView
的布局而不是我的NonDiarisedPinnedListView
。任何特殊的原因,为什么我可以绕过它?这是相关的代码:
PinnedListView
构造函数:
public PinnedListView(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.pinned_list_view, this);
final ListView listView = (ListView) findViewById(R.id.list);
final ImageView arrowButton = (ImageView) findViewById(R.id.arrowButton);
arrowButton.setSelected(true);
final RelativeLayout header = (RelativeLayout) findViewById(R.id.header);
header.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (!arrowButton.isSelected()) {
arrowButton
.setImageResource(android.R.drawable.arrow_down_float);
listView.setVisibility(View.VISIBLE);
arrowButton.setSelected(true);
} else {
arrowButton
.setImageResource(android.R.drawable.arrow_up_float);
listView.setVisibility(View.GONE);
arrowButton.setSelected(false);
}
}
});
}
NonDiarisedPinnedListView
构造函数:
public NonDiarisedPinnedListView(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.non_diarised_pinned_list_view, this);
}
如您所见,除了新布局外,后者是空的。它取出了一些东西(在前者的构造函数中没有引用)并且它添加了一些在其他地方使用的东西。
干杯!
答案 0 :(得分:1)
原因是你打电话的超级。我建议重载PinnedListView
类的构造函数,并允许它采用布局int
。见下面的例子
public PinnedListView(Context context, AttributeSet attrs) {
this(R.layout.pinned_list_view);
}
public PinnedListView(Context context, AttributeSet attrs, int layout) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(layout, this);
final ListView listView = (ListView) findViewById(R.id.list);
final ImageView arrowButton = (ImageView) findViewById(R.id.arrowButton);
arrowButton.setSelected(true);
final RelativeLayout header = (RelativeLayout) findViewById(R.id.header);
header.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (!arrowButton.isSelected()) {
arrowButton
.setImageResource(android.R.drawable.arrow_down_float);
listView.setVisibility(View.VISIBLE);
arrowButton.setSelected(true);
} else {
arrowButton
.setImageResource(android.R.drawable.arrow_up_float);
listView.setVisibility(View.GONE);
arrowButton.setSelected(false);
}
}
});
}
public NonDiarisedPinnedListView(Context context, AttributeSet attrs) {
super(context, attrs, R.layout.non_diarised_pinned_list_view);
}