在运行时将按钮添加到列表视图中的每一行
问题描述:
我正在使用列表视图的android应用程序中工作。我目前只有一个文本视图的行布局的XML。根据某些条件,某些行将有一个附加按钮,其他一些行可能有两个附加按钮。我可以重写适配器类的getView方法来执行此逻辑吗?有没有任何性能问题?在运行时将按钮添加到列表视图中的每一行
答
有2个按钮,然后一排布局:
public View getView(int position, View convertView, ViewGroup parent){
// the usual convertView stuff
if(convertView == null){
convertView = layoutInflater.inflate(//TODO);
Tag tag = new Tag();
convertView.setTag(tag);
tag.button1 = (Button)convertView.findViewById(R.id.btn1);
tag.button2 = (Button)convertView.findViewById(R.id.btn2);
}
Tag tag = (Tag)convertView.getTag();
boolean buttonOneShown = //TODO;
boolean buttonTwoShown = //TODO;
tag.button1.setVisibility(buttonOneShown ? VISIBLE : GONE);
tag.button1.setVisibility(buttonTwoShown ? VISIBLE : GONE);
}
表现会就好了。
如何定义标签对象?我似乎无法在Android API文档中找到它... – katzoft 2010-08-30 14:38:28