我正在创建一个FrameLayout然后我添加两个视图(一个ImageView和一个TextView)。
我在getView()上的代码:
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
TextView textView1;
int new_width = wid/2;
int textview_id=0;
FrameLayout frame_layout = new FrameLayout(mContext); // Instantiate the parent
frame_layout.setLayoutParams(new GridView.LayoutParams(android.widget.FrameLayout.LayoutParams.MATCH_PARENT,
android.widget.FrameLayout.LayoutParams.MATCH_PARENT));
textView1 = new TextView(mContext); textView1.setId(position);
textView1.setText("Set Wallpaper");
textView1.setTextColor(mContext.getResources().getColor(R.color.set_wallpaper_colors));
textView1.setVisibility(View.INVISIBLE);
textView1.setTypeface(textView1.getTypeface(), Typeface.BOLD);
FrameLayout.LayoutParams textview_params = new FrameLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
textview_params.gravity = Gravity.CENTER;
FrameLayout.LayoutParams imageview_params = new FrameLayout.LayoutParams(
new_width -5, new_width -5 );
imageView = new ImageView(mContext);
//imageView.setLayoutParams(new GridView.LayoutParams(new_width - 5, new_width -5));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(0, 0, 0, 0);
imageView.setImageResource(mThumbIds[position]);
frame_layout.addView(imageView, imageview_params);
frame_layout.addView(textView1, textview_params);
return frame_layout;
}
如何更改它以使用ViewHolder类。没有布局可以膨胀。
答案 0 :(得分:1)
使用您的代码应该是这样的
static class ViewHolder {
TextView textView1;
ImageView imageView1;
}
@Override
public View getView(final int row, View convertView, final ViewGroup parent) {
ViewHolder holder = null;
FrameLayout frame_layout = (FrameLayout) convertView;
if (frame_layout == null) {
holder = new ViewHolder();
holder.textView1 = new TextView(mContext);
holder.imageView1 = new ImageView(mContext);
frame_layout.setTag(holder);
}
else {
holder = (ViewHolder) frame_layout.getTag();
}
holder.textView1.setText("Set Wallpaper");
holder.textView1.setTextColor(mContext.getResources().getColor(R.color.set_wallpaper_colors));
holder.textView1.setVisibility(View.INVISIBLE);
holder.textView1.setTypeface(holder.textView1.getTypeface(), Typeface.BOLD);
FrameLayout.LayoutParams textview_params = new FrameLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
textview_params.gravity = Gravity.CENTER;
FrameLayout.LayoutParams imageview_params = new FrameLayout.LayoutParams(
new_width -5, new_width -5 );
holder.imageView1.setScaleType(ImageView.ScaleType.CENTER_CROP);
holder.imageView1.setPadding(0, 0, 0, 0);
holder.imageView1.setImageResource(mThumbIds[position]);
frame_layout.addView(holder.imageView1, imageview_params);
frame_layout.addView(holder.textView1, textview_params);
return frame_layout;
}