ListView onItemLongClick 弹出ContextMenu

简介: ListView onItemLongClick 弹出ContextMenu

开发了一个简单的功能:

长按ListView中的item后, 弹出选择菜单:


public class NetworktoolActivity extends Activity implements View.OnClickListener, AdapterView.OnItemLongClickListener{
    boolean isSendItem = false;
    int selectPos = -1;
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        isSendItem = tranAdapter.isSendItem(position);
        selectPos = position;
        registerForContextMenu(view);
        openContextMenu(view);
        unregisterForContextMenu(view);
        return false;
    }
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        menu.add("COPY");
        if(isSendItem)menu.add("RETRY");
        else menu.add("SAVE AS FILE");
        super.onCreateContextMenu(menu, v, menuInfo);
    }
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        if(item.getTitle().equals("COPY")){
            ClipboardManager cm = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
            cm.setPrimaryClip(ClipData.newPlainText(TAG, tranAdapter.getContent(selectPos)));
        }else if(item.getTitle().equals("RETRY")){
            etUrl.setText(tranAdapter.getContent(selectPos));
            onClick(btGo);
        }else if(item.getTitle().equals("SAVE AS FILE")){
            saveAsFile(tranAdapter.getItem(selectPos));
        }
        return true;
    }


出了异常:


02-08 14:26:04.733 11061-11061 E/AndroidRuntime: FATAL EXCEPTION: main
  java.lang.StackOverflowError
  at android.view.View.showContextMenu(View.java:4277)
  at android.app.Activity.openContextMenu(Activity.java:2879)
  at com.ansondroider.networktool.NetworktoolActivity.onItemLongClick(NetworktoolActivity.java:89)
  at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:2856)
  at android.view.View.showContextMenu(View.java:4277)
  at android.app.Activity.openContextMenu(Activity.java:2879)
  at com.ansondroider.networktool.NetworktoolActivity.onItemLongClick(NetworktoolActivity.java:89)
  at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:2856)
  at android.view.View.showContextMenu(View.java:4277)
  at android.app.Activity.openContextMenu(Activity.java:2879)
  at com.ansondroider.networktool.NetworktoolActivity.onItemLongClick(NetworktoolActivity.java:89)
  at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:2856)
  at android.view.View.showContextMenu(View.java:4277)
  at android.app.Activity.openContextMenu(Activity.java:2879)
  at com.ansondroider.networktool.NetworktoolActivity.onItemLongClick(NetworktoolActivity.java:89)
  at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:2856)
  at android.view.View.showContextMenu(View.java:4277)
  at android.app.Activity.openContextMenu(Activity.java:2879)
  at com.ansondroider.networktool.NetworktoolActivity.onItemLongClick(NetworktoolActivity.java:89)
  at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:2856)
  at android.view.View.showContextMenu(View.java:4277)
  at android.app.Activity.openContextMenu(Activity.java:2879)
  at com.ansondroider.networktool.NetworktoolActivity.onItemLongClick(NetworktoolActivity.java:89)
  at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:2856)
  at android.view.View.showContextMenu(View.java:4277)
  at android.app.Activity.openContextMenu(Activity.java:2879)
  at com.ansondroider.networktool.NetworktoolActivity.onItemLongClick(NetworktoolActivity.java:89)
  at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:2856)
  at android.view.View.showContextMenu(View.java:4277)
  at android.app.Activity.openContextMenu(Activity.java:2879)
  at com.ansondroider.networktool.NetworktoolActivity.onItemLongClick(NetworktoolActivity.java:89)
  at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:2856)
  at android.view.View.showContextMenu(View.java:4277)
  at android.app.Activity.openContextMenu(Activity.java:2879)
  at com.ansondroider.networktool.NetworktoolActivity.onItemLongClick(NetworktoolActivity.java:89)
  at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:2856)
  at android.view.View.showContextMenu(View.java:4277)
  at android.app.Activity.openContextMenu(Activity.java:2879)
  at com.ansondroider.networktool.NetworktoolActivity.onItemLongClick(NetworktoolActivity.java:89)
  at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:2856)
  at android.view.View.showContextMenu(View.java:4277)
  at android.app.Activity.openContextMenu(Activity.java:2879)
  at com.ansondroider.networktool.NetworktoolActivity.onItemLongClick(NetworktoolActivity.java:89)
  at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:2856)
  at android.view.View.showContextMenu(View.java:4277)
  at android.app.Activity.openContextMenu(Activity.java:2879)
  at com.ansondroider.networktool.NetworktoolActivity.onItemLongClick(NetworktoolActivity.java:89)
  at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:2856)
  at android.view.View.showContextMenu(View.java:4277)
  at android.app.Activity.openContextMenu(Activity.java:2879)
  at com.ansondroider.networktool.NetworktoolActivity.onItemLongClick(NetworktoolActivity.java:89)
  at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:2856)
  at android.view.View.showContextMenu(View.java:4277)
  at android.app.Activity.openContextMenu(Activity.java:2879)
  at com.ansondroider.networktool.NetworktoolActivity.onItemLongClick(NetworktoolActivity.java:89)
  at android.


修正代码如下:


public class NetworktoolActivity extends Activity implements View.OnClickListener, AdapterView.OnItemLongClickListener{
    String TAG = "NetworktoolActivity";
    EditText etUrl;
    Button btGo;
    ListView lvInfos;
    TransactionAdapter tranAdapter;
    H h;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_networktool);
        init();
    }
    void init(){
        etUrl = (EditText)findViewById(R.id.etUrl);
        btGo = (Button)findViewById(R.id.btGo);
        btGo.setOnClickListener(this);
        lvInfos = (ListView)findViewById(R.id.lvInfo);
        lvInfos.setOnItemLongClickListener(this);
        tranAdapter = new TransactionAdapter(this);
        lvInfos.setAdapter(tranAdapter);
        //关键CODE
        registerForContextMenu(lvInfos);
    }
    @Override
    public void onClick(View v) {
        if(v == btGo){
            startNewRequest(etUrl.getText().toString());
        }
    }
    void startNewRequest(final String url){
        new Thread(){
            @Override
            public void run() {
                Transaction t = new Transaction();
                t.id = t.hashCode();
                t.url = url;
                t.reqTime = TimeUtils.getTimeString();
                t.res = "waiting...";
                Message msg = h.obtainMessage(H.MSG_ADD, t);
                h.sendMessage(msg);
                byte[] content = HttpUtilsAndroid.getByteContentFromURL(url);
                t.resTime = TimeUtils.getTimeString();
                t.rawData = content;
                t.res = content != null ? new String(content) : "ERROR";
                h.sendEmptyMessage(H.MSG_UPDATE);
            }
        }.start();
    }
    boolean isSendItem = false;
    int selectPos = -1;
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        isSendItem = tranAdapter.isSendItem(position);
        selectPos = position;
        //----------递归来源----------
        //registerForContextMenu(view);
        //openContextMenu(view);
        //unregisterForContextMenu(view);
        return false;
    }
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        menu.add("COPY");
        if(isSendItem)menu.add("RETRY");
        else menu.add("SAVE AS FILE");
        super.onCreateContextMenu(menu, v, menuInfo);
    }
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        if(item.getTitle().equals("COPY")){
            ClipboardManager cm = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
            cm.setPrimaryClip(ClipData.newPlainText(TAG, tranAdapter.getContent(selectPos)));
        }else if(item.getTitle().equals("RETRY")){
            etUrl.setText(tranAdapter.getContent(selectPos));
            onClick(btGo);
        }else if(item.getTitle().equals("SAVE AS FILE")){
            saveAsFile(tranAdapter.getItem(selectPos));
        }
        return true;
    }


相关文章
|
22天前
|
XML Java Android开发
Android控件之基础控件——进度条类的view——TextView、Checkbox复选控件、RadioButton单选控件、ToggleButton开关、SeekBar拖动条、menu、弹窗
Android控件之基础控件——进度条类的view——TextView、Checkbox复选控件、RadioButton单选控件、ToggleButton开关、SeekBar拖动条、menu、弹窗
|
2月前
toolbar设置返回键以及点击事件
toolbar设置返回键以及点击事件
11 0
UWP 取消GridView、ListView鼠标选中、悬停效果
原文:UWP 取消GridView、ListView鼠标选中、悬停效果 因为经常碰到ListView或者ListBox之类的选中、鼠标悬停样式和自己设置的主题颜色不搭,这时就需要改变这些样式了. 而这里我通过ListView来说明,大致思路其实就是重新定义Item的Template。
1152 0
|
Android开发 Java 容器