ActionBarCompat-ListPopupMenu / src / com.example.android.actionbarcompat.listpopupmenu /

PopupListFragment.java

1
/*
2
 * Copyright (C) 2013 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
package com.example.android.actionbarcompat.listpopupmenu;
17
 
18
import android.os.Bundle;
19
import android.support.v4.app.ListFragment;
20
import android.support.v7.widget.PopupMenu;
21
import android.view.MenuItem;
22
import android.view.View;
23
import android.view.ViewGroup;
24
import android.widget.ArrayAdapter;
25
import android.widget.ListView;
26
import android.widget.Toast;
27
 
28
import java.util.ArrayList;
29
 
30
/**
31
 * This ListFragment displays a list of cheeses, with a clickable view on each item whichs displays
32
 * a {@link android.support.v7.widget.PopupMenu PopupMenu} when clicked, allowing the user to
33
 * remove the item from the list.
34
 */
35
public class PopupListFragment extends ListFragment implements View.OnClickListener {
36
 
37
    @Override
38
    public void onActivityCreated(Bundle savedInstanceState) {
39
        super.onActivityCreated(savedInstanceState);
40
 
41
        // We want to allow modifications to the list so copy the dummy data array into an ArrayList
42
        ArrayList<String> items = new ArrayList<String>();
43
        for (int i = 0, z = Cheeses.CHEESES.length ; i < z ; i++) {
44
            items.add(Cheeses.CHEESES[i]);
45
        }
46
 
47
        // Set t
he ListAdapter
48
        setListAdapter(new PopupAdapter(items));
49
    }
50
 
51
    @Override
52
    public void onListItemClick(ListView listView, View v, int position, long id) {
53
        String item = (String) listView.getItemAtPosition(position);
54
 
55
        // Show a toast if the user clicks on an item
56
        Toast.makeText(getActivity(), "Item Clicked: " + item, Toast.LENGTH_SHORT).show();
57
    }
58
 
59
    @Override
60
    public void onClick(final View view) {
61
        // We need to post a Runnable to show the popup to make sure that the PopupMenu is
62
        // correctly positioned. The reason being that the view may change position before the
63
        // PopupMenu is shown.
64
        view.post(new Runnable() {
65
            @Override
66
            public void run() {
67
                showPopupMenu(view);
68
            }
69
        });
70
    }
71
 
73
    private void showPopupMenu(View view) {
74
        final PopupAdapter adapter = (PopupAdapter) getListAdapter();
75
 
76
        // Retrieve the clicked item from view's tag
77
        final String item = (String) view.getTag();
78
 
79
        // Create a PopupMenu, giving it the clicked view for an anchor
80
        PopupMenu popup = new PopupMenu(getActivity(), view);
81
 
82
        // Inflate our menu resource into the PopupMenu's Menu
83
        popup.getMenuInflater().inflate(R.menu.popup, popup.getMenu());
84
 
85
        // Set a listener so we are notified if a menu item is clicked
86
        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
87
            @Override
88
            public boolean onMenuItemClick(MenuItem menuItem) {
89
                switch (menuItem.getItemId()) {
90
                    case R.id.menu_remove:
91
                        // Remove the item from the adapter
92
                        adapter.remove(item);
93
                        return true;
94
                }
95
                return false;
96
            }
97
        });
98
 
99
        // Finally show the PopupMenu
100
        popup.show();
101
    }
103
 
104
    /**
105
     * A simple array adapter that creates a list of cheeses.
106
     */
107
    class PopupAdapter extends ArrayAdapter<String> {
108
 
109
        PopupAdapter(ArrayList<String> items) {
110
            super(getActivity(), R.layout.list_item, android.R.id.text1, items);
111
        }
112
 
113
        @Override
114
        public View getView(int position, View convertView, ViewGroup container) {
115
            // Let ArrayAdapter inflate the layout and set the text
116
            View view = super.getView(position, convertView, container);
117
 
119
            // Retrieve the popup button from the inflated view
120
            View popupButton = view.findViewById(R.id.button_popup);
121
 
122
            // Set the item as the button's tag so it can be retrieved later
123
            popupButton.setTag(getItem(position));
124
 
125
            // Set the fragment instance as the OnClickListener
126
            popupButton.setOnClickListener(PopupListFragment.this);
128
 
129
            // Finally return the view to be displayed
130
            return view;
131
        }
132
    }
133
 
134
}