2015-09-10 3 views
4

Пожалуйста, смотрите ниже изображение:ANDROID: несколько видов карт внутри вида ресайклера

Как можно вставить несколько карточек в виде ресайклеров. или любым другим способом достичь этого.
Использование вида Recycler необходимо.

enter image description here

+0

Вы имеете в виду, что каждый элемент RecyclerView содержит 2 CardView? –

+0

правильный ......... – ojas

+0

вы можете использовать вид сетки также –

ответ

6

Я думаю, что правильный путь к достижению цели, как описано в изображении присоединенной будет используйте GridLayoutManager, а не используя RecyclerView.LayoutManager или LinearLayoutManager.

LayoutManager прилагается на RecyclerView определяет количество колонок. Есть 3 подкласса.

  1. LinearLayoutManager
  2. GridLayoutmanger
  3. StaggeredGridLayoutmanger

В вашей деятельности, где вы инициализировать RecyclerView.LayoutManager, изменить

RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManger(this); 

в

GridLayoutManager mLayoutManager = new GridLayoutManger(this, 2); 

2 является числом пролетов сетки. Каждый элемент будет размещен за один промежуток времени, и, таким образом, у вас будет 2 столбца в вашем просмотре ресайклера.

2

Ваш XML может сделать так:

<LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 
     <android.support.v7.widget.CardView 
       android:layout_width="0dp" 
       android:layout_weight="1" 
       android:layout_height="wrap_content"> 
     </android.support.v7.widget.CardView> 
     <android.support.v7.widget.CardView 
       android:layout_width="0dp" 
       android:layout_weight="1" 
       android:layout_height="wrap_content"> 
     </android.support.v7.widget.CardView> 
</LinearLayout> 
3

Попробуйте

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="8dp"> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 
     <android.support.v7.widget.CardView 

      android:layout_width="match_parent" 
      android:layout_weight="1" 
      android:layout_height="300dp" 
      android:orientation="vertical"> 
      <ImageView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:src="@drawable/a"/> 
      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="Huming Bird"/> 
     </android.support.v7.widget.CardView> 
     <android.support.v7.widget.CardView 
      android:layout_width="match_parent" 
      android:layout_weight="1" 
      android:layout_height="wrap_content"> 
      <ImageView 
       android:layout_width="match_parent" 
       android:layout_height="300dp" 
       android:src="@drawable/b"/> 
      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="Huming Bird"/> 
     </android.support.v7.widget.CardView> 
    </LinearLayout> 

</LinearLayout> 
0

Для динамической настройки в соответствии с количеством требуемых столбцов вы можете установить диспетчер компоновки на основе количества столбцов. Это очень гибко. Тот же/похожий код с соответствующей компоновкой может работать на любом планшете или на телефоне.

// Set the adapter 
    if (view instanceof RecyclerView) { 
     Context context = view.getContext(); 
     RecyclerView recyclerView = (RecyclerView) view; 
     if (mColumnCount <= 1) { 
      recyclerView.setLayoutManager(new LinearLayoutManager(context)); 
     } else { 
      recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount)); 
     } 
     .... 
    } 
Смежные вопросы