那麼,您還可以在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));
}
}
謝謝,不敢相信我錯過了:) –