enter image description here //我在xml
中有如下的回收站视图<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/toolbar" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/grid_recycle_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:background="#f8f9fb"
/>
<android.support.v7.widget.RecyclerView
android:id="@+id/list_recycle_view"
android:visibility="invisible"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</FrameLayout>
</LinearLayout>
//在我的onprepareGridView方法中,我已经完成了以下
private void prepareGridRecycleView() {
gridRecyclerView.setHasFixedSize(true);
SpaceItemDecoration itemDecoration = new SpaceItemDecoration(10);
gridRecyclerView.setClipToPadding(false);
gridRecyclerView.addItemDecoration(itemDecoration);
adapter = new ShopPageGridViewAdapter(ShopPageContent.this, new ShopPageHeaderGridInterface() {
@Override
public void onClick(boolean isClicked) {
gridRecyclerView.setVisibility(View.INVISIBLE);
listRecycleView.setVisibility(View.VISIBLE);
}
}, new ShopPageHeaderListInterface() {
@Override
public void onClick(boolean isClicked) {
}
}, new CartCoordivateInfoHolder() {
@Override
public void setViewCoordinate(ImageView view, int x, int y) {
view.setVisibility(View.VISIBLE);
view.bringToFront();
view.invalidate();
int pos[] = new int[2];
cart.getLocationOnScreen(pos);
Log.e("x", String.valueOf(pos[0]));
Log.e("y", String.valueOf(pos[1]));
TranslateAnimation anim = new TranslateAnimation(0, pos[0] - x, 0, pos[1] - y);
anim.setDuration(5000);
anim.setFillAfter(true);
view.startAnimation(anim);
}
});
final GridLayoutManager manager = new GridLayoutManager(this, 2);
manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
return adapter.isPositionHeader(position) ? manager.getSpanCount() : 1;
}
});
gridRecyclerView.setAdapter(adapter);
gridRecyclerView.setLayoutManager(manager);
}
//项目装饰器类如下所示
public class SpaceItemDecoration extends RecyclerView.ItemDecoration {
private int space;
public SpaceItemDecoration(int space) {
this.space = space;
}
@Override
public void getItemOffsets(Rect outRect, View view,
RecyclerView parent, RecyclerView.State state) {
int position = parent.getChildAdapterPosition(view);
if (position == 0) {
outRect.left = 0;
outRect.right = 0;
outRect.bottom = 0;
outRect.top = 0;
return;
}
if (parent.getChildAdapterPosition(view) == 1 || parent.getChildAdapterPosition(view) == 2) {
outRect.top = 0;
outRect.left =(space);
outRect.right = space;
outRect.bottom = space;
return;
}
outRect.left = space;[enter image description here][1]
outRect.right = space;
outRect.bottom = space;
outRect.top = space;
}
}
//我面临的问题是左侧和右侧的空间不同。左侧空间小于右侧空间。
//图片链接
答案 0 :(得分:3)
尝试:
public class SpaceItemDecoration extends RecyclerView.ItemDecoration {
private int space;
public SpaceItemDecoration(int space) {
this.space = space;
}
@Override
public void getItemOffsets(Rect outRect, View view,
RecyclerView parent, RecyclerView.State state) {
int position = parent.getChildAdapterPosition(view);
outRect.left = space;
outRect.right = space;
outRect.bottom = space;
outRect.top = space;
if(position%2==0){
outRect.left = 0;
}
}
}
此代码未经过测试;