【问题标题】:ImageView height too large in recyclerview with use of StaggeredGridLayoutManager [Android/Kotlin]使用 StaggeredGridLayoutManager [Android/Kotlin] 在 recyclerview 中的 ImageView 高度太大
【发布时间】:2018-11-21 06:18:08
【问题描述】:

它在旧项目中运行良好用java编写

当用 kotlin 编写相同的东西时,它不起作用...

问题 RecyclerView 项目 高度太大

lateinit var mImageProcessedRecyclerViewAdapater:ImageProcessedRecyclerViewAdapater
lateinit var mImageContainerAfterProcess: RecyclerView

//初始化适配器和itemdecoration

    mImageContainerAfterProcess= findViewById(R.id.ImageContainerAfterProcess)

    mImageContainerAfterProcess.addItemDecoration(RecyclerViewItemDecoration(16))

    mImageContainerAfterProcess.layoutManager=StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)

    mImageContainerAfterProcess.adapter=mImageProcessedRecyclerViewAdapater

项目视图xml文件

<android.support.constraint.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"
    android:orientation="vertical">

<ImageView
    android:id="@+id/processedImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="2dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

包含recyclerview的Activity布局文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".activitis.ImageProcess">

    // layout height and width is match_constraint 
    <android.support.v7.widget.RecyclerView
        android:id="@+id/ImageContainerAfterProcess"
        android:layout_width="0dp"
        android:layout_height="0dp"
        tools:listitem="@layout/processd_image_item_view"
        app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>
</android.support.constraint.ConstraintLayout>

我不知道我做错了什么

通过布局检查器捕获输出

我的旧项目输出

【问题讨论】:

  • 发布你的recyclerview xml完整代码
  • @sasikumar 发布
  • 这两个 ConstraintLayouts 都是不必要的,因为 RecyclerView 和 ImageView 都是其父级的唯一子级。它们可以是根元素。另外,尝试将 RecyclerView 的宽度和高度固定为 match_parent,并将 ImageView 的宽度设置为 match_parent。让我知道这是否有效。
  • @RahulKumar 感谢您的评论先生,但它不起作用

标签: android android-layout android-recyclerview kotlin staggered-gridview


【解决方案1】:

尝试将此添加到您的处理图像ImageView

android:scaleType="centerCrop"
android:adjustViewBounds="true"
//use centerCrop to fill staggered view or centerInside to avoid cropping.

【讨论】:

  • 感谢您为这些属性图像视图获得了所需的完美高度。
【解决方案2】:

wrap_content 与约束布局的行为方式不同。要实现您想要的,您需要将以下代码添加到您的 ImageView 并将其宽度和高度设置为 0dp

app:layout_constraintWidth_default="wrap"
app:layout_constraintHeight_default="wrap"

【讨论】:

  • 你是否将imageview的高度和宽度设置为0dp?
  • 将其设置为 0dp 并尝试。
  • 先生还有其他建议吗?
  • 您的图片是否太大,您可以从源图片中查看尺寸吗?
  • 是的,图像太大,但这段代码正在处理旧项目,我将通过输出
【解决方案3】:

你的图像视图应该是这样的

   <ImageView
    android:id="@+id/processedImage"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:padding="2dp"
    android:scaleType="fitXY"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

【讨论】:

  • 你能设置imageview高度为android:layout_height="200dp"
  • 如果我们想固定图像的高度,那么为什么我们要使用 StaggeredGridLayoutManager 先生
【解决方案4】:

我的输出是

  android:scaleType="fitXY"

所有代码都在这里 布局

<RelativeLayout 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"
android:layout_margin="5dp">

<com.google.android.material.card.MaterialCardView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    app:cardCornerRadius="4dp"
    app:cardElevation="2dp">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp">

        <ImageView
            android:id="@+id/image_preview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scaleType="fitXY"
            android:src="@mipmap/ic_launcher" />

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/image_preview"
            android:layout_marginTop="10dp"
            android:text="Name"
            android:textColor="#000" />
    </RelativeLayout>
</com.google.android.material.card.MaterialCardView>
和代码是
recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
    StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
    layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
    layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setHasFixedSize(true);

【讨论】:

    猜你喜欢
    • 2019-08-25
    • 1970-01-01
    • 2016-12-17
    • 1970-01-01
    • 2011-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多