2016-02-04 104 views
3

我一直試圖在recyclerview中重疊兩個相鄰的位置,但未能這樣做。我希望第一個位置的一部分重疊/平鋪在第二個位置。recyclerview中的重疊位置/視圖

我一直在玩弄一個自定義的RecyclerView.ItemDecoration和它的getItemOffsets,但是,似乎只能添加一個位置上的填充/邊距,而不是將其翻譯到它的視圖外的新位置。

任何人都有如何用recyclerview實現這一目標的指針?

回答

6

那麼,您還可以在RecyclerView.ItemDecoration中添加負邊距


例子:

ItemDecorator.java

public class ItemDecorator extends RecyclerView.ItemDecoration { 
    private final int mSpace; 

    public ItemDecorator(int space) { 
     this.mSpace = space; 
    } 

    @Override 
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { 
     outRect.left = mSpace; 
     outRect.right = mSpace; 
     outRect.bottom = mSpace; 
     outRect.top = mSpace; 
    } 
} 

MainActivity.java

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     ArrayList<String> arrayList = new ArrayList<>(); 
     arrayList.add("Hi"); 
     arrayList.add("World"); 
     arrayList.add("What"); 

     RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rec1); 
     //Negative margin! 
     ItemDecorator itemDecorator = new ItemDecorator(-30);  
     recyclerView.setAdapter(new CustomAdapter(arrayList)); 
     recyclerView.addItemDecoration(itemDecorator); 
     recyclerView.setLayoutManager(new LinearLayoutManager(this)); 

    } 
} 
+0

謝謝,不敢相信我錯過了:) –