如何通过 java 在 gridview 之间给出 space
How to give space between gridview in through java
我如何在 gridView 项目之间完美对齐,我使用 java 添加了 gridview 并且它的基本适配器设置在其中我添加了图像和文本视图。
Xml代码:
<android.support.v7.widget.CardView
android:layout_width="180dp"
android:layout_height="200dp"
android:layout_margin="5dp"
android:orientation="vertical"
app:cardCornerRadius="5dp"
app:cardElevation="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:id="@+id/ImageWomenItem"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:scaleType="fitXY"
android:src="@drawable/womenone" />
</LinearLayout>
<TextView
android:id="@+id/itemName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Red Top"
android:textColor="#8e8d8d"
android:textSize="15dp" />
</LinearLayout>
</android.support.v7.widget.CardView>
这是 Activity 代码,我在其中声明了 recylerview 并设置了它的适配器
Java代码:
recyclerView = (RecyclerView) view.findViewById(R.id.recyclerViewClothes);
recyclerView.setHasFixedSize(true);
mLayoutManager = new GridLayoutManager(getActivity(), 2);
recyclerView.setLayoutManager(mLayoutManager);
mAdapter = new GridAdapter();
recyclerView.setAdapter(mAdapter);
向适配器的根布局添加填充 xml。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="8dp">
<android.support.v7.widget.CardView
app:cardCornerRadius="5dp"
app:cardElevation="5dp"
android:layout_width="180dp"
android:layout_height="200dp"
android:layout_margin="5dp"
android:orientation="vertical">.......
</android.support.v7.widget.CardView>
</LinearLayout>
使用Space装饰Class为此
public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
private int space;
public SpacesItemDecoration(int space) {
this.space = space;
}
@Override
public void getItemOffsets(Rect outRect, View view,
RecyclerView parent, RecyclerView.State state) {
outRect.left = space;
outRect.right = space;
outRect.bottom = space;
// Add top margin only for the first item to avoid double space between items
if (parent.getChildLayoutPosition(view) == 0) {
outRect.top = space;
} else {
outRect.top = 0;
}
}
}
您的 Activity 您设置适配器的位置
int spacing = getResources().getDimensionPixelSize(R.dimen.spacing);
recyclerview.addItemDecoration(new SpacesItemDecoration(spacing));
在dimen.xml
中指定
<dimen name="spacing">5px</dimen>
Xml代码:
<android.support.v7.widget.CardView
android:layout_width="175dp"
android:layout_height="200dp"
android:orientation="vertical"
app:cardCornerRadius="5dp"
app:cardElevation="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:id="@+id/ImageWomenItem"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:scaleType="fitXY"
android:src="@drawable/womenone" />
</LinearLayout>
<TextView
android:id="@+id/itemName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Red Top"
android:textColor="#8e8d8d"
android:textSize="15dp" />
</LinearLayout>
</android.support.v7.widget.CardView>
试试这个,
recyclerView = (RecyclerView) findViewById(R.id.recyclerView );
recyclerView .setLayoutManager(new GridLayoutManager(getActivity(), 2));
int spanCount = 2;
int spacing_left = 10;
int spacing_top=15;
recyclerView.addItemDecoration(new GridSpacingItemDecoration(spanCount, spacing_left, spacing_top));
GridSpacingItemDecoration:
/* set spacing for grid view */
public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {
final private int spanCount, spacing, spacing_top;
final private boolean includeEdge;
public GridSpacingItemDecoration(int spanCount, int spacing_left, int spacing_top) {
this.spanCount = spanCount;
this.spacing = spacing_left;
this.includeEdge = true;
this.spacing_top = spacing_top;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
int position = parent.getChildAdapterPosition(view); // item phases_position
int column = position % spanCount; // item column
if (includeEdge) {
outRect.left = spacing - column * spacing / spanCount;
outRect.right = (column + 1) * spacing / spanCount;
if (position < spanCount) { // top edge
outRect.top = spacing_top;
}
outRect.bottom = spacing_top; // item bottom
} else {
outRect.left = column * spacing / spanCount;
outRect.right = spacing - (column + 1) * spacing / spanCount;
if (position >= spanCount) {
outRect.top = spacing_top; // item top
}
}
}
}
我如何在 gridView 项目之间完美对齐,我使用 java 添加了 gridview 并且它的基本适配器设置在其中我添加了图像和文本视图。
Xml代码:
<android.support.v7.widget.CardView
android:layout_width="180dp"
android:layout_height="200dp"
android:layout_margin="5dp"
android:orientation="vertical"
app:cardCornerRadius="5dp"
app:cardElevation="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:id="@+id/ImageWomenItem"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:scaleType="fitXY"
android:src="@drawable/womenone" />
</LinearLayout>
<TextView
android:id="@+id/itemName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Red Top"
android:textColor="#8e8d8d"
android:textSize="15dp" />
</LinearLayout>
</android.support.v7.widget.CardView>
这是 Activity 代码,我在其中声明了 recylerview 并设置了它的适配器
Java代码:
recyclerView = (RecyclerView) view.findViewById(R.id.recyclerViewClothes);
recyclerView.setHasFixedSize(true);
mLayoutManager = new GridLayoutManager(getActivity(), 2);
recyclerView.setLayoutManager(mLayoutManager);
mAdapter = new GridAdapter();
recyclerView.setAdapter(mAdapter);
向适配器的根布局添加填充 xml。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="8dp">
<android.support.v7.widget.CardView
app:cardCornerRadius="5dp"
app:cardElevation="5dp"
android:layout_width="180dp"
android:layout_height="200dp"
android:layout_margin="5dp"
android:orientation="vertical">.......
</android.support.v7.widget.CardView>
</LinearLayout>
使用Space装饰Class为此
public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
private int space;
public SpacesItemDecoration(int space) {
this.space = space;
}
@Override
public void getItemOffsets(Rect outRect, View view,
RecyclerView parent, RecyclerView.State state) {
outRect.left = space;
outRect.right = space;
outRect.bottom = space;
// Add top margin only for the first item to avoid double space between items
if (parent.getChildLayoutPosition(view) == 0) {
outRect.top = space;
} else {
outRect.top = 0;
}
}
}
您的 Activity 您设置适配器的位置
int spacing = getResources().getDimensionPixelSize(R.dimen.spacing);
recyclerview.addItemDecoration(new SpacesItemDecoration(spacing));
在dimen.xml
中指定 <dimen name="spacing">5px</dimen>
Xml代码:
<android.support.v7.widget.CardView
android:layout_width="175dp"
android:layout_height="200dp"
android:orientation="vertical"
app:cardCornerRadius="5dp"
app:cardElevation="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:id="@+id/ImageWomenItem"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:scaleType="fitXY"
android:src="@drawable/womenone" />
</LinearLayout>
<TextView
android:id="@+id/itemName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Red Top"
android:textColor="#8e8d8d"
android:textSize="15dp" />
</LinearLayout>
</android.support.v7.widget.CardView>
试试这个,
recyclerView = (RecyclerView) findViewById(R.id.recyclerView );
recyclerView .setLayoutManager(new GridLayoutManager(getActivity(), 2));
int spanCount = 2;
int spacing_left = 10;
int spacing_top=15;
recyclerView.addItemDecoration(new GridSpacingItemDecoration(spanCount, spacing_left, spacing_top));
GridSpacingItemDecoration:
/* set spacing for grid view */
public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {
final private int spanCount, spacing, spacing_top;
final private boolean includeEdge;
public GridSpacingItemDecoration(int spanCount, int spacing_left, int spacing_top) {
this.spanCount = spanCount;
this.spacing = spacing_left;
this.includeEdge = true;
this.spacing_top = spacing_top;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
int position = parent.getChildAdapterPosition(view); // item phases_position
int column = position % spanCount; // item column
if (includeEdge) {
outRect.left = spacing - column * spacing / spanCount;
outRect.right = (column + 1) * spacing / spanCount;
if (position < spanCount) { // top edge
outRect.top = spacing_top;
}
outRect.bottom = spacing_top; // item bottom
} else {
outRect.left = column * spacing / spanCount;
outRect.right = spacing - (column + 1) * spacing / spanCount;
if (position >= spanCount) {
outRect.top = spacing_top; // item top
}
}
}
}