Simple sample app for ListPopupWindow
Change-Id: I239b885313014019226dc0e1a95a3b3f098cad09
This commit is contained in:
@@ -463,6 +463,16 @@
|
||||
<category android:name="com.example.android.supportv7.SAMPLE_CODE"/>
|
||||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
<!-- ListPopupWindow demo activity -->
|
||||
<activity android:name=".widget.ListPopupWindowActivity"
|
||||
android:label="@string/list_popup_window_activity"
|
||||
android:theme="@style/Theme.AppCompat.Light">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN"/>
|
||||
<category android:name="com.example.android.supportv7.SAMPLE_CODE"/>
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- Copyright (C) 2015 The Android Open Source Project
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
|
||||
<RelativeLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/container"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
<Button
|
||||
android:id="@+id/test_button"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignParentTop="true"
|
||||
android:layout_centerHorizontal="true"
|
||||
android:text="@string/list_popup_window_button" />
|
||||
|
||||
<CheckBox
|
||||
android:id="@+id/is_modal"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignParentBottom="true"
|
||||
android:layout_centerHorizontal="true"
|
||||
android:text="@string/list_popup_window_is_modal"
|
||||
android:checked="true" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/log"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="150dip"
|
||||
android:layout_above="@id/is_modal"
|
||||
android:padding="8dip"
|
||||
android:background="#E5E5E5" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_above="@id/log"
|
||||
android:padding="8dip"
|
||||
android:textStyle="bold"
|
||||
android:text="@string/list_popup_window_summary" />
|
||||
</RelativeLayout>
|
||||
|
||||
@@ -195,5 +195,10 @@
|
||||
<string name="mode_night_activity_title">AppCompat/DayNight/Activity Usage</string>
|
||||
<string name="mode_night_dialog_title">AppCompat/DayNight/Dialog Usage</string>
|
||||
<string name="mode_night_alertdialog_title">AppCompat/DayNight/AlertDialog Usage</string>
|
||||
|
||||
<string name="list_popup_window_activity">List popup window</string>
|
||||
<string name="list_popup_window_summary">This activity illustrates the use of list popup windows. The popup window is shown by clicking the button above. The checkbox controls the modality. The text area below logs various events - it should log a tap on itself when a non-modal popup window is dismissed by tapping on the log area.</string>
|
||||
<string name="list_popup_window_button">Show popup!</string>
|
||||
<string name="list_popup_window_is_modal">is modal</string>
|
||||
</resources>
|
||||
|
||||
|
||||
@@ -0,0 +1,158 @@
|
||||
/*
|
||||
* Copyright (C) 2015 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.example.android.supportv7.widget;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.support.v7.widget.ListPopupWindow;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.BaseAdapter;
|
||||
import android.widget.Button;
|
||||
import android.widget.CheckBox;
|
||||
import android.widget.FrameLayout;
|
||||
import android.widget.PopupWindow;
|
||||
import android.widget.TextView;
|
||||
import com.example.android.supportv7.R;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
public class ListPopupWindowActivity extends AppCompatActivity {
|
||||
private ViewGroup mContainer;
|
||||
|
||||
private CheckBox mIsModal;
|
||||
|
||||
private TextView mLog;
|
||||
|
||||
private Button mButton;
|
||||
|
||||
private ListPopupWindow mListPopupWindow;
|
||||
|
||||
private BaseAdapter mListPopupAdapter;
|
||||
|
||||
private SimpleDateFormat mDateFormat;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
setContentView(R.layout.list_popup_window_activity);
|
||||
|
||||
mDateFormat = new SimpleDateFormat("HH:mm:ss.SSS");
|
||||
|
||||
mContainer = (ViewGroup) findViewById(R.id.container);
|
||||
mIsModal = (CheckBox) mContainer.findViewById(R.id.is_modal);
|
||||
mLog = (TextView) mContainer.findViewById(R.id.log);
|
||||
mButton = (Button) mContainer.findViewById(R.id.test_button);
|
||||
|
||||
mButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
mListPopupWindow = new ListPopupWindow(mContainer.getContext());
|
||||
|
||||
final String[] POPUP_CONTENT =
|
||||
new String[] { "Alice", "Bob", "Charlie", "Deirdre", "El" };
|
||||
mListPopupAdapter = new BaseAdapter() {
|
||||
class ViewHolder {
|
||||
private TextView title;
|
||||
private TextView shortcut;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount() {
|
||||
return POPUP_CONTENT.length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getItem(int position) {
|
||||
return POPUP_CONTENT[position];
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getItemId(int position) {
|
||||
return position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public View getView(int position, View convertView, ViewGroup parent) {
|
||||
if (convertView == null) {
|
||||
convertView = LayoutInflater.from(parent.getContext()).inflate(
|
||||
R.layout.abc_popup_menu_item_layout, parent, false);
|
||||
ViewHolder viewHolder = new ViewHolder();
|
||||
viewHolder.title = (TextView) convertView.findViewById(R.id.title);
|
||||
viewHolder.shortcut =
|
||||
(TextView) convertView.findViewById(R.id.shortcut);
|
||||
convertView.setTag(viewHolder);
|
||||
}
|
||||
|
||||
ViewHolder viewHolder = (ViewHolder) convertView.getTag();
|
||||
viewHolder.title.setText(POPUP_CONTENT[position]);
|
||||
viewHolder.shortcut.setVisibility(View.GONE);
|
||||
return convertView;
|
||||
}
|
||||
};
|
||||
|
||||
mListPopupWindow.setAdapter(mListPopupAdapter);
|
||||
mListPopupWindow.setAnchorView(mButton);
|
||||
|
||||
// Register a listener to be notified when an item in out popup window has
|
||||
// been clicked.
|
||||
mListPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
||||
@Override
|
||||
public void onItemClick(AdapterView<?> parent, View view, int position,
|
||||
long id) {
|
||||
addToLog("Item #"+ position + " clicked");
|
||||
addToLog("Dismissing popup window");
|
||||
mListPopupWindow.dismiss();
|
||||
}
|
||||
});
|
||||
|
||||
// Register a listener to be notified when our popup window is dismissed.
|
||||
mListPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
|
||||
@Override
|
||||
public void onDismiss() {
|
||||
addToLog("Popup window dismissed");
|
||||
}
|
||||
});
|
||||
|
||||
// Set popup window modality based on the current checkbox state.
|
||||
mListPopupWindow.setModal(mIsModal.isChecked());
|
||||
|
||||
// and show it
|
||||
mListPopupWindow.show();
|
||||
}
|
||||
});
|
||||
|
||||
// Set up a click listener on the log text view. When the popup window is in modal
|
||||
// mode and is dismissed by tapping outside of its bounds *and* over the log text
|
||||
// view bounds, we should *not* get this click listener invoked.
|
||||
mLog.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
addToLog("Log view clicked");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void addToLog(String toLog) {
|
||||
String toPrepend = mDateFormat.format(new Date()) + " " + toLog + "\n";
|
||||
mLog.setText(toPrepend + mLog.getText());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user