안드로이드에서 LayoutInflater가 하는 일이 뭔가요?

조회수 7193회

안드로이드에서 LayoutInflater가 하는 일이 뭔가요?

1 답변

  • 좋아요

    0

    싫어요
    채택 취소하기

    리스트뷰에서 커스텀 뷰를 만드려면 반드시 행 레이아웃을 정의해야합니다. LayoutInflater는 xml을 만들고 어댑터의 코드를 짤때 보통 사용합니다.

    public MyAdapter(Context context, List<MyObject> objects) extends ArrayAdapter {
      super(context, 1, objects);
      /* We get the inflator in the constructor */
      mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      View view;
      /* We inflate the xml which gives us a view */
      view = mInflater.inflate(R.layout.my_list_custom_row, parent, false);
    
      /* Get the item in the adapter */
      MyObject myObject = getItem(position);
    
      /* Get the widget with id name which is defined in the xml of the row */
      TextView name = (TextView) view.findViewById(R.id.name);
    
      /* Populate the row's xml with info from the item */
      name.setText(myObject.getName());
    
      /* Return the generated view */
      return view;
    }
    

    부가 설명,

    LayoutInflater란?

    XML에 정의된 Resource(자원) 들을 View의 형태로 반환해 줍니다. 보통 자바 코드에서 View, ViewGroup 을 사용하거나, Adpter의 getview() 또는 Dialog, Popup 구현시 배경화면이 될 Layout을 만들어 놓고 View의 형태로 반환 받아 Acitivity에서 실행 하게 됩니다.

    우리가 보통 Activity를 만들면 onCreate() 메서드에 기본으로 추가되는 setContentView(R.layout.activity_main) 메서드와 같은 원리라고 생각하시면 됩니다. 이 메서드 또한 activity_main.xml 파일을 View로 만들어서 Activity 위에 보여주고 있습니다. 사용자의 화면에 보여지는 것들은 Activity 위에 있는 View라는 점을 잊지 말아 주세요.

    출처 : http://arabiannight.tistory.com/entry/340

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)