为什么在ListView中使用Inflater [英] Why to use Inflater in listview

查看:104
本文介绍了为什么在ListView中使用Inflater的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 我总是不清楚为什么我们需要在android中使用充气机,为什么它们在ListView中用于自定义布局(如下所示)?
  • 什么是充气机?
  • 使用充气机有什么优势?
public class MobileArrayAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final String[] values;


public MobileArrayAdapter(Context context, String[] values) {
    super(context, R.layout.list_mobile, values);
    this.context = context;
    this.values = values;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.list_mobile, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.label);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
    textView.setText(values[position]);

谢谢

推荐答案

什么是充气机?

总结 LayoutInflater文档所说的... ... LayoutInflater是其中之一Android系统服务,负责获取定义布局的XML文件并将其转换为View对象.然后,操作系统使用这些视图对象绘制屏幕.

To summarize what the LayoutInflater Documentation says... A LayoutInflater is one of the Android System Services that is responsible for taking your XML files that define a layout, and converting them into View objects. The OS then uses these view objects to draw the screen.

对于为什么我们需要在android中使用充气机,我一直含糊不清,为什么 它们在Android ListView中用于自定义布局吗?

I always had ambiguity on why we need to use inflater in android, Why are they used in android ListView for a custom layout ?

通常,您不需要直接使用LayoutInflater.当您在活动的onCreate()方法中调用setContentView()时,Android会为您完成大部分布局调整. 因此,作为程序员,您有责任确保视图被夸大.现在,您想在ListView的上下文中为视图充气.如果您不想自定义每个项目,则Adapter类可以为您完成膨胀.但是,如果要自定义列表中显示的视图,则必须使用LayoutInflater手动为每个视图充气,因为没有其他可用的现有方法.

Typically, you don't ever need to directly use a LayoutInflater. Android does most of the layout inflation for you when you call setContentView() in the onCreate() method of your activity. So you, as the programmer, are responsible for making sure the views are inflated. Now you want to inflate views in the context of a ListView. The Adapter class can do the inflation for you if you do not want to customize each item. But if you want to customize the views shown in a list, you will have to manually inflate each view with the LayoutInflater, since there is no other existing method you can use.

使用充气机有什么优势?

使用它没有优势.您需要使用某种形状或形式的LayoutInflater来膨胀您的静态XML布局.

There is no advantage to using it. You are required to use a LayoutInflater in some shape or form to inflate your static XML layouts.

或者,您可以使用Java代码动态创建视图.但是,您将需要调用方法来手动设置视图的每个属性.我认为,使用XML/膨胀过程更容易.此外,Android在构建时会对XML文件进行预处理,因此可以缩短执行时间.

Alternatively, you could create views dynamically with java code. However, you would need to call methods to set each property for the view by hand. In my opinion, it is easier to use the XML/inflation process. In addition, Android pre-processes your XML files at build time, so this results in a faster execution time.

这篇关于为什么在ListView中使用Inflater的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆