【AndroidのViewを制する】 ListPopupWindowのポップアップ表示を調整する
今回、紹介するListPopupWindowは画面の任意の位置に好きなListViewをポップアップすることができます。
メソッドがたくさんあるため数回に分割して説明します。
今回はListPopupWindowのListViewやポップアップの見た目を変更するメソッドの説明します。
プロンプトビューを設定する
ポップアップには入力を促す説明を表示するためのプロンプトビューを設定することができます。使用メソッド
| void setPromptPosition(int position) |
プロンプトビューを表示する位置を設定します。 表示可能な位置はリストの上下のどちらか片方になります。 引数にはListPopupWindowに定義されている下記の定数を使用します。
|
| int getPromptPosition() |
プロンプトビューを表示する位置を取得します。 |
| void setPromptView(View prompt) |
プロンプトビューとして表示するViewを設定します。 |
リストを設定する
ListPopupWindowのフィールドにはDropDownListViewがあり、このクラスはListViewを継承しています。 そのため、通常のListViewと同様の操作でリストを表示、操作可能です。使用メソッド
| void setAdapter(ListAdapter adapter) | ListPopupWindowに含まれるDropDownListViewにListAdapterを設定します。 |
| ListView getListView() |
ListPopupWindowに含まれるDropDownListViewを取得します。 isShowingがfalseの場合はnullが返却されます。 |
| void setListSelector(Drawable selector) | ListView内のアイテムが選択された時の色変化をセレクターで設定する。 |
| void setBackgroundDrawable(Drawable d) | 背景色をDrawableインスタンスで指定する。 |
| Drawable getBackground() | 背景色のDrawableインスタンスを取得する。 |
使用サンプル
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout frameLayout = new FrameLayout(this);
final Button button = new Button(this);
button.setText("表示");
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final ListPopupWindow listPopupWindow = createListPopupWindow(button);
setListView(listPopupWindow);
listPopupWindow.show();
ListView listView = listPopupWindow.getListView();
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
});
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ListPopupWindow.WRAP_CONTENT, ListPopupWindow.WRAP_CONTENT);
layoutParams.gravity = Gravity.CENTER;
frameLayout.addView(button , layoutParams);
setContentView(frameLayout);
}
private ListPopupWindow createListPopupWindow(Button button) {
final ListPopupWindow listPopupWindow = new ListPopupWindow(this);
// アンカーViewの設定
listPopupWindow.setAnchorView(button);
// 幅の設定
listPopupWindow.setWidth(500);
// 高さの設定
listPopupWindow.setHeight(500);
// 垂直下方向に表示位置を200ピクセル移動させる。
listPopupWindow.setVerticalOffset(200);
// 水平右方向に表示位置を200ピクセル移動させる。
listPopupWindow.setHorizontalOffset(200);
return listPopupWindow;
}
private void setListView(final ListPopupWindow listPopupWindow) {
// プロンプトビューを設定
setPrompt(listPopupWindow);
// ListViewを設定
setList(listPopupWindow);
}
private void setList(ListPopupWindow listPopupWindow) {
final String[] strings = new String[] {
"日曜日", "月曜日", "火曜日", "水曜日", "木曜日", "金曜日", "土曜日"
};
final ListAdapter listAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_checked, strings);
// ListPopupWindowに含まれるListViewにAdapterを設定します。
listPopupWindow.setAdapter(listAdapter);
// Listをタップした時の色の変化をセレクターで設定する。
StateListDrawable d = new StateListDrawable();
ColorDrawable colorPressed = new ColorDrawable(Color.GREEN);
colorPressed.setAlpha(100);
d.addState( new int[]{ android.R.attr.state_pressed }, colorPressed );
listPopupWindow.setListSelector(d);
// 背景色を青色に変更
ColorDrawable backgroundDrawable = new ColorDrawable(Color.BLUE);
backgroundDrawable.setAlpha(80);
listPopupWindow.setBackgroundDrawable(backgroundDrawable);
}
private void setPrompt(ListPopupWindow listPopupWindow) {
ImageView imageView = new ImageView(this);
imageView.setBackgroundResource(R.mipmap.ic_launcher);
// ImageViewをプロンプトビューとして設定
listPopupWindow.setPromptView(imageView);
// プロンプトビューを下端に表示するように設定
listPopupWindow.setPromptPosition(ListPopupWindow.POSITION_PROMPT_BELOW);
}
}