【问题标题】:Make a Recyclerview of 9 items with GridLayoutManager fit the screen使用 GridLayoutManager 使 9 个项目的 Recyclerview 适合屏幕
【发布时间】:2021-06-18 08:25:20
【问题描述】:

我有一个由 RecyclerView 和 GridLayoutManager 生成的网格,所以在我的情况下,网格上总是只有 9 个项目,所以我希望它们填满整个屏幕。以下是它们在我的屏幕上的外观:

这是activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</RelativeLayout>

那么,single_item.xml 是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="120dp"
    android:padding="1dp"
    >
    <ImageView
        android:id="@+id/image_item"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
         />
</RelativeLayout>

我是如何附加 Gridlayoutmanager 的:

        rv = findViewById(R.id.rv);        
        ImageAdapter imageAdapter = new ImageAdapter(allBitmaps, MainActivity.this);
        mLayoutManager = new GridLayoutManager(MainActivity.this, 3);
        rv.setLayoutManager(mLayoutManager);
        rv.setAdapter(imageAdapter);

所以我希望网格能够很好地适应整个屏幕,如何才能做到最好?

【问题讨论】:

  • 贴合屏幕会让它们失去宽高比
  • @Zain,将为您提供适合屏幕的解决方案,我们可能会解决纵横比

标签: java android android-recyclerview bitmap android-adapter


【解决方案1】:

这是场景:

  • 在将适配器连接到 RecycerView 之前测量其宽度和高度:
  • 将上一步的宽高除以3,得到想要的RecyclerView项目宽高;因为你有 3 个水平和垂直的项目。
  • 将项目宽度和高度作为参数传递给RecyclerView 适配器并将它们存储为字段。

在 onCreate() 活动中:

recyclerView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
    @Override
    public boolean onPreDraw() {
        if (recyclerView.getViewTreeObserver().isAlive())
            recyclerView.getViewTreeObserver().removeOnPreDrawListener(this);

        float width = (float) recyclerView.getWidth() / 3;
        float height = (float) recyclerView.getHeight() / 3;
        ImageAdapter adapter = new ImageAdapter(context, width, height, allBitmaps);
        recyclerView.setAdapter(adapter);

        return true;
    }
});

  • 在ViewHolder中设置ImageView的宽度和高度:
class ViewHolder extends RecyclerView.ViewHolder {

    ImageView image;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        image = itemView.findViewById(R.id.image);

        image.getLayoutParams().width = (int) itemWidth;
        image.getLayoutParams().height = (int) itemHeight;
    }
}

我仅使用ImageView 作为列表项布局对此进行了测试:

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleType="fitXY" />

人像预览:

风景预览:

【讨论】:

  • 谢谢大家,它成功了,有没有办法让它们全部变成正方形而不是三角形
  • 我猜你的意思不是矩形?如果是这样,那么它们将不适合整个布局,因为它是一个矩形,这不是您想要的。
  • 我没明白你的意思是Gridlayout的三角形项目吗?
  • 我不知道为什么我一直在输入三角形,我的意思是 rectangle。道歉
  • 没问题,伙计,我也在第一条评论中驳回了它:D .. 如果您需要正方形,那么我们需要一个正方形移动装置(或正方形 RecyclerView 布局,所以我们会回来到初始阶段),我有两个建议,首先你可以让它们像在你的 OP 中一样,但将 RecyclerView 在屏幕中居中,并有一些模糊背景..第二个是将原始图像剪切为在将手机屏幕分成 9 块之前适合手机屏幕(我没有测试它,但它可能会有失真的图像).. 无论如何祝你好运
【解决方案2】:

我想如果你想让这些图像填​​满屏幕,你需要在你的单个 item.xml 中修改图像的宽度和高度

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-11
    • 2015-07-06
    • 1970-01-01
    • 1970-01-01
    • 2016-02-19
    • 1970-01-01
    相关资源
    最近更新 更多