2012-03-15 14 views
0

下面的示例使我的Expandable列表消失,只要我設置OnTouch偵聽器或onCLick偵聽器。Android ExpandableLists,實現手勢,設置onTouch偵聽器使可擴展列表消失

我有一個ActivityswipeDetector類:

public class ActivitySwipeDetector implements View.OnTouchListener { 

public static enum Action { 
    LR, // Left to Right 
    RL, // Right to Left 
    TB, // Top to bottom 
    BT, // Bottom to Top 
    None // when no action was detected 
} 


private static final String logTag = "SwipeDetector"; 
private static final int MIN_DISTANCE = 100; 
private float downX, downY, upX, upY; 
private Action mSwipeDetected = Action.None; 


public boolean swipeDetected(){ 
    return mSwipeDetected != Action.None; 
} 

public Action getAction(){ 
    return mSwipeDetected; 
} 


public boolean onTouch(View v, MotionEvent event) { 
    switch (event.getAction()) { 
    case MotionEvent.ACTION_DOWN: { 
     downX = event.getX(); 
     downY = event.getY(); 
     mSwipeDetected = Action.None; 
     return false; // allow other events like Click to be processed 
    } 
    case MotionEvent.ACTION_UP: { 
     upX = event.getX(); 
     upY = event.getY(); 

     float deltaX = downX - upX; 
     float deltaY = downY - upY; 

     // horizontal swipe detection 
       if (Math.abs(deltaX) > MIN_DISTANCE) { 
        // left or right 
        if (deltaX < 0) { 
         Log.i(logTag, "Swipe Left to Right"); 
         mSwipeDetected = Action.LR; 
         return false; 
        } 
        if (deltaX > 0) { 
         Log.i(logTag, "Swipe Right to Left"); 
         mSwipeDetected = Action.RL; 
         return false; 
        } 
       } else 

       // vertical swipe detection 
       if (Math.abs(deltaY) > MIN_DISTANCE) { 
        // top or down 
        if (deltaY < 0) { 
         Log.i(logTag, "Swipe Top to Bottom"); 
         mSwipeDetected = Action.TB; 
         return false; 
        } 
        if (deltaY > 0) { 
         Log.i(logTag, "Swipe Bottom to Top"); 
         mSwipeDetected = Action.BT; 
         return false; 
        } 
       } 
       return false; 
    } 
    } 
    return false; 
} 

}

我的主類中我加在頂部以下的的onCreate方法 - 但前兩行進行擴展列表dissapear:

final ActivitySwipeDetector swipeDetector = new ActivitySwipeDetector(); 
     list.setOnTouchListener(swipeDetector); 

     list.setOnItemClickListener(new OnItemClickListener() { 

       public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
         if (swipeDetector.swipeDetected()){ 
          // do the onSwipe action 
         } else { 
          // do the onItemClick action 
         } 
        } 
           }); 
      list.setOnItemLongClickListener(new OnItemLongClickListener() { 
       public boolean onItemLongClick(AdapterView<?> parent, View view,int position, long id) { 
        if (swipeDetector.swipeDetected()){ 
         // do the onSwipe action 
         return true; 
        } else { 
         // do the onItemLongClick action 
         return false; 
        } 
       } 
      }); 

「list」被定義爲list = getExpandableListView();

所以1)我在做什麼錯,使擴展列表dissapear

2)我應該使用onGroupClick聽衆和onChildClick聽衆?如果是這樣,請告訴我在哪裏添加他們

3)它說「做滑動動作」和「做OnItemClick動作」,我該添加什麼?

我用左到右揮筆在列表上的項目擴大

從右到左揮筆回去了,我有一個爲具有代碼已經這麼一個按鈕i應該用我假設的刷卡來代替?

任何幫助將不勝感激!

Ž

回答

1

發現錯誤

我加在主類中的聽衆前我已經設置列表= getExpandableListView();

當我插入聽衆後,它工作正常!