2016-06-27 21 views
0

Ich bin auf Itemlongclicklistener t manipulieren Elemente in meinem Gridview. Ich möchte den Longclick mit Touch-Listener ändern. dies ist mein Codeandroid, wie Element touchlistener in gridView

//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; 

      } 

     }; 

, wie kann ich das tun?

+0

Sie ItemLongClick auf Touch-Ereignis implementieren möchten? – Nisarg

Antwort

0

versuchen, wie diese

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; 
    } 

});