[안드로이드] ListView에서 addFooterView 했을 때 보이지 않는 문제

 ListView 에는 addFooterView라는 것이 있습니다. 안드로이드 개발자 사이트를 보면 아래와 같이 설명이 되어 있습니다.







public void addFooterView (View v)


Since: API Level 1



Add a fixed view to appear at the bottom of the list. If addFooterView is called more than once, the views will appear in the order they were added. Views added using this call can take focus if they want.


NOTE: Call this before calling setAdapter. This is so ListView can wrap the supplied cursor with one that will also account for header and footer views.



Parameters





v The view to add.


public void addFooterView (View v, Object data, boolean isSelectable)


Since: API Level 1



Add a fixed view to appear at the bottom of the list. If addFooterView is called more than once, the views will appear in the order they were added. Views added using this call can take focus if they want.


NOTE: Call this before calling setAdapter. This is so ListView can wrap the supplied cursor with one that will also account for header and footer views.



Parameters











v The view to add.
data Data to associate with this view
isSelectable true if the footer view can be selected

출처 : http://developer.android.com/reference/android/widget/ListView.html#addFooterView(android.view.View)

 보니까 setAdapter 전에 호출을 해야 된다고 합니다. 제가 원하는 것은 setAdapter 후에도 정상적으로 해당 함수가 동작하는이었습니다. 그래서 테스트를 해보니 setAdapter 전에 dummy layout을 하나 add 해놓으면 그 뒤로 추가하는 것은 문제가 없다는 것을 발견하였습니다. 즉 setAdapter 후에도 addFooterView와 같은 것이 동작하도록 하려면 우선 dummy layout을 하나 만들어야 합니다. 예를 들면 아래와 같습니다.





<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout

xmlns:android=”http://schemas.android.com/apk/res/android”

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”>

</LinearLayout>

 setAdapter 함수를 호출하기 전에 이러한 layout 파일을 하나 addFooterView 하도록 합니다. add를 하더라도 내용이 아무 것도 없기 때문에 화면에 보이지 않습니다. 이렇게 하면 setAdapter 후에 addFooterView를 해도 정상적으로 해당 기능이 동작하는 것을 보실 수 있으실 것 입니다. 하지만 addHeaderView의 경우 비슷한 기능을 하는 함수인데 아래 코드 때문에 이와 같은 동작이 불가능 한 것 같습니다.





if (mAdapter != null) { throw new IllegalStateException( “Cannot add header view to list — setAdapter has already been called.”); }

.

– 안드로이드 2.1 & 2.2 버전에서 테스트 되었습니다.

2 thoughts on “[안드로이드] ListView에서 addFooterView 했을 때 보이지 않는 문제

  1. 안녕하세요. 블로그 내용 잘 참고하였습니다. ^^ 감사합니다
    질문있습니다. setAdapter 전에 dummy를 추가하였는데, 잘 안되네요..

    View header = getLayoutInflater().inflate(R.layout.header, null, false);
    View dummy = getLayoutInflater().inflate(R.layout.dummy, null, false);

    listView.addHeaderView(header);
    listView.addHeaderView(dummy); <- 추가부분. listView.setAdapter(adapter); 이런 식으로 하라는 말씀 아니신지요? addHeader는 예외때문에 절대 안되는건가요? footer는 잘되는데 ㅠㅠ 답변부탁드립니다 ^^

    1. 네 그렇게 하라는 것이 맞습니다만, 헤더가 잘 안되나요?
      잘 안된다고 하신게 어떤게 안된다고 하시는 것인지 정확히 모르겠네요^^;

답글 남기기

이메일 주소는 공개되지 않습니다.