LoginSignup
4
6

More than 5 years have passed since last update.

ListViewのヘッダー追加

Posted at

ListViewには、ヘッダー/フッターを追加することが出来る。

設定方法

ヘッダーの追加方法は以下の通りListView::addHeaderView()を利用する。
第3引数は選択可否フラグ。以下例では選択不可にしている。

list = (ListView)findViewById(R.id.list_view);
View header = View.inflate(this, R.layout.header_list, null);
list.addHeaderView(header, null, false);

注意点

選択処理の実装において、OnItemClickListener::onItemClick()を利用するが、
position = 0がAdapterの先頭ではなくヘッダーとなる点に注意が必要。

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // headerがposition = 0となるため、adapterから取得するindexを調整する必要がある
        if(position <= 0) {
            return;
        }
        XXXObject  object = adapter.getItem(position - 1);
        if(object == null) {
            return;
        }
        // TODO: 
    }
});
4
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
6