2014-10-23 2 views
0

У меня есть вид карты в моем приложении (с использованием библиотек поддержки), и я хочу, чтобы карты обертывались, если они не все вписываются в одну строку. В настоящее время они просто складываются вертикально. Есть ли способ сделать их не только обертыванием, но и заполнить весь родитель, если они являются единственным элементом в списке?CardView wrapping

В идеале я бы хотел, чтобы две карты в строке, а затем для их переноса в эту точку на новую строку.

Посмотреть содержащий recyclerview:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/my_rootlayout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 

<LinearLayout 
    android:id="@+id/my_window" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_centerInParent="true" 
    android:orientation="vertical"> 

    <TextView 
     android:id="@+id/my_headertext" 
     style="@style/roboto_header_text_black" 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_centerHorizontal="true" 
     android:layout_gravity="center_horizontal" 
     android:layout_weight=".15" 
     android:gravity="center" 
     android:padding="@dimen/device_padding" 
     android:text="@string/en_us_my_header" 
     android:textIsSelectable="false" /> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/my_list" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight=".85" 

     /> 
</LinearLayout> 

CardView:

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:card_view="http://schemas.android.com/apk/res-auto" 
android:layout_width="250dp" 
android:layout_height="wrap_content" 
android:orientation="vertical" 
card_view:cardCornerRadius="2dp"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="1" 
    android:orientation="vertical"> 
<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="200dp" 
    android:orientation="horizontal"> 

    <ImageView 
     android:id="@+id/image_thumbnail" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_centerHorizontal="true" 
     android:gravity="center" 
     android:src="@drawable/default_image" 
     android:scaleType="centerCrop" 
     android:adjustViewBounds="true" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="40dp" 
     android:orientation="horizontal" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentStart="true" 
     android:background="#99000000" 
     android:gravity="center_vertical" 
     android:paddingLeft="8dp"> 

     <TextView 
      android:id="@+id/my_listitem_title_text" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:singleLine="true" 
      android:text="This is my title" 
      android:textColor="@color/white" 
      android:textSize="20sp" 
      android:fontFamily="sans-serif-light" 
      android:paddingLeft="8dp" 
      android:paddingRight="8dp" 
      android:paddingTop="4dp" 
      android:paddingBottom="4dp" /> 

    </LinearLayout> 
</RelativeLayout> 

    <ProgressBar 
     android:layout_width="match_parent" 
     android:layout_height="8dp" 
     android:indeterminate="false" 
     style="?android:progressBarStyleHorizontal" 
     android:progress="45" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:paddingLeft="8dp" 
     android:paddingRight="8dp" 
     android:paddingTop="2dp" 
     android:paddingBottom="2dp"> 
    <TextView 
     android:id="@+id/code_text_block" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="@color/black" 
     android:textSize="14sp" 
     android:text="ABC" /> 

    <TextView 
    android:id="@+id/my_listitem_id_text" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="#123456" 
     android:textColor="@color/black" 
    android:layout_marginLeft="8dp" /> 
    </LinearLayout> 

    <TextView 
     android:id="@+id/my_listitem_date_text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textSize="14sp" 
     android:paddingLeft="8dp" 
     android:paddingRight="8dp" 
     android:paddingTop="2dp" 
     android:paddingBottom="2dp" 
     android:text="Nov 5 - Nov 12, 2016" 
     android:textColor="#7D7D7D" /> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingLeft="8dp" 
     android:paddingRight="8dp" 
     android:paddingTop="4dp" 
     android:paddingBottom="4dp"> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Download" 
      android:textAllCaps="true" 
      android:fontFamily="sans-serif" 
      android:textStyle="bold" 
      android:id="@+id/download_button" 
      android:background="@color/white" 
      android:textColor="@color/black" 
      android:paddingLeft="4dp" 
      android:paddingRight="4dp" 
      android:paddingTop="8dp" 
      android:paddingBottom="8dp" /> 
    </LinearLayout> 

</LinearLayout> 

ответ

1

Да, вы должны использовать GridLayout как @ random6174 said, используйте это code as guide, он делает именно то, что вы хотите.

Внимание к этой части, где он объявляет RecyclerView LayoutManager (MainActivity.java):

mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView); 
mLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL); 
mRecyclerView.setLayoutManager(mLayoutManager); 

Я надеюсь, что это поможет вам.

0

Я думаю, что вы можете использовать макет с потока здесь. Насколько я знаю, Android не имеет встроенного класса макета потока, но есть много вещей, которые можно найти в другом месте; например, this one.

Но тогда, поскольку вы хотите отображать карты, которые, вероятно, будут иметь одинаковый размер, возможно, вы также можете использовать GridLayout и программно настроить количество столбцов в соответствии с вашими потребностями.

Удачи вам!

/edit: Nevermind, я только что узнал, что такое CardView. Я думал, что вы хотите отображать игровые карты. Возможно, макет раскладки по-прежнему подходит для вас, но я, конечно, неправильно понял этот вопрос. Виноват.