recyclerview 中的重叠位置/视图 [英] Overlap positions/views in recyclerview
问题描述
我一直试图在回收站视图中重叠两个相邻的位置,但没有做到.我想要第一个位置的一部分重叠/绘制在第二个位置上.
I've been trying to overlap two adjacent positions in a recyclerview, but fail to do so. I would like a part of the first position overlap/draw over the second position.
我一直在使用自定义 RecyclerView.ItemDecoration 和它的 getItemOffsets,但是它似乎只能在一个位置上添加填充/边距,而不是将它转换到它的视图父之外的新位置.
I've have been playing around with a custom RecyclerView.ItemDecoration and it's getItemOffsets, however that just seem to be able to add padding/margin on a position, not translating it to a new position outside it's viewparent.
有没有人知道如何使用回收视图实现这一点?
Anyone got any pointers of how to achieve this with a recyclerview?
推荐答案
嗯,你也可以在 RecyclerView.ItemDecoration.
Well, you can also add negative margin in a 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));
}
}
这篇关于recyclerview 中的重叠位置/视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!