2011-08-26 108 views
1

我是一名android初學者。 我正在做一個Android項目,它有一個函數來重新排列列表中的東西。 我發現了一個開源的https://github.com/commonsguy/cwac-touchlist#readme 和模塊名稱是CWAC:TouchListViewAndroid拖放ListView問題

但我的執行過程中,我有一些問題,希望有人能幫助我,

  1. 我想將關斷刪除函數當我在水平移動列表項,但我不能... 如果我評論在TouchListView.onTouchEvent()的情況下移除代碼MotionEvent.ACTION_CANCEL 它會發生意想不到的行爲。

  2. 另外,我想在拖動海豚瀏覽器書籤頁這樣的項目時有一些動畫,但我不知道它應該實現DragListener嗎?

但是,我修正了一個錯誤。

@Override 
    public boolean onTouchEvent(MotionEvent ev) { 
     if (mGestureDetector != null) { 
      mGestureDetector.onTouchEvent(ev); 
     } 
     if ((mDragListener != null || mDropListener != null) && mDragView != null) { 
      int action = ev.getAction(); 
      switch (action) { 
      case MotionEvent.ACTION_UP: 
      case MotionEvent.ACTION_CANCEL: 
       Rect r = mTempRect; 
       mDragView.getDrawingRect(r); 
       stopDragging(); 

       if (mRemoveMode == SLIDE_RIGHT && ev.getX() > r.left + (r.width() * 3/4)) { 
        if (mRemoveListener != null) { 
         mRemoveListener.remove(mFirstDragPos); 
        } 
        unExpandViews(true); 
       } else if (mRemoveMode == SLIDE_LEFT && ev.getX() < r.left + (r.width()/4)) { 
        if (mRemoveListener != null) { 
         mRemoveListener.remove(mFirstDragPos);      
        } 
        unExpandViews(true); 
       } else { 
        if (mDropListener != null && mDragPos >= 0 && mDragPos < getCount() - 1) { 
         mDropListener.drop(mFirstDragPos, mDragPos); 
        } 
        unExpandViews(false); 
       } 
       break; 

      case MotionEvent.ACTION_DOWN: 
      case MotionEvent.ACTION_MOVE: 
       int x = (int) ev.getX(); 
       int y = (int) ev.getY(); 
       dragView(x, y); 
       int itemnum = getItemForPosition(y); 
       if (itemnum >= 0) { 
        if (action == MotionEvent.ACTION_DOWN || itemnum != mDragPos) { 
         if (mDragListener != null) { 
          mDragListener.drag(mDragPos, itemnum); 
         } 
         mDragPos = itemnum; 
         doExpansion(); 
        } 
        int speed = 0; 
        adjustScrollBounds(y); 
        if (y > mLowerBound) { 
         // scroll the list up a bit 
         speed = y > (mHeight + mLowerBound)/2 ? 16 : 4; 
        } else if (y < mUpperBound) { 
         // scroll the list down a bit 
         speed = y < mUpperBound/2 ? -16 : -4; 
        } 
        if (speed != 0) { 
         int ref = pointToPosition(0, mHeight/2); 
         if (ref == AdapterView.INVALID_POSITION) { 
          // we hit a divider or an invisible view, check 
          // somewhere else 
          ref = pointToPosition(0, mHeight/2 + getDividerHeight() + 64); 
         } 
         View v = getChildAt(ref - getFirstVisiblePosition()); 
         if (v != null) { 
          int pos = v.getTop(); 
          setSelectionFromTop(ref, pos - speed);       
         } 
        } 
       } 
       break; 
      } 
      return true; 
     } 
     return super.onTouchEvent(ev); 
    } 

對於MotionEvent.ACTION_CANCEL,如果我拖動項目在list.getCount,它會拋出異常的情況,所以我更換條件從

if (mDropListener != null && mDragPos >= 0 && mDragPos < getCount()) { 
     mDropListener.drop(mFirstDragPos, mDragPos); 
} 

if (mDropListener != null && mDragPos >= 0 && mDragPos < getCount() - 1) { 
     mDropListener.drop(mFirstDragPos, mDragPos); 
} 

然後,異常將被修復。

任何人都可以幫助我? 非常感謝。

回答

0

我想將關斷remove函數當我移動在水平列表中的項目,但我不能

使用自定義remove_mode屬性與"none"值。例如:

<?xml version="1.0" encoding="utf-8"?> 
<com.commonsware.cwac.tlv.TouchListView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tlv="http://schemas.android.com/apk/res/com.commonsware.cwac.tlv.demo" 

    android:id="@android:id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:drawSelectorOnTop="false" 
    tlv:normal_height="64dip" 
    tlv:grabber="@+id/icon" 
    tlv:remove_mode="none" 
/> 

我想拖着像海豚瀏覽器書籤頁的項目中有一些動畫,但我不知道是不是

我將不能夠幫助你那個,對不起。

+0

哦,謝謝。 我現在可以關閉移除功能。 我會嘗試實現動畫。 如果我可以實現此功能,我可以發佈解決方案。 – Rebecca