使用StaggeredGridLayoutManager的recyclerview中的ImageView高度太大[Android / Kotlin]

时间:2018-11-21 06:18:09

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

它在用Java编写的旧项目

中正常工作

kotlin 编写的相同内容不起作用时...

问题回收站查看项目高度过大

lateinit var mImageProcessedRecyclerViewAdapater:ImageProcessedRecyclerViewAdapater
lateinit var mImageContainerAfterProcess: RecyclerView

//初始化适配器和项装饰

    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的活动布局文件

<?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>

我找不到我做错了什么

通过布局检查器捕获输出

enter image description here

我的旧项目输出

enter image description here

4 个答案:

答案 0 :(得分:0)

wrap_content在约束布局中的行为不相似。要实现所需的功能,您需要在您的ImageView中添加以下代码,并将其宽度和高度设置为0dp

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

答案 1 :(得分:0)

您的图像视图应该是这样

   <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" />

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

我的输出是 enter image description here

  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);