ListPopupWindowActivity.java revision b31c3281d870e9abb673db239234d580dcc4feff
1/* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.example.android.supportv7.widget; 18 19import android.os.Bundle; 20import androidx.appcompat.app.AppCompatActivity; 21import androidx.appcompat.widget.ListPopupWindow; 22import android.view.LayoutInflater; 23import android.view.View; 24import android.view.ViewGroup; 25import android.widget.AdapterView; 26import android.widget.BaseAdapter; 27import android.widget.Button; 28import android.widget.CheckBox; 29import android.widget.PopupWindow; 30import android.widget.TextView; 31import com.example.android.supportv7.R; 32 33import java.text.SimpleDateFormat; 34import java.util.Date; 35 36public class ListPopupWindowActivity extends AppCompatActivity { 37 private ViewGroup mContainer; 38 39 private CheckBox mIsModal; 40 41 private TextView mLog; 42 43 private Button mButton; 44 45 private ListPopupWindow mListPopupWindow; 46 47 private BaseAdapter mListPopupAdapter; 48 49 private SimpleDateFormat mDateFormat; 50 51 @Override 52 protected void onCreate(Bundle savedInstanceState) { 53 super.onCreate(savedInstanceState); 54 55 setContentView(R.layout.list_popup_window_activity); 56 57 mDateFormat = new SimpleDateFormat("HH:mm:ss.SSS"); 58 59 mContainer = findViewById(R.id.container); 60 mIsModal = (CheckBox) mContainer.findViewById(R.id.is_modal); 61 mLog = (TextView) mContainer.findViewById(R.id.log); 62 mButton = (Button) mContainer.findViewById(R.id.test_button); 63 64 mButton.setOnClickListener(new View.OnClickListener() { 65 @Override 66 public void onClick(View v) { 67 mListPopupWindow = new ListPopupWindow(mContainer.getContext()); 68 69 final String[] POPUP_CONTENT = 70 new String[] { "Alice", "Bob", "Charlie", "Deirdre", "El" }; 71 mListPopupAdapter = new BaseAdapter() { 72 class ViewHolder { 73 private TextView title; 74 private TextView shortcut; 75 } 76 77 @Override 78 public int getCount() { 79 return POPUP_CONTENT.length; 80 } 81 82 @Override 83 public Object getItem(int position) { 84 return POPUP_CONTENT[position]; 85 } 86 87 @Override 88 public long getItemId(int position) { 89 return position; 90 } 91 92 @Override 93 public View getView(int position, View convertView, ViewGroup parent) { 94 if (convertView == null) { 95 convertView = LayoutInflater.from(parent.getContext()).inflate( 96 R.layout.abc_popup_menu_item_layout, parent, false); 97 ViewHolder viewHolder = new ViewHolder(); 98 viewHolder.title = (TextView) convertView.findViewById(R.id.title); 99 viewHolder.shortcut = 100 (TextView) convertView.findViewById(R.id.shortcut); 101 convertView.setTag(viewHolder); 102 } 103 104 ViewHolder viewHolder = (ViewHolder) convertView.getTag(); 105 viewHolder.title.setText(POPUP_CONTENT[position]); 106 viewHolder.shortcut.setVisibility(View.GONE); 107 return convertView; 108 } 109 }; 110 111 mListPopupWindow.setAdapter(mListPopupAdapter); 112 mListPopupWindow.setAnchorView(mButton); 113 114 // Register a listener to be notified when an item in our popup window has 115 // been clicked. 116 mListPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() { 117 @Override 118 public void onItemClick(AdapterView<?> parent, View view, int position, 119 long id) { 120 addToLog("Item #"+ position + " clicked"); 121 addToLog("Dismissing popup window"); 122 mListPopupWindow.dismiss(); 123 } 124 }); 125 126 // Register a listener to be notified when our popup window is dismissed. 127 mListPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { 128 @Override 129 public void onDismiss() { 130 addToLog("Popup window dismissed"); 131 } 132 }); 133 134 // Set popup window modality based on the current checkbox state. 135 mListPopupWindow.setModal(mIsModal.isChecked()); 136 137 // and show it 138 mListPopupWindow.show(); 139 } 140 }); 141 142 // Set up a click listener on the log text view. When the popup window is in modal 143 // mode and is dismissed by tapping outside of its bounds *and* over the log text 144 // view bounds, we should *not* get this click listener invoked. 145 mLog.setOnClickListener(new View.OnClickListener() { 146 @Override 147 public void onClick(View v) { 148 addToLog("Log view clicked"); 149 } 150 }); 151 } 152 153 private void addToLog(String toLog) { 154 String toPrepend = mDateFormat.format(new Date()) + " " + toLog + "\n"; 155 mLog.setText(toPrepend + mLog.getText()); 156 } 157} 158