如何填充一个视图并将其添加到
LinearLayout
的子视图列表中?attachToRoot
设置为false;然后在初始化视图后手动添加视图。否则,除了第一个添加的子项之外,所有其他初始化都会丢失。View view = inflater.inflate(R.layout.some_view, parent, false);
((TextView) view.findViewById(R.id.some_text)).setText(someString);
parent.addView(view);
一个不应该做的例子:
View view = inflater.inflate(R.layout.some_view, parent);
((TextView) view.findViewById(R.id.some_text)).setText(someString);
public class SearchResultAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<SearchResultInfo> ChoseInfo;
private static LayoutInflater inflater=null;
public SearchResultImageLoader imageLoader;
public SearchResultAdapter(Activity a, ArrayList<SearchResultInfo> ChoseInfo) {
activity = a;
this.ChoseInfo=ChoseInfo;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new SearchResultImageLoader(activity.getApplicationContext());
}
public int getCount() {
return ChoseInfo.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public static class ViewHolder{
public TextView PriceValue;
public ImageView image;
public TextView LikeValue;
public TextView LikeName;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if(convertView==null){
vi = inflater.inflate(R.layout.searchresult_showlayout_item, null);
holder=new ViewHolder();
holder.PriceValue=(TextView)vi.findViewById(R.id.priceValue);
holder.image=(ImageView)vi.findViewById(R.id.clothimage);
holder.LikeName=(TextView)vi.findViewById(R.id.LikeName);
holder.LikeValue=(TextView)vi.findViewById(R.id.LikeValue);
vi.setTag(holder);
}
else
{
holder=(ViewHolder)vi.getTag();
}
holder.PriceValue.setText(ChoseInfo.get(position).Price);
holder.LikeValue.setText(ChoseInfo.get(position).LikeNum);
holder.image.setTag(ChoseInfo.get(position).BitmapPath);
imageLoader.DisplayImage(ChoseInfo.get(position).BitmapPath, activity, holder.image);
holder.image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//这里以后要进行传值
Intent intent =new Intent();
intent.setClass(getDialogContext(activity), SearchDetailActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
getDialogContext(activity).startActivity(intent);
}
});
return vi;
}