LoginSignup
14
14

More than 5 years have passed since last update.

Android版Chromeみたいなボタン付きメニュー

Last updated at Posted at 2016-04-26

chrome.png
Android版Chromeのようなリストアイテムの上部にボタンが並んだメニューを、PopupWindowとしてActivity/Fragmentの上に重ねて表示します

今回はViewを細かく触らずAdapterを使って済ませたかったので、ListPopupWindowにヘッダーとしてボタンを表示し、リストとしてメニュー内容(新しいタブ、新しいシークレットタブ…)を表示します
PopupWindowでやるなら多分PopupWindow::setContentViewでできると思います

ListPopupWindow::setAdapterでListViewと同じようにアダプターを渡すと、リスト表示したPopupWindowが表示されます
ListPopupWindowはListViewのようにHeaderViewを追加することができないので、一度HeaderViewつきのListViewを用意してから、ListViewのアダプターをListPopupWindowに設定します

ListPopupWindow popupWindow = new ListPopupWindow(this);

ListView listView = new ListView(this);
listView.addHeaderView(createHeader(popupWindow.getListView()));
listView.setAdapter(createListAdapter(this));

HeaderViewListAdapter adapter = (HeaderViewListAdapter) listView.getAdapter();
popupWindow.setAdapter(adapter);
popupWindow.show();

するとChromeメニューのように、ヘッダーつきのリスト表示PopupWindowが表示されます
chromenu.png

あとはHeaderView内にImageButtonを表示してクリックイベントを紐付ければChromeアプリのメニューのように動かせます
ソース:https://github.com/gyamoto/ChroMenu

14
14
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
14
14