2016-10-28 117 views
2

我試圖創建一個視圖,可以刷卡解散,但它可以撤消。這些物品在FrameView內有背景和前景。我在ItemTouchHelper.Callback覆蓋onChildDraw這樣的:onChildDraw導致項目閃爍

@Override 
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, 
         float dX, float dY, int actionState, boolean isCurrentlyActive) { 
    if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) { 
     if (viewHolder instanceof ItemViewHolder) { 
      ItemViewHolder holder = (ItemViewHolder) viewHolder; 

      holder.foreground.setTranslationX(dX); 
     } 
    } else { 
     super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); 
    } 
} 

一切正常,直到我嘗試基於用戶動作這樣撤消刷卡動作:

foreground.animate() 
      .translationX(0) 
      .setDuration(150); 

它確實帶來實際的視圖回它的位置,但如果用戶試圖再次滑動該項目,則會再次顯示背景,並且出於某種原因,使用大於屏幕寬度的dX參數調用onChildDraw。點擊該項目可以顯示一秒鐘的背景視圖,然後通常可以忽略前景。我怎樣才能解決這個問題?

+0

你有沒有解決問題隊友? –

+0

我已經添加了答案 – tofiffe

回答

1

解決方案是使用getDefaultUIUtil()函數ItemTouchHelper.Callback。工作代碼如下:

@Override 
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, 
         float dX, float dY, int actionState, boolean isCurrentlyActive) { 
    if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) { 
     if (viewHolder instanceof TrackerListAdapter.ItemViewHolder) { 
      TrackerListAdapter.ItemViewHolder holder = 
        (TrackerListAdapter.ItemViewHolder) viewHolder; 

      getDefaultUIUtil().onDraw(c, recyclerView, holder.foreground, dX, dY, actionState, isCurrentlyActive); 
     } 
    } else { 
     super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); 
    } 
}