我想创建一个没有任何库的可滚动卡片视图。
每个LinearLayout都应该是一张卡片。 我可以通过编程方式创建一个LinearLayout并使用xml LinearLayout中的id初始化它吗?
这里就像这样(但它不起作用):
public class LinearLayoutAdapter extends BaseAdapter {
private List<ImageContent> images;
private Context context;
private Activity activity;
private int width, height;
public LinearLayoutAdapter(Activity activity, Context context, List<ImageContent> img,
int width, int height) {
this.context = context;
this.activity=activity;
this.images = img;
this.width = width;
this.height = height;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return images.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return images.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout lLayout;
if (convertView != null) {
lLayout = (LinearLayout) convertView;
//lLayout = (LinearLayout) convertView.findViewById(R.id.imageLinearLayout); // this does not work
} else {
lLayout = new LinearLayout(context);
}
return lLayout;
}
}
答案 0 :(得分:1)
如果您想从XML获取linearlayout,则需要对布局进行扩充。
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(
R.layout.your_layout, parent, false);
}
lLayout = (LinearLayout) convertView.findViewById(R.id.imageLinearLayout);
查看此网站,以便更好地重复使用ViewHolder进行布局。 http://www.piwai.info/android-adapter-good-practices/