如何将膨胀视图添加到线性布局

发布于 2024-12-02 14:38:38 字数 41 浏览 3 评论 0原文

如何膨胀视图并将其添加到 LinearLayout 的子视图列表中?

How do you inflate a view and add it to a the list of child views of a LinearLayout?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陪你到最终 2024-12-09 14:38:38

当您调用 inflater 构造函数时,将 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);

When you call the inflater constructor, set attachToRoot to false; then manually add the view after you've initialized it. Otherwise, you'll lose your initialization for all but the first child added.

Example:

View view = inflater.inflate(R.layout.some_view, parent, false);
((TextView) view.findViewById(R.id.some_text)).setText(someString);
parent.addView(view);

An example of what not to do:

View view = inflater.inflate(R.layout.some_view, parent);
((TextView) view.findViewById(R.id.some_text)).setText(someString);
乖乖公主 2024-12-09 14:38:38
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;
}
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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文