2016-10-20 1 views
0

ich ein Raster Umsetzung des recyclerview haben mit der Autofit Raster wie hier beschrieben:Platz Artikel für AutoFitRecyclerView Gitter

http://blog.sqisland.com/2014/12/recyclerview-autofit-grid.html

jedoch meine Rasterelemente sind nicht Quadrate (sie sind Rechtecke) trotz der Gitterbefestigungs Artikelgröße. Gibt es eine Möglichkeit, die Gegenstände Platz zu machen (wie in tatsächlichen GridView), während die autofit Eigenschaft beibehalten

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

fragment_recyclerview.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: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> 

Antwort

0

So ist die Grund dafür ist, dass es sehr selten ist, dass Ihre RecyclerView eine Breite hat, die durch Ihre columnWidth Eigenschaft teilbar ist. Das bedeutet, dass Ihre tatsächliche Spaltenbreite fast immer größer als 140 dpi ist, während die Zellenhöhe immer 140 dpi beträgt.

Um dies zu beheben, müssen Sie Ihr Bild der Spaltenbreite anpassen und die Höhe der Breite anpassen. Eine Möglichkeit, dies zu erreichen, wäre die Verwendung eines SquareFrameLayout:

<com.project.SquareFrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/image_wrapper" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:padding="1dp"> 

    <com.makeramen.roundedimageview.RoundedImageView 
     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" /> 

</com.project.SquareFrameLayout> 
+0

Großartig! 'SquareFrameLayout' ist der Retter – stud91