Detailed explanation of LayoutInflater in Android Development

Source: Internet
Author: User

In actual development, the LayoutInflater class is very useful. Its function is similar to findViewById (). The difference is that LayoutInflater is used to find and instantiate xml layout files in layout! FindViewById () is used to find the widget controls (such as buttons and TextView) in a specific xml file ). In many cases, you can use inflate to load an interface that is not loaded or needs to be dynamically loaded. for example, in the getView of BaseAdapter, the widget in the view is obtained in the Custom Dialog. Below is a simple BaseAdapter: first declare LayoutInflater [java] private LayoutInflater inflater = null; use [java] inflater = LayoutInflater in the getView () method. from (context); View v = inflater. inflate (R. layout. listview_item, null); complete code: [java] public class PullXmlAdapter extends BaseAdapter {private List <Book> list = null; private Context context = null; private LayoutInflater inflater = null; public PullXmlAdapter (Context context, List <Book> list) {// TODO Auto-generated constructor stub this. context = context; this. list = list ;}@ Override public int getCount () {// TODO Auto-generated method stub return list. size () ;}@ Override public Object getItem (int arg0) {// TODO Auto-generated method stub return arg0 ;}@ Override public long getItemId (int arg0) {// TODO Auto-generated method stub return arg0;} @ Override public View getView (int arg0, View arg1, ViewGroup arg2) {// TODO Auto-generated method stub inflater = LayoutInflater. from (context); View v = inflater. inflate (R. layout. listview_item, null); TextView TV = (TextView) v. findViewById (R. id. lisview_item_ TV); TV. setText (list. get (arg0 ). getName (); return v ;}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.