Why to use Inflater in listview

后端 未结 3 1179
伪装坚强ぢ
伪装坚强ぢ 2020-12-28 22:47
  • I always had ambiguity on why we need to use inflater in android, Why are they used in ListView for custom layouts (like below)?
  • What is an Inflater ?
  • <
相关标签:
3条回答
  • 2020-12-28 22:50

    I always had ambiguity on why wee need to use inflater in android, Why are they used in android ListView for a custom layout ?
    They are used to create the Views for each row.

    What is an Inflater ?
    A system service that creates a View out of an XML layout.

    In the below code i am trying to understand why Inflator is used ?
    The inflater is used to create the Views for the rows.

    What is the advantage of using Inflater ?
    Compared to what? How do you want to create the Views out of the XML layout?

    0 讨论(0)
  • 2020-12-28 22:56

    What is an Inflater ?

    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.

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

    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.

    What is the advantage of using Inflater ?

    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.

    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.

    0 讨论(0)
  • 2020-12-28 23:07

    Put simply, an inflater allows you to create a View from a resource layout file so that you do not need to create everything programmatically.

    In your example, you inflate the layout R.layout.list_mobile. This lets you access all of the views within it. For example, you then call:

    TextView textView = (TextView) rowView.findViewById(R.id.label);
    

    By calling rowView.findViewById() you are able to access views that were created within that layout. Often for ListViews you will have a row XML file that you then inflate and put your data into the views.

    0 讨论(0)
提交回复
热议问题