为什么在列表视图中使用 Inflater [英] Why to use Inflater in listview

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

问题描述

  • 对于为什么我们需要在 android 中使用 inflater,我总是有歧义,为什么它们在 ListView 中用于自定义布局(如下所示)?
  • 什么是充气机?
  • 使用 Inflater 有什么好处?
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 文档的内容...LayoutInflatercode> 是 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 中使用 inflater,我总是有歧义,为什么它们是否在 android ListView 中用于自定义布局?

通常,您不需要直接使用 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.

使用 Inflater 有什么好处?

使用它没有任何好处.您需要使用某种形状或形式的 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/inflation 过程更容易.此外,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.

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

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