如何适合Recyclerview中的图像视图数量?

发布于 2025-02-05 05:31:22 字数 2149 浏览 3 评论 0原文

我有一个recyclerview,带有固定的尺寸比例,

<androidx.constraintlayout.widget.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ...>

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:orientation="horizontal"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        app:layout_constraintDimensionRatio="35:16"
        ... />
</androidx.constraintlayout.widget.ConstraintLayout>

我需要在ImageView 内部显示recyclerview,但显示了固定数量的图像。在屏幕上,其余图像需要滚动以显示,类似于图像

“

我用image> ImageView内部创建此布局:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="4dp"
        android:layout_marginEnd="4dp"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"
        android:src="@drawable/image2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

此处的问题

我尝试了很多,但是如果没有设置layout_widthlayout_hieght没有固定的数字,则不会显示图像。 我想要的是设置ImageView Hight Width 动态使用 recyClerview hight 屏幕宽度< /strong>。

I have a RecyclerView with a fixed Dimension-Ratio

<androidx.constraintlayout.widget.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ...>

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:orientation="horizontal"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        app:layout_constraintDimensionRatio="35:16"
        ... />
</androidx.constraintlayout.widget.ConstraintLayout>

I Need to show list of ImageView inside the RecyclerView, but with a fixed number of images shown on the screen, and the rest of the images need to scroll to be shown, similar to the image

image

I Create this layout with ImageView inside it:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="4dp"
        android:layout_marginEnd="4dp"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"
        android:src="@drawable/image2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

The problem here

I try a lot, but the image doesn't show without setting the layout_width and layout_hieght with a fixed number.
and What i want is to set ImageView hight-width dynamicly with RecyclerView Hight and Screen Width.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多像笑话 2025-02-12 05:31:23

该解决方案非常简单:

用于获取屏幕宽度的适配器构造函数中的Pass Displaymetrics

DisplayMetrics displayMetrics;
public SearchSliderOneAdapter(DisplayMetrics displayMetrics) {
        this.displayMetrics = displayMetrics;
}

并计算 imageView = screenwidth \ numberofimage increateviewholder适配器的函数。

public SearchSliderOneViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_image_slider, parent, false);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                displayMetrics.widthPixels / numberOfImage,
                GridLayout.LayoutParams.MATCH_PARENT, 1);
        inflate.setLayoutParams(params);
        return new SearchSliderOneViewHolder(inflate, listener);
}

The solution is super easy:

pass DisplayMetrics In The Adapter Constructor for getting screen width.

DisplayMetrics displayMetrics;
public SearchSliderOneAdapter(DisplayMetrics displayMetrics) {
        this.displayMetrics = displayMetrics;
}

and calculate the width of ImageView = ScreenWidth\numberOfImage inside onCreateViewHolder function of the adapter.

public SearchSliderOneViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_image_slider, parent, false);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                displayMetrics.widthPixels / numberOfImage,
                GridLayout.LayoutParams.MATCH_PARENT, 1);
        inflate.setLayoutParams(params);
        return new SearchSliderOneViewHolder(inflate, listener);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文