如何只给一个按钮充气一次?
How to inflate a button only one time?
我只想使用 inflater 动态创建按钮一次。我有三个按钮和一个加载功能。这个函数在每个按钮中调用,每次调用时,都会出现预建按钮。我只想在每次单击按钮时创建一个按钮。怎么样?
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
assert inflater != null;
LinearLayout root = (LinearLayout) inflater.inflate(R.layout.analyze_company_buttons_layout, null, false);
Button btnCompany = root.findViewById(R.id.btnCompany);
btnCompany.setText(header);
btnCompany.setTag(headerCode);
btnCompany.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String compCode = (String) view.getTag();
Company company = getCompany(compCode, filterCompanyList);
lines = new ArrayList<>();
if (company != null) {
filterCompanyList.remove(company);
setBackgroundColorForTabButton(view, lytTabs, R.color.colorPrimaryExtraExtraLight, R.color.white);
} else {
company = getCompany(compCode, companyList);
filterCompanyList.add(company);
setBackgroundColorForTabButton(view, lytTabs, R.color.white, R.color.colorPrimaryExtraExtraLight);
}
loadFunct(startDate, endDate, "LW");
}
});
lytTabs.addView(root);
setBackgroundColorForTabButton(btnCompany, lytTabs, R.color.white, R.color.colorPrimaryExtraExtraLight);
lytTabs.invalidate();
lytTabs.removeAllViews();函数适用于它
我只想使用 inflater 动态创建按钮一次。我有三个按钮和一个加载功能。这个函数在每个按钮中调用,每次调用时,都会出现预建按钮。我只想在每次单击按钮时创建一个按钮。怎么样?
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
assert inflater != null;
LinearLayout root = (LinearLayout) inflater.inflate(R.layout.analyze_company_buttons_layout, null, false);
Button btnCompany = root.findViewById(R.id.btnCompany);
btnCompany.setText(header);
btnCompany.setTag(headerCode);
btnCompany.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String compCode = (String) view.getTag();
Company company = getCompany(compCode, filterCompanyList);
lines = new ArrayList<>();
if (company != null) {
filterCompanyList.remove(company);
setBackgroundColorForTabButton(view, lytTabs, R.color.colorPrimaryExtraExtraLight, R.color.white);
} else {
company = getCompany(compCode, companyList);
filterCompanyList.add(company);
setBackgroundColorForTabButton(view, lytTabs, R.color.white, R.color.colorPrimaryExtraExtraLight);
}
loadFunct(startDate, endDate, "LW");
}
});
lytTabs.addView(root);
setBackgroundColorForTabButton(btnCompany, lytTabs, R.color.white, R.color.colorPrimaryExtraExtraLight);
lytTabs.invalidate();
lytTabs.removeAllViews();函数适用于它