我正在使用自定义适配器,每行都有使用
创建的按钮数量@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowview = (HorizontalScrollView) inflater.inflate(
R.layout.hrowlayout, parent, false);
LinearLayout ll1 = (LinearLayout) rowview.findViewById(R.id.ll);
Button[] b = new Button[M];
for (int i = 0; i < M; i++) {
b[i] = new Button(ll1.getContext());
b[i].setText(....);
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams( x, 90);
b[i].setLayoutParams(param2);
ll1.addView(b[i],params2);
}
return rowview;
}
我的问题是如何在此适配器上使用ViewHolder以及如何标记不在XML文件中的按钮..即
b[i] = new Button[M];
答案 0 :(得分:1)
ViewHolder模式中的ViewHolder是一个哑对象,它只保存对视图内容的引用,以避免重复使用inflate()和findViewById(),这样你就可以将Button数组与任何东西一起粘贴到ViewHolder中否则你想参考。
这样的事情就是你做的。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewHolder holder;
if(convertView == null){//if we haven't already created a view
//inflate a new view
View rowview = (HorizontalScrollView) inflater.inflate(
R.layout.hrowlayout, parent, false);
LinearLayout ll1 = (LinearLayout) rowview.findViewById(R.id.ll);
Button[] b = new Button[M];
for (int i = 0; i < M; i++) {
b[i] = new Button(ll1.getContext());
b[i].setText(....);
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams( x, 90);
b[i].setLayoutParams(param2);
ll1.addView(b[i],params2);
}
convertView = rowview;
holder = new ViewHolder();
holder.buttons = b;
convertView.setTag(viewHolder);
}
holder = convertView.getTag()
for(Button button : holder.buttons){
//do something
}
return convertView;
}
和查看持有人
class ViewHolder{
Button [] buttons;
//other things
}
答案 1 :(得分:0)
您可以在viewholder中声明您的按钮数组
public static class viewHolder{
Button[] b;
}
getView
中的您可以使用holder.b=new Button[4];