Upv*_*ote 10 android android-gridlayout gridlayoutmanager android-recyclerview
我想在网格中的项目之间创建一个空格.这些物品都应具有相同的宽度/高度.
我尝试过的:
创建一个GridLayoutOffsetDecorator将偏移量应用于所有网格项:
class GridLayoutOffsetDecorator(var offset: Int) : RecyclerView.ItemDecoration() {
override fun getItemOffsets(
outRect: Rect,
view: View,
parent: RecyclerView,
state: RecyclerView.State?) {
super.getItemOffsets(outRect, view, parent, state)
outRect.set(offset, offset, offset, offset)
}
}
Run Code Online (Sandbox Code Playgroud)
具有偏移量8dp会16dp在项目之间创建空间.所以我们仍然需要8dp在外边缘应用填充:
<android.support.v7.widget.RecyclerView
android:id="@+id/productList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:padding="8dp" />
Run Code Online (Sandbox Code Playgroud)
结果是:
问题:项目大小不相同:
当您看到蓝线时,您会注意到高度略有不同.仅在填充Recyclerview之后才会出现此差异.这个填充似乎稍微调整了一些项目的大小.你们有这方面的经验吗?知道如何解决这个问题吗?
通过从项目中移除图像,高度差消失.这是图像的设置方式:
<SquareImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
app:imageResource="@{product.image}" />
Run Code Online (Sandbox Code Playgroud)
SquareImageView:
class SquareImageView : ImageView {
constructor(context: Context) : super(context)
constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet)
constructor(context: Context, attributeSet: AttributeSet, defStyleAttr: Int) : super(context, attributeSet, defStyleAttr)
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec)
}
}
Run Code Online (Sandbox Code Playgroud)
所以问题可能是由图像本身引起的.
我在使用时遇到了同样的问题GridLayoutManager。通过在适配器中设置图像的宽度和高度解决了这个问题。这是我的解决方案,希望有所帮助。
这就是我ImageView在布局中的放置方式。
<LinearLayout
android:id="@+id/layoutImageProduct"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
tools:minHeight="100dp"
tools:minWidth="150dp">
<ImageView
android:id="@+id/imageProduct"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:src="@drawable/placeholder_product"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
然后应用LayoutParams到父布局上ImageView
//Setting View width/height
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) holder.layoutImageProduct.getLayoutParams();
params.width = (int) ResourcesUtils.getColumnWidth(context);
params.height = ((int) (ResourcesUtils.getColumnWidth(context)));
holder.layoutImageProduct.setLayoutParams(params);
params = (LinearLayout.LayoutParams) holder.imageProduct.getLayoutParams();
params.width = (int) ResourcesUtils.getColumnWidth(context);
params.height = (int) ResourcesUtils.getColumnWidth(context);
holder.imageProduct.setLayoutParams(params);
Run Code Online (Sandbox Code Playgroud)
这是 getWidth 方法。
public static float getColumnWidth(Context context) {
if (GRID_COLUMN_WIDTH == -1) {
int screenWidth = getDisplayMetrics(context).widthPixels;
float horizontalPadding = getDimensInPixel(context, R.dimen.activity_horizontal_margin);
// NUM OF COLUMN = 2
GRID_COLUMN_WIDTH = screenWidth / 2 - horizontalPadding * 2;
}
return GRID_COLUMN_WIDTH;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
570 次 |
| 最近记录: |