2017-04-20 59 views

回答

3

你可以試試下面給出一個lib中,你只需要一些修改

https://github.com/daimajia/AndroidSwipeLayout

謝謝!

+0

它工作在兩邊? –

+0

是的,我已經在其工作前雙方使用這個 –

+0

在哪裏我可以添加兩側滑動我沒有找到我可以添加的地方 –

0

你需要的東西是這樣的:

@Override 
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { 
int position = viewHolder.getAdapterPosition(); 

if (direction == ItemTouchHelper.LEFT){ 
    Log.i("ItemSwipe", "LEFT"); 
} 
else if (direction == ItemTouchHelper.RIGHT){ 
    Log.i("ItemSwipe", "RIGHT"); 
} 
} 
+0

onSwiped哪個類是方法? –

0

下面是工作的代碼創建自定義監聽SyncedScrollListener並將其分配給兩個recycleview。

rv1.setOnScrollListener(new SyncedScrollListener(rv2)); 
    rv2.setOnScrollListener(new SyncedScrollListener(rv1)); 


public class SyncedScrollListener implements AbsListView.OnScrollListener { 
    int offset; 
    int oldVisibleItem = -1; 
    int currentHeight; 
    int prevHeight; 
    private View mSyncedView; 

    public SyncedScrollListener(View syncedView) { 

     if (syncedView == null) { 
      throw new IllegalArgumentException("syncedView is null"); 
     } 

     mSyncedView = syncedView; 
    } 

    public void onScroll(AbsListView view, int firstVisibleItem, 
         int visibleItemCount, int totalItemCount) { 

     int[] location = new int[2]; 

     if (visibleItemCount == 0) { 
      return; 
     } 

     if (oldVisibleItem != firstVisibleItem) { 

      if (oldVisibleItem < firstVisibleItem) { 
       prevHeight = currentHeight; 
       currentHeight = view.getChildAt(0).getHeight(); 

       offset += prevHeight; 

      } else { 
       currentHeight = view.getChildAt(0).getHeight(); 

       View prevView; 
       if ((prevView = view.getChildAt(firstVisibleItem - 1)) != null) { 
        prevHeight = prevView.getHeight(); 
       } else { 
        prevHeight = 0; 
       } 

       offset -= currentHeight; 
      } 

      oldVisibleItem = firstVisibleItem; 
     } 

     view.getLocationOnScreen(location); 
     int listContainerPosition = location[1]; 

     view.getChildAt(0).getLocationOnScreen(location); 
     int currentLocation = location[1]; 

     int blah = listContainerPosition - currentLocation + offset; 

     mSyncedView.scrollTo(0, blah); 

    } 

    public void onScrollStateChanged(AbsListView view, int scrollState) { 
     // TODO Auto-generated method stub 

    } 
}