Android 如何制作RecyclerView';s网格项的高度和宽度是否相等?
我将我的Android 如何制作RecyclerView';s网格项的高度和宽度是否相等?,android,grid,android-recyclerview,Android,Grid,Android Recyclerview,我将我的RecyclerView定义为网格,方法是使用gridlayoutmanager设置其布局,该布局的跨度计数为3,并且已经正常工作。我还向我的recycler视图添加了一个项目装饰器,它将管理网格项目之间的间距,这也很好 public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration { private int spanCount; private int spacing; p
RecyclerView
定义为网格,方法是使用gridlayoutmanager
设置其布局,该布局的跨度计数为3,并且已经正常工作。我还向我的recycler视图添加了一个项目装饰器,它将管理网格项目之间的间距,这也很好
public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {
private int spanCount;
private int spacing;
private boolean includeEdge;
public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) {
this.spanCount = spanCount;
this.spacing = spacing;
this.includeEdge = includeEdge;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
int position = parent.getChildAdapterPosition(view); // item position
int column = position % spanCount; // item column
if (includeEdge) {
outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)
if (position < spanCount) { // top edge
outRect.top = spacing;
}
outRect.bottom = spacing; // item bottom
} else {
outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing)
outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f / spanCount) * spacing)
if (position >= spanCount) {
outRect.top = spacing; // item top
}
}
}
}
我的网格项xml布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:layout_weight="1"
android:cropToPadding="false"
android:id="@+id/profile_picture"
android:scaleType="centerCrop"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:text="Theodore"
android:includeFontPadding="false"
android:textColor="@android:color/black"
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
通过在网格项之间添加空格,将调整网格项的宽度,这可能导致无法获得“我的项目”视图的比例大小。我想要得到的是动态调整网格项高度等于其宽度的大小。如何操作?您可以在“回收器视图保持器”的“onBindViewHolder”功能中编写代码。这样,您可以获取“回收器视图”的视图树观察者,并更改“回收器项目”视图的宽度或(和)高度。确保宽度/高度相等的一种方法是使用自定义视图,该视图在调用
onMeasure()
时根据其宽度计算高度。这样做的好处是,除了创建自定义视图之外,不需要编码
这是一个自定义视图,扩展了线性布局
:
public class SquareLayout extends LinearLayout {
public SquareLayout(Context context) {
super(context);
}
public SquareLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int width, int height) {
// note we are applying the width value as the height
super.onMeasure(width, width);
}
}
<com.yourpackage.name.views.SquareLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/profile_picture"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:cropToPadding="false"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:includeFontPadding="false"
android:textColor="@android:color/black"
android:text="Theodore"/>
</com.yourpackage.name.views.SquareLayout>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintDimensionRatio="1:1"
tools:src="@drawable/ic_chat_bubble_black_24dp"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
以下是网格项布局的修改版本,使用上述自定义线性布局
:
public class SquareLayout extends LinearLayout {
public SquareLayout(Context context) {
super(context);
}
public SquareLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int width, int height) {
// note we are applying the width value as the height
super.onMeasure(width, width);
}
}
<com.yourpackage.name.views.SquareLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/profile_picture"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:cropToPadding="false"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:includeFontPadding="false"
android:textColor="@android:color/black"
android:text="Theodore"/>
</com.yourpackage.name.views.SquareLayout>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintDimensionRatio="1:1"
tools:src="@drawable/ic_chat_bubble_black_24dp"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
我使用了
ConstraintLayout
:
public class SquareLayout extends LinearLayout {
public SquareLayout(Context context) {
super(context);
}
public SquareLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int width, int height) {
// note we are applying the width value as the height
super.onMeasure(width, width);
}
}
<com.yourpackage.name.views.SquareLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/profile_picture"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:cropToPadding="false"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:includeFontPadding="false"
android:textColor="@android:color/black"
android:text="Theodore"/>
</com.yourpackage.name.views.SquareLayout>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintDimensionRatio="1:1"
tools:src="@drawable/ic_chat_bubble_black_24dp"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
使用TreeObserver是最后的选择,也是一个非常老练的解决方案,使用修改过的自定义布局要干净得多。此外,根据布局树修改布局可能会导致多个布局过程。我复制了您的GridSpacingItemDecoration
,效果非常好!最简单的一个。谢谢。妈的,现在我指的是我工作的约束布局人,我有一个相对的布局,并没有形状有相等的宽度。现在把它放在relative中,并使用android:src=。我正要增加所有图像svg的尺寸。现在只有使用svg才是整个互联网上唯一有效的解决方案!!!。。荣誉