我有一个
在iOS上,我使用
但它给出了这个结果,无论设备如何,宽度都相同: 我不想改变图标的大小,只想使宽度间距基本匹配其父容器。
谢谢!
编辑:这是GridRecyclerView类,这是一个自定义类,可帮助进行网格动画:
RecyclerView
,我希望它始终有三列。使用GridLayoutManager
时,出现了静态宽度的问题,我不知道如何使其匹配屏幕宽度。在iOS上,我使用
UICollectionView
和自动布局
很容易解决宽度问题,就像这样:
我该如何在Android上获得相同的结果?以下是我的content_symptom_tracker.xml
代码:<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="match_parent"
android:background="@color/bzPrimary"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".SymptomTracker"
tools:showIn="@layout/activity_symptom_tracker">
<com.cryptixltd.peterruppert.brainzaps.GridRecyclerView
android:id="@+id/symptomRecycler"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:foregroundGravity="center"
android:layoutAnimation="@anim/grid_layout_animation_from_bottom"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
以下是我在Activity中设置Recycler的方法:
recyclerView = findViewById(R.id.symptomRecycler);
int numberOfColumns = 3;
recyclerView.setLayoutManager(new GridLayoutManager(this, numberOfColumns));
adapter = new SymptomAdapter(this, common_symptoms);
recyclerView.setAdapter(adapter);
但它给出了这个结果,无论设备如何,宽度都相同: 我不想改变图标的大小,只想使宽度间距基本匹配其父容器。
谢谢!
编辑:这是GridRecyclerView类,这是一个自定义类,可帮助进行网格动画:
public class GridRecyclerView extends RecyclerView {
public GridRecyclerView(Context context) { super(context); }
public GridRecyclerView(Context context, AttributeSet attrs) { super(context, attrs); }
public GridRecyclerView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); }
@Override
protected void attachLayoutAnimationParameters(View child, ViewGroup.LayoutParams params,
int index, int count) {
final LayoutManager layoutManager = getLayoutManager();
if (getAdapter() != null && layoutManager instanceof GridLayoutManager){
GridLayoutAnimationController.AnimationParameters animationParams =
(GridLayoutAnimationController.AnimationParameters) params.layoutAnimationParameters;
if (animationParams == null) {
// If there are no animation parameters, create new once and attach them to
// the LayoutParams.
animationParams = new GridLayoutAnimationController.AnimationParameters();
params.layoutAnimationParameters = animationParams;
}
// Next we are updating the parameters
// Set the number of items in the RecyclerView and the index of this item
animationParams.count = count;
animationParams.index = index;
// Calculate the number of columns and rows in the grid
final int columns = ((GridLayoutManager) layoutManager).getSpanCount();
animationParams.columnsCount = columns;
animationParams.rowsCount = count / columns;
// Calculate the column/row position in the grid
final int invertedIndex = count - 1 - index;
animationParams.column = columns - 1 - (invertedIndex % columns);
animationParams.row = animationParams.rowsCount - 1 - invertedIndex / columns;
} else {
// Proceed as normal if using another type of LayoutManager
super.attachLayoutAnimationParameters(child, params, index, count);
}
}
}
RecyclerView
的项以及RecyclerView
本身设置layout_width="match_parent"
。 - atarasenko