2017-04-14 71 views
0

您將如何根據他們在屏幕上的位置給水平RecyclerView中的物品垂直偏移?當用戶向左或向右滾動時,我希望物體在靠近屏幕的兩端時接近中間位置時降低。RecyclerView物品垂直填充取決於他們的位置

這是我要去的效果圖。藍色表示左右滾動。紅色表示每個項目根據其在屏幕上的位置的垂直偏移量。當用戶向左或向右滾動時,我希望他們根據位置平穩地上升和下降。

enter image description here

回答

1

你必須創建自定義ItemDecoration,這看起來就像是這樣的:

public class MyItemDecoration extends RecyclerView.ItemDecoration { 

    @Override 
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { 
     final int childCount = parent.getAdapter().getItemCount(); 
     final int center = childCount >> 1; 
     final int currentPosition = parent.getChildAdapterPosition(view); 
     if (currentPosition < center) { 
      outRect.set(0, 0, 0, currentPosition * 10); 
     } else { 
      outRect.set(0, 0, 0, (childCount - currentPosition) * 10); 
     } 
    } 
} 

使用

recyclerView.addItemDecoration(new MyItemDecoration()); 

結果:

enter image description here

+0

非常好!它很棒!非常感謝你! – MarkInTheDark