setOnLongClickListener

setOnLongClickListener

我在ListView中看到了许多setOnLongClickListener的示例,但是我使用的是AndroidHive's ExpandableListView,因此似乎无法识别(错误:setOnLongClickListener无法解析为一种类型)。我该如何做才能使用setOnLongClickListener?

ExpandableListView expListView;
...
expListView.setOnLongClickListener(new OnLongClickListener() {
            @Override
            public boolean onLongChildClick(View v) {

                return true;
            }
        });


MainActivity.java(侦听器位于第一个方法中):

package com.example.groceryrunnerv4;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.Toast;


public class MainActivity extends Activity {

    ExpandableListAdapter listAdapter;
    ExpandableListView expListView;
    List<String> listDataHeader;
    HashMap<String, List<String>> listDataChild;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // get the listview
        expListView = (ExpandableListView) findViewById(R.id.lvExp);

        // preparing list data
        prepareListData();

        listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

        // setting list adapter
        expListView.setAdapter(listAdapter);


        // Listview on child click listener
        expListView.setOnChildClickListener(new OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {

                //((TextView) v).setPaintFlags(((TextView) v).getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
                //listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition)   .setPaintFlags(CHILD.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
                return false;
            }
        });

        expListView.setOnLongClickListener(new OnLongClickListener() {
            @Override
            public boolean onLongChildClick(View v) {

                return true;
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


     // Adds food group data
    private void prepareListData() {
        listDataHeader = new ArrayList<String>();
        listDataChild = new HashMap<String, List<String>>();

        // Adding child data groups
        listDataHeader.add("Produce");
        listDataHeader.add("Grains");
        listDataHeader.add("Meat & Seafood");
        listDataHeader.add("Frozen");
        listDataHeader.add("Canned");
        listDataHeader.add("Bakery");
        listDataHeader.add("Beverages");
        listDataHeader.add("Other");

        // Adding child data items
        List<String> Produce = new ArrayList<String>();
        Produce.add("Chaquita Bananas");
        Produce.add("Apples (8)");
        Produce.add("Kiwi");
        Produce.add("Romaine Lettuce (3)");

        List<String> Grains = new ArrayList<String>();
        Grains.add("Whole Grain Bread");
        Grains.add("Whole Wheat English Muffins");
        Grains.add("Pasta");
        Grains.add("Oatmeal");

        List<String> MeatSeafood = new ArrayList<String>();
        MeatSeafood.add("My dead friends");

        List<String> Frozen = new ArrayList<String>();
        Frozen.add("Edamame");
        Frozen.add("Bean Burgers");

        List<String> Canned = new ArrayList<String>();
        Canned.add("Amy's Lentils");
        Canned.add("Jam");
        Canned.add("Peanu Butter");

        List<String> Bakery = new ArrayList<String>();
        Canned.add("Fresh Bread");

        List<String> Beverages = new ArrayList<String>();
        Canned.add("Water");

        listDataChild.put(listDataHeader.get(0), Produce);
        listDataChild.put(listDataHeader.get(1), Grains);
        listDataChild.put(listDataHeader.get(2), MeatSeafood);
        listDataChild.put(listDataHeader.get(3), Frozen);
        listDataChild.put(listDataHeader.get(4), Canned);
        listDataChild.put(listDataHeader.get(5), Bakery);
        listDataChild.put(listDataHeader.get(6), Beverages);
    }

    // Method for activity events
    public void onButtonClick(View v) {
        final int id = v.getId();
        switch (id) {
        case R.id.CreateLG:
            createLGPopup(v);
            break;
        case R.id.EditButton:
            createEditButtonPopup(v);
            break;
        case R.id.SaveButton:
            Toast.makeText(getApplicationContext(), "List saved.",
                    Toast.LENGTH_SHORT).show();
            break;
        case R.id.ListButton:
            // chooseListDialog()
        }
    }
    // findViewById(R.id.GetStarted).setVisibility(View.INVISIBLE);
    // TextView text = (TextView) findViewById(R.id.GetStarted);
    // text.setText(choice);

    // CreateLG Button's Popup Menu
    public void createLGPopup(View v) {
        PopupMenu LGMenu = new PopupMenu(this, v);
        LGMenu.getMenuInflater().inflate(R.menu.createlg_menu, LGMenu.getMenu());
        LGMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(MenuItem item) {
                String choice = new String((String) item.getTitle());
                if (choice.equals("Create List")) {
                    createListDialog();
                }
                else if (choice.equals("Create Group")) {
                    createGroupDialog();
                }
                return false;
            }
        });
        LGMenu.show();
    }

    // Create Edit Button's Popup Menu
        public void createEditButtonPopup(View v) {
            PopupMenu EditMenu = new PopupMenu(this, v);
            EditMenu.getMenuInflater().inflate(R.menu.editlist_menu, EditMenu.getMenu());
            EditMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    String choice = new String((String) item.getTitle());
                    if (choice.equals("Edit List Name")) {
                        editListDialog();
                    }
                    else if (choice.equals("Clear All Items")) {
                        Toast.makeText(getApplicationContext(), "All list items deleted.",
                                Toast.LENGTH_SHORT).show();
                    }
                    else if (choice.equals("Delete List")) {
                        TextView text = (TextView) findViewById(R.id.ListName);
                        text.setText("Grocery Runner");
                        Toast.makeText(getApplicationContext(), "\"" + text.getText().toString() + "\" list edited.",
                                Toast.LENGTH_SHORT).show();
                    }
                    return false;
                }
            });
            EditMenu.show();
        }

        // Create List Dialog
        public AlertDialog.Builder dialogBuilder;
        private void createListDialog() {
            dialogBuilder = new AlertDialog.Builder(this);
            final EditText textInput = new EditText(this);
            dialogBuilder.setTitle("Create new list");
            dialogBuilder.setMessage("Name your list: ");
            dialogBuilder.setView(textInput);
            dialogBuilder.setPositiveButton("Create", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    TextView text = (TextView) findViewById(R.id.ListName);
                    text.setText(textInput.getText().toString());
                    Toast.makeText(getApplicationContext(), "\"" + textInput.getText().toString() + "\" list created.",
                            Toast.LENGTH_SHORT).show();

                    //add list to ListsButton
                }
            });
            dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(getApplicationContext(), "Cancelled.",
                            Toast.LENGTH_SHORT).show();
                }
            });
            // Output
            AlertDialog dialogue = dialogBuilder.create();
            dialogue.show();
        }

        // Create Group Dialog
        private void createGroupDialog() {
            dialogBuilder = new AlertDialog.Builder(this);
            final EditText textInput = new EditText(this);
            dialogBuilder.setTitle("Create new group");
            dialogBuilder.setMessage("Name your group: ");
            dialogBuilder.setView(textInput);
            dialogBuilder.setPositiveButton("Create", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    createGroup(textInput.getText().toString());
                    Toast.makeText(getApplicationContext(), "\"" + textInput.getText().toString() + "\" group created.",
                            Toast.LENGTH_SHORT).show();
                }
            });
            dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(getApplicationContext(), "Cancelled.",
                            Toast.LENGTH_SHORT).show();
                }
            });
            // Output
            AlertDialog dialogue = dialogBuilder.create();
            dialogue.show();
        }

        public void createGroup(String inputGroup){
            listDataHeader = new ArrayList<String>();
            listDataChild = new HashMap<String, List<String>>();
            // Adding child data group
            listDataHeader.add(inputGroup);
            // Adding child data items
            List<String> group = new ArrayList<String>();
            Integer groupIndex = listDataHeader.indexOf(inputGroup);
            listDataChild.put(listDataHeader.get(groupIndex), group);
        }

        // Create List Dialog
        private void editListDialog() {
            dialogBuilder = new AlertDialog.Builder(this);
            final EditText textInput = new EditText(this);
            dialogBuilder.setTitle("Edit list name");
            dialogBuilder.setMessage("Name your list: ");
            dialogBuilder.setView(textInput);
            dialogBuilder.setPositiveButton("Create", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    TextView text = (TextView) findViewById(R.id.ListName);
                    text.setText(textInput.getText().toString());
                    Toast.makeText(getApplicationContext(), "\"" + textInput.getText().toString() + "\" list edited.",
                            Toast.LENGTH_SHORT).show();

                }
            });
            dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(getApplicationContext(), "Cancelled.",
                            Toast.LENGTH_SHORT).show();
                }
            });
            // Output
            AlertDialog dialogue = dialogBuilder.create();
            dialogue.show();
        }


}


ExpandableListAdapter.java:

package com.example.groceryrunnerv4;

import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Context _context;
    private List<String> _listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap<String, List<String>> _listDataChild;

    public ExpandableListAdapter(Context context, List<String> listDataHeader,
            HashMap<String, List<String>> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosititon);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {

        final String childText = (String) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item, null);
        }

        TextView txtListChild = (TextView) convertView
                .findViewById(R.id.lblListItem);

        txtListChild.setText(childText);
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return this._listDataHeader.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group, null);
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

}

最佳答案

getExpandableListView().setOnItemLongClickListener(new OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
            int groupPosition = ExpandableListView.getPackedPositionGroup(id);
            int childPosition = ExpandableListView.getPackedPositionChild(id);

            // You now have everything that you would as if this was an OnChildClickListener()
            // Add your logic here.

            // Return true as we are handling the event.
            return true;
        }

        return false;
    }
});


来自Android: long click on the child views of a ExpandableListView?

07-24 09:49