2017-04-20 29 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 

    } 
}