Android 如何在RecyclerView的每个网格之间添加分隔符?
我需要在RecyclerView的每个网格之间添加一个分隔符Android 如何在RecyclerView的每个网格之间添加分隔符?,android,android-recyclerview,divider,Android,Android Recyclerview,Divider,我需要在RecyclerView的每个网格之间添加一个分隔符 RecyclerView recyclerView= (RecyclerView) profileView.findViewById(R.id.profile_recycler_view); StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL); recycler
RecyclerView recyclerView= (RecyclerView) profileView.findViewById(R.id.profile_recycler_view);
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
Adapter Adapter = new Adapter(getActivity());
recyclerView.setAdapter(profileAdapter);
请帮帮我
例子:
你需要
装饰
。以下是一个例子:
public class ItemOffsetDecoration extends RecyclerView.ItemDecoration {
private int offset;
public ItemOffsetDecoration(int offset) {
this.offset = offset;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
outRect.left = offset;
outRect.right = offset;
outRect.bottom = offset;
if(parent.getChildAdapterPosition(view) == 0) {
outRect.top = offset;
}
}
}
在你的活动/片段中
grid.setLayoutManager(new GridLayoutManager(this, 2));
grid.addItemDecoration(new ItemOffsetDecoration(1));
为此,您需要
装饰。
以下是一个例子:
public class ItemOffsetDecoration extends RecyclerView.ItemDecoration {
private int offset;
public ItemOffsetDecoration(int offset) {
this.offset = offset;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
outRect.left = offset;
outRect.right = offset;
outRect.bottom = offset;
if(parent.getChildAdapterPosition(view) == 0) {
outRect.top = offset;
}
}
}
在你的活动/片段中
grid.setLayoutManager(new GridLayoutManager(this, 2));
grid.addItemDecoration(new ItemOffsetDecoration(1));
我想,Jagadesh Seeram说的是项目之间的水平分隔符,但是RecyclerView.ItemDecoration
到处添加分隔符
如果未对某些项目使用autoscroll,则应这样编写:
public class ItemOffsetDecoration extends RecyclerView.ItemDecoration {
private int offset;
public ItemOffsetDecoration(int offset) {
this.offset = offset;
}
@Override
public void getItemOffsets(Rect outRect, View view,
RecyclerView parent, RecyclerView.State state) {
int bottonIndex;
if (parent.getAdapter().getItemCount() % 2 == 0){
bottonIndex = parent.getAdapter().getItemCount() - 2;
} else {
bottonIndex = parent.getAdapter().getItemCount() - 1;
}
if (parent.getChildAdapterPosition(view) < bottonIndex){
outRect.bottom = offset;
} else {
outRect.bottom = 0;
}
if(parent.getChildAdapterPosition(view) > 1 ) {
outRect.top = offset;
} else {
outRect.top = 0;
}
if( (parent.getChildAdapterPosition(view) % 2 ) == 0) {
outRect.right = offset;
outRect.left = 0;
} else {
outRect.right = 0;
outRect.left = offset;
}
}
公共类ItemOffsetDecoration扩展了RecyclerView.ItemDecoration{
私有整数偏移;
公共项目抵销装饰(内部抵销){
这个偏移量=偏移量;
}
@凌驾
public void getItemOffset(Rect-outRect、View-View、,
RecyclerView父级,RecyclerView.State(状态){
int-bottonidex;
if(parent.getAdapter().getItemCount()%2==0){
bottonIndex=parent.getAdapter().getItemCount()-2;
}否则{
bottonIndex=parent.getAdapter().getItemCount()-1;
}
if(parent.getChildAdapterPosition(视图)1){
outRect.top=偏移量;
}否则{
outRect.top=0;
}
if((父.getChildAdapterPosition(视图)%2)=0){
outRect.right=偏移量;
outRect.left=0;
}否则{
outRect.right=0;
outRect.left=偏移量;
}
}
我想,Jagadesh Seeram说的是物品之间的水平分隔,但是RecyclerView.itemEdition
到处都添加分隔符
如果未对某些项目使用autoscroll,则应这样编写:
public class ItemOffsetDecoration extends RecyclerView.ItemDecoration {
private int offset;
public ItemOffsetDecoration(int offset) {
this.offset = offset;
}
@Override
public void getItemOffsets(Rect outRect, View view,
RecyclerView parent, RecyclerView.State state) {
int bottonIndex;
if (parent.getAdapter().getItemCount() % 2 == 0){
bottonIndex = parent.getAdapter().getItemCount() - 2;
} else {
bottonIndex = parent.getAdapter().getItemCount() - 1;
}
if (parent.getChildAdapterPosition(view) < bottonIndex){
outRect.bottom = offset;
} else {
outRect.bottom = 0;
}
if(parent.getChildAdapterPosition(view) > 1 ) {
outRect.top = offset;
} else {
outRect.top = 0;
}
if( (parent.getChildAdapterPosition(view) % 2 ) == 0) {
outRect.right = offset;
outRect.left = 0;
} else {
outRect.right = 0;
outRect.left = offset;
}
}
公共类ItemOffsetDecoration扩展了RecyclerView.ItemDecoration{
私有整数偏移;
公共项目抵销装饰(内部抵销){
这个偏移量=偏移量;
}
@凌驾
public void getItemOffset(Rect-outRect、View-View、,
RecyclerView父级,RecyclerView.State(状态){
int-bottonidex;
if(parent.getAdapter().getItemCount()%2==0){
bottonIndex=parent.getAdapter().getItemCount()-2;
}否则{
bottonIndex=parent.getAdapter().getItemCount()-1;
}
if(parent.getChildAdapterPosition(视图)1){
outRect.top=偏移量;
}否则{
outRect.top=0;
}
if((父.getChildAdapterPosition(视图)%2)=0){
outRect.right=偏移量;
outRect.left=0;
}否则{
outRect.right=0;
outRect.left=偏移量;
}
}
请阅读问题..它会将分隔符添加到每个项目中。我不需要。我需要如图所示的分隔符。好的,我理解。您不能复制/粘贴此代码,但请注意您可以定义哪个组件将受到装饰的影响。但是如果我没说错,您希望中间有一条黑线?如果是c如果我会为你写解决方案。请阅读问题。它会将分隔符添加到每个项目中。我不想要那样。我需要如图所示的分隔符。好的,我明白。你不能复制/粘贴此代码,但请注意你可以定义哪个组件将受到装饰的影响。但是如果我做对了,你想要黑色l中间的线?如果是这样的话,我会给你写解决方案。那么你想要一条黑线作为分隔线?那么你想要一条黑线作为分隔线?