我在运行时在baseAdapter getView()中设置了我的视图(按钮)高度。
但是我的layoutParams只为getview的最后一个元素设置。
我的getView代码:
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null) {
vi = inflater.inflate(R.layout.listrow_round_table, null);
btnRoundTables = (Button) vi.findViewById(R.id.btn_table);
//set the btn height at runtime
ViewTreeObserver viewTreeObserver = btnRoundTables.getViewTreeObserver();
viewTreeObserver.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
btnRoundTables.getViewTreeObserver().removeOnPreDrawListener(this);
int btnWidth=btnRoundTables.getMeasuredWidth();
btnRoundTables.setLayoutParams(new LinearLayout.LayoutParams(btnWidth, btnWidth));
return true;
}
});
HashMap<String, String> mainMap = data.get(position);
// Setting all values in listview
btnRoundTables.setText(mainMap.get(Keys.KEY_TITLE));
btnRoundTables.setTag(mainMap.get(Keys.KEY_TABLE_ID));
btnRoundTables.setOnClickListener(this);
Log.d("table name" + mainMap.get(Keys.KEY_TITLE), "listadapterGrid");
}
return vi;
}
我的输出:
感谢您的帮助..
答案 0 :(得分:1)
你在getView中做错了。您必须在适配器中使用ViewHolder模式。
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
ViewHolder holder;
if (convertView == null) {
vi = inflater.inflate(R.layout.listrow_round_table, null);
holder = new ViewHolder();
holder.btnRoundTables = (Button) vi.findViewById(R.id.btn_table);
vi.setTag(holder);
}else{
holder = (ViewHolder)vi.getTag();
}
//set the btn height at runtime
ViewTreeObserver viewTreeObserver = holder.btnRoundTables.getViewTreeObserver();
viewTreeObserver.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
holder.btnRoundTables.getViewTreeObserver().removeOnPreDrawListener(this);
int btnWidth=holder.btnRoundTables.getMeasuredWidth();
holder.btnRoundTables.setLayoutParams(new LinearLayout.LayoutParams(btnWidth, btnWidth));
return true;
}
});
HashMap<String, String> mainMap = data.get(position);
// Setting all values in listview
holder.btnRoundTables.setText(mainMap.get(Keys.KEY_TITLE));
holder.btnRoundTables.setTag(mainMap.get(Keys.KEY_TABLE_ID));
holder.btnRoundTables.setOnClickListener(this);
Log.d("table name" + mainMap.get(Keys.KEY_TITLE), "listadapterGrid");
}
return vi;
}
public class ViewHolder{
Button btnRoundTables;
}