android如何在gridView

时间:2016-06-27 11:11:24

标签: android gridview ontouchlistener onlongclicklistener

我正在使用itemlongclicklistener来操作gridview中的项目。 我想用触摸监听器改变longclick。 这是我的代码

//drag artifact on long clic
        OnItemLongClickListener myOnItemLongClickListener = new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view,
                                           int position, long id) {

                Artifact selectedItem = (Artifact) (parent.getItemAtPosition(position));
                ArtifactAdapter associatedAdapter = (ArtifactAdapter) (parent.getAdapter());
                List<Artifact> associatedList = associatedAdapter.getList();

                PassObject passObj = new PassObject(view, selectedItem, associatedList, position);

                ClipData data = ClipData.newPlainText("", "");
                View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
                view.startDrag(data, shadowBuilder, passObj, 0);
                view.setVisibility(View.INVISIBLE);
                zPLayout.setVisibility(View.VISIBLE);
                if (associatedList == listArtifact) {
                    trashEditLayout.setVisibility(View.VISIBLE);
                    optionsArtifactLayout.setVisibility(View.GONE);
                }

                return true;

            }

        };

我该怎么做?

1 个答案:

答案 0 :(得分:0)

试试这个

private static final int MIN_CLICK_DURATION = 600;
private long startClickTime;
private boolean longClickActive;

your_view.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:

              if (longClickActive == false) {
                                longClickActive = true;
                                startClickTime = Calendar.getInstance().getTimeInMillis();
                            }
            break;

        case MotionEvent.ACTION_UP:

             if (longClickActive == true) {
                                long clickDuration = Calendar.getInstance().getTimeInMillis() - startClickTime;
                                if (clickDuration >= MIN_CLICK_DURATION) {
                                    longClickActive = false;
                                    // long click detected here 
                                }
             }

            break;
       case MotionEvent.ACTION_UP:

             longClickActive = false;

            break;
        default:
            break;
        }
        return true;
    }

});