2013-01-18 26 views
40

我正在開發一個應用程序與列表視圖中的店鋪列表。當我將listview的項目向右(或向左)滑動時,我需要這個項目應該從列表視圖中刪除。刪除與幻燈片項目列表視圖 - 像Gmail

我有我的列表視圖,只需要執行它的功能。

在此先感謝。

+0

就像平板電腦一樣,您可以通過將任務從當前任務列表中滑動到左側或右側來刪除任務。 –

+0

這裏是一個鏈接,可以顯示/指導你如何做到這一點:[刷卡解散](https://gist.github.com/2980593) 。 – hardartcore

回答

19

這就是我如何實現這種效果。我們有一個ListView lvSimple,我們添加onTouchListener到我們的lvSimple。這是我的工作代碼。

float historicX = Float.NaN, historicY = Float.NaN; 
static final int DELTA = 50; 
enum Direction {LEFT, RIGHT;} 
... 
ListView lvSimple = (ListView) findViewById(R.id.linLayout); 
... 
lvSimple.setOnTouchListener(new OnTouchListener() { 
    @Override 
    public boolean onTouch(View v, MotionEvent event) { 

     switch (event.getAction()) { 
      case MotionEvent.ACTION_DOWN: 
       historicX = event.getX(); 
       historicY = event.getY(); 
       break; 

      case MotionEvent.ACTION_UP: 
       if (event.getX() - historicX < -DELTA) { 
        FunctionDeleteRowWhenSlidingLeft(); 
        return true; 
       } 
       else if (event.getX() - historicX > DELTA) { 
        FunctionDeleteRowWhenSlidingRight(); 
        return true; 
       } 
       break; 

      default: 
       return false; 
     } 
     return false; 
    } 
}); 

其中功能FunctionDeleteRowWhenSlidingLeft()時,當我們向左滑動呼籲,FunctionDeleteRowWhenSlidingRight() - 分別權。在這個功能中,你需要粘貼動畫代碼。

+1

Ohhhh,經過兩天的搜索和調試,它節省了我! 謝謝 –

+6

但如何獲得幻燈片的索引?! –

+1

list.setOnItemClickListener(新AdapterView.OnItemClickListener(){ 公共無效onItemClick(適配器視圖父視圖V,INT位置,長ID){ // DO的東西在這裏 SwipedPosition =位置; } }); – pukingminion

6

Answer Android-Developer指向Roman Nurik的代碼gist.github.com。此代碼已過時。他在他的開源項目Dash Clock中使用這個Swipe來關閉監聽器。

在使用Gist.github.com中的代碼之前,您應該知道一些事情。

  1. gist.Github中過時的代碼對觸摸非常敏感。如果你不斷點擊ListView中的項目,它將被刪除。在更新後的代碼中,他確定了閃電的靈敏度。
  2. 如果您在 ListView中聲明瞭分隔符,此偵聽程序無法正常工作。如果您想要分隔線,請在ListItem佈局中聲明它們。
  3. 此代碼仍在測試版。買者自負。

因此,我建議使用更新的代碼。你可以找到更新源here

+0

我正在使用在您推薦的鏈接中找到的更新後的代碼,但我在聲明它時遇到了問題。每當我打電話給該行:「新的SwipeDismissListViewTouchListener.OnDismissCallback(){」它出現了,「SwipeDismissListViewTouchListener.OnDismissCallback無法解析爲類型」任何想法?不太確定解決方案 – Silmarilos

+0

@Silmarilos使用新的SwipeDismissListViewTouchListener.DismissCallbacks()代替 – Blacklight

+0

https://github.com/romannurik/Android-SwipeToDismiss/blob/master/src/com/example/android/swipedismiss/SwipeDismissTouchListener。 java – user3167086

5

您應該考慮的另一個選擇是使用Tim Roes的EnhancedListView庫。[更新日期2015/8/1]隨着RecycleView的推出,該庫已被棄用。

前面提到的Roman Nurik的SwipeToDismiss偵聽器需要API等級12或更高。傑克沃頓移植此代碼以支持SwipeToDismissNOA中的所有API級別。

Tim Roes將此庫進一步擴展爲支持還原功能。

+0

INFO:EnhancedListView已棄用。 – iraSenthil

+0

@iraSenthil那麼你現在在使用什麼或刷卡和刪除? SwipetoDismissNOA還是別的? – Darpan

0

我用macloving寫了一個答案。目前它正在工作,但只有當你的孩子都擁有相同的身高時纔有效。

listView.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      // TODO Auto-generated method stub 
      switch (event.getAction()) { 
       case MotionEvent.ACTION_DOWN: 
        historicX = event.getX(); 
        historicY = event.getY(); 
        return false; 

       case MotionEvent.ACTION_UP: 
        if (listView.getChildAt(0) != null) { 
         int heightOfEachItem = haveListView.getChildAt(0).getHeight(); 
         int heightOfFirstItem = -haveListView.getChildAt(0).getTop() + haveListView.getFirstVisiblePosition()*heightOfEachItem; 
         //IF YOU HAVE CHILDS IN LIST VIEW YOU START COUNTING 
         //listView.getChildAt(0).getTop() will see top of child showed in screen 
         //Dividing by height of view, you get how many views are not in the screen 
         //It needs to be Math.ceil in this case because it sometimes only shows part of last view 
         final int firstPosition = (int) Math.ceil(heightOfFirstItem/heightOfEachItem); // This is the same as child #0 

         //Here you get your List position, use historic Y to get where the user went first 
         final int wantedPosition = (int) Math.floor((historicY - haveListView.getChildAt(0).getTop())/heightOfEachItem) + firstPosition; 
         //Here you get the actually position in the screen 
         final int wantedChild = wantedPosition - firstPosition; 
         //Depending on delta, go right or left 
         if (event.getX() - historicX < -DELTA) { 
          //If something went wrong, we stop it now 
          if (wantedChild < 0 || wantedChild >= List.size()|| wantedChild >= listView.getChildCount()) { 

           return true; 
          } 
          //Start animation with 500 miliseconds of time 
          listView.getChildAt(wantedChild).startAnimation(outToLeftAnimation(500)); 
          //after 500 miliseconds remove from List the item and update the adapter. 
          new java.util.Timer().schedule(
            new java.util.TimerTask() { 
             @Override 
             public void run() { 
              List.remove(wantedPosition); 
              updateAdapter(); 
             } 
            }, 
            500 
          ); 
          return true; 

         } else if (event.getX() - historicX > DELTA) { 
          //If something went wrong, we stop it now 
          if (wantedChild < 0 || wantedChild >= List.size() || wantedChild >= listView.getChildCount()) { 

           return true; 
          } 
          //Start animation with 500 miliseconds of time 
          listView.getChildAt(wantedChild).startAnimation(outToRightAnimation(500)); 
          //after 500 miliseconds remove from List the item and update the adapter. 
          new java.util.Timer().schedule(
            new java.util.TimerTask() { 
             @Override 
             public void run() { 
              List.remove(wantedPosition); 
              updateAdapter(); 
             } 
            }, 
            500 
          ); 
          return true; 

         } 
        } 
        return true; 
       default: 
        return false; 
      } 
     } 
    }); 

的動畫有這樣的功能:

private Animation outToLeftAnimation(int duration) { 
    Animation outtoLeft = new TranslateAnimation(
      Animation.RELATIVE_TO_PARENT, 0.0f, 
      Animation.RELATIVE_TO_PARENT, -1.0f, 
      Animation.RELATIVE_TO_PARENT, 0.0f, 
      Animation.RELATIVE_TO_PARENT, 0.0f); 
    outtoLeft.setDuration(duration); 
    outtoLeft.setInterpolator(new AccelerateInterpolator()); 
    return outtoLeft; 
} 

private Animation outToRightAnimation(int duration) { 
    Animation outtoRight = new TranslateAnimation(
      Animation.RELATIVE_TO_PARENT, 0.0f, 
      Animation.RELATIVE_TO_PARENT, +1.0f, 
      Animation.RELATIVE_TO_PARENT, 0.0f, 
      Animation.RELATIVE_TO_PARENT, 0.0f); 
    outtoRight.setDuration(duration); 
    outtoRight.setInterpolator(new AccelerateInterpolator()); 
    return outtoRight; 
} 

我想這一點,到現在爲止我還沒有看到錯誤的,如果有人可以嘗試也將是一件好事。