Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android AutoFitRecyclerView网格的方形项目_Android_Android Recyclerview - Fatal编程技术网

Android AutoFitRecyclerView网格的方形项目

Android AutoFitRecyclerView网格的方形项目,android,android-recyclerview,Android,Android Recyclerview,我使用Autofit网格实现了recyclerview,如下所述: 但是,我的网格项不是正方形(它们是矩形),尽管固定了网格项的大小。在维护autofit属性的同时,是否有办法使项目呈方形(如在实际的GridView中) item\u grid\u image.xml <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/and

我使用Autofit网格实现了recyclerview,如下所述:

但是,我的网格项不是正方形(它们是矩形),尽管固定了网格项的大小。在维护autofit属性的同时,是否有办法使项目呈方形(如在实际的
GridView
中)

item\u grid\u image.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:tools="http://schemas.android.com/tools"
         android:id="@+id/image_wrapper"
         android:layout_width="140dp"
         android:layout_height="140dp"
         android:padding="1dp"
         android:layout_centerInParent="true">

    <com.makeramen.roundedimageview.RoundedImageView
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:contentDescription="@null"
        android:scaleType="centerCrop"
        tools:src="@drawable/file_directory" /> 

</FrameLayout>
<?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:id="@+id/grid_view_wrapper"
    android:layout_marginStart="@dimen/default_margin"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

        <com.project.AutoFitRecyclerView
            android:id="@+id/grid_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/count_text_view"
            android:layout_alignParentTop="true"
            android:columnWidth="140dp"
            android:layout_centerInParent="true"
            android:requiresFadingEdge="vertical"
            tools:listitem="@layout/item_grid_image">
        </com.project.AutoFitRecyclerView>

</RelativeLayout>

fragment\u recyclerview.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:tools="http://schemas.android.com/tools"
         android:id="@+id/image_wrapper"
         android:layout_width="140dp"
         android:layout_height="140dp"
         android:padding="1dp"
         android:layout_centerInParent="true">

    <com.makeramen.roundedimageview.RoundedImageView
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:contentDescription="@null"
        android:scaleType="centerCrop"
        tools:src="@drawable/file_directory" /> 

</FrameLayout>
<?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:id="@+id/grid_view_wrapper"
    android:layout_marginStart="@dimen/default_margin"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

        <com.project.AutoFitRecyclerView
            android:id="@+id/grid_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/count_text_view"
            android:layout_alignParentTop="true"
            android:columnWidth="140dp"
            android:layout_centerInParent="true"
            android:requiresFadingEdge="vertical"
            tools:listitem="@layout/item_grid_image">
        </com.project.AutoFitRecyclerView>

</RelativeLayout>

因此,这样做的原因是,
RecyclerView
的宽度很少会被
columnWidth
属性整除。这意味着您的实际列宽几乎总是大于140dp,而单元格高度总是140dp

要解决此问题,需要使图像与列宽匹配,并使高度与宽度匹配。实现这一点的一种方法是使用:


太棒了
SquareFrameLayout
是救世主