2016-01-02 3 views
0

У меня есть и XML-файл, как это (проблема с RecyclerView - прокрутите список до конца файла):Android RecyclerView - установка его высота и отображающие элементы

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/recipe_coordinator_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <!-- Scroll View Layout --> 
    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="true"> 

     <!-- Main Layout--> 
     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:focusableInTouchMode="true"> 

      <!-- Recipe Image --> 
      <ImageView 
       android:id="@+id/recipe_image" 
       android:layout_width="match_parent" 
       android:layout_height="200dp" 
       android:adjustViewBounds="true" 
       android:background="@color/background_3" 
       android:scaleType="centerCrop" 
       android:src="@drawable/no_image_placeholder" 
       android:layout_alignParentTop="true" 
       android:layout_alignParentStart="true" /> 

      <!-- Toolbar --> 
      <include 
       android:id="@+id/toolbar" 
       layout="@layout/transparent_toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_alignParentTop="true" 
       android:layout_alignParentStart="true" /> 

      <!-- Recipe Title And Recipe Info --> 
      <LinearLayout 
       android:id="@+id/layout_title_info" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       android:background="@color/background_8" 
       android:layout_below="@id/recipe_image"> 

       <!-- Recipe Title --> 
       <TextView 
        android:id="@+id/recipe_title" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:textColor="@color/white_text" 
        android:fontFamily="sans-serif-medium" 
        android:textSize="22sp" 
        android:background="@color/background_8" 
        android:paddingTop="48dp" 
        android:paddingBottom="8dp" 
        android:paddingLeft="8dp" 
        android:paddingRight="8dp" 
        android:text="Szarlotka z nadzieniem kakaowym" /> 

       <!-- Recipe Info --> 
       <TableLayout 
        android:id="@+id/table_layout" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:shrinkColumns="*" 
        android:stretchColumns="*" 
        android:padding="8dp"> 

        <!-- Row 1 With 2 Columns --> 
        <TableRow 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content"> 

         <!-- Column 1 [Kitchen/Meal Type] --> 
         <LinearLayout 
          android:layout_height="wrap_content" 
          android:layout_width="0dp" 
          android:orientation="vertical" 
          android:layout_weight="1" 
          android:layout_column="0"> 

          <!-- Image With Text --> 
          <LinearLayout 
           android:layout_width="match_parent" 
           android:layout_height="wrap_content" 
           android:gravity="center|left" 
           android:orientation="horizontal"> 

           <!-- Image Icon --> 
           <ImageView 
            android:layout_width="18dp" 
            android:layout_height="18dp" 
            android:src="@drawable/ic_script" 
            android:layout_marginRight="2dp"/> 

           <!-- Kitchen/Meal Type Label --> 
           <TextView 
            android:layout_height="wrap_content" 
            android:layout_width="match_parent" 
            android:textSize="17sp" 
            android:textColor="@color/teal_text" 
            android:text="@string/recipe_kitchen_meal_type"/> 

          </LinearLayout> 

          <!-- Kitchen/Meal Type --> 
          <TextView 
           android:id="@+id/recipe_category" 
           android:layout_height="wrap_content" 
           android:layout_width="match_parent" 
           android:textSize="16sp" 
           android:layout_marginLeft="20dp" 
           android:textColor="@color/white_text" 
           android:fontFamily="sans-serif-light" 
           android:text="-"/> 

         </LinearLayout> 

         <!-- Column 2 [Difficulty Level] --> 
         <LinearLayout 
          android:layout_height="wrap_content" 
          android:layout_width="0dp" 
          android:orientation="vertical" 
          android:layout_weight="1" 
          android:layout_marginLeft="8dp" 
          android:layout_column="0"> 

          <!-- Image With Text --> 
          <LinearLayout 
           android:layout_width="match_parent" 
           android:layout_height="wrap_content" 
           android:gravity="center|left" 
           android:orientation="horizontal"> 

           <!-- Image Icon --> 
           <ImageView 
            android:layout_width="18dp" 
            android:layout_height="18dp" 
            android:src="@drawable/ic_signal" 
            android:layout_marginRight="2dp"/> 

           <!-- Difficulty Level Label --> 
           <TextView 
            android:layout_height="wrap_content" 
            android:layout_width="match_parent" 
            android:textSize="17sp" 
            android:textColor="@color/teal_text" 
            android:text="@string/recipe_difficulty_lvl"/> 

          </LinearLayout> 

          <!-- Difficulty Level --> 
          <TextView 
           android:id="@+id/recipe_difficulty_lvl" 
           android:layout_height="wrap_content" 
           android:layout_width="match_parent" 
           android:fontFamily="sans-serif-light" 
           android:textSize="16sp" 
           android:layout_marginLeft="20dp" 
           android:textColor="@color/white_text" 
           android:text="-"/> 

         </LinearLayout> 

        </TableRow> 

        <!-- Row 2 With 2 Columns --> 
        <TableRow 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_marginTop="8dp"> 

         <!-- Column 1 [Preparation Time] --> 
         <LinearLayout 
          android:layout_height="wrap_content" 
          android:layout_width="0dp" 
          android:orientation="vertical" 
          android:layout_weight="1" 
          android:layout_column="0"> 

          <!-- Image With Text --> 
          <LinearLayout 
           android:layout_width="match_parent" 
           android:layout_height="wrap_content" 
           android:gravity="center|left" 
           android:orientation="horizontal"> 

           <!-- Image Icon --> 
           <ImageView 
            android:layout_width="18dp" 
            android:layout_height="18dp" 
            android:src="@drawable/ic_timer" 
            android:layout_marginRight="2dp"/> 

           <!-- Preparation Time Label --> 
           <TextView 
            android:layout_height="wrap_content" 
            android:layout_width="match_parent" 
            android:textSize="17sp" 
            android:textColor="@color/teal_text" 
            android:text="@string/recipe_preparation_time"/> 

          </LinearLayout> 

          <!-- Preparation Time --> 
          <TextView 
           android:id="@+id/recipe_preparation_time" 
           android:layout_height="wrap_content" 
           android:layout_width="match_parent" 
           android:textSize="16sp" 
           android:layout_marginLeft="20dp" 
           android:fontFamily="sans-serif-light" 
           android:textColor="@color/white_text" 
           android:text="-"/> 

         </LinearLayout> 

         <!-- Column 2 [People Count] --> 
         <LinearLayout 
          android:layout_height="wrap_content" 
          android:layout_width="0dp" 
          android:orientation="vertical" 
          android:layout_marginLeft="8dp" 
          android:layout_weight="1" 
          android:layout_column="1"> 

          <!-- Image With Text --> 
          <LinearLayout 
           android:layout_width="match_parent" 
           android:layout_height="wrap_content" 
           android:gravity="center|left" 
           android:orientation="horizontal"> 

           <!-- Image Icon --> 
           <ImageView 
            android:layout_width="18dp" 
            android:layout_height="18dp" 
            android:src="@drawable/ic_human" 
            android:layout_marginRight="2dp"/> 

           <!-- People Count Label --> 
           <TextView 
            android:layout_height="wrap_content" 
            android:layout_width="match_parent" 
            android:textSize="17sp" 
            android:textColor="@color/teal_text" 
            android:text="@string/recipe_people_count"/> 

          </LinearLayout> 

          <!-- People Count --> 
          <TextView 
           android:id="@+id/recipe_people_count" 
           android:layout_height="wrap_content" 
           android:layout_width="match_parent" 
           android:textSize="16sp" 
           android:layout_marginLeft="20dp" 
           android:fontFamily="sans-serif-light" 
           android:textColor="@color/white_text" 
           android:text="-"/> 

         </LinearLayout> 

        </TableRow> 

        <!-- Row 3 With 2 Columns --> 
        <TableRow 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_marginTop="8dp"> 

         <!-- Column 1 [Add Date] --> 
         <LinearLayout 
          android:layout_height="wrap_content" 
          android:layout_width="0dp" 
          android:orientation="vertical" 
          android:layout_weight="1" 
          android:layout_column="0"> 

          <!-- Image With Text --> 
          <LinearLayout 
           android:layout_width="match_parent" 
           android:layout_height="wrap_content" 
           android:gravity="center|left" 
           android:orientation="horizontal"> 

           <!-- Image Icon --> 
           <ImageView 
            android:layout_width="18dp" 
            android:layout_height="18dp" 
            android:src="@drawable/ic_calendar" 
            android:layout_marginRight="2dp"/> 

           <!-- Add Date Label --> 
           <TextView 
            android:layout_height="wrap_content" 
            android:layout_width="match_parent" 
            android:textSize="17sp" 
            android:textColor="@color/teal_text" 
            android:text="@string/recipe_add_date"/> 

          </LinearLayout> 

          <!-- Add Date --> 
          <TextView 
           android:id="@+id/recipe_add_date" 
           android:layout_height="wrap_content" 
           android:layout_width="match_parent" 
           android:textSize="16sp" 
           android:fontFamily="sans-serif-light" 
           android:textColor="@color/white_text" 
           android:layout_marginLeft="20dp" 
           android:layout_marginTop="2dp" 
           android:text="-"/> 

         </LinearLayout> 

         <!-- Column 2 [Author Name] --> 
         <LinearLayout 
          android:layout_height="wrap_content" 
          android:layout_width="0dp" 
          android:orientation="vertical" 
          android:layout_weight="1" 
          android:layout_marginLeft="8dp" 
          android:layout_column="0"> 

          <!-- Image With Text --> 
          <LinearLayout 
           android:layout_width="match_parent" 
           android:layout_height="wrap_content" 
           android:gravity="center|left" 
           android:orientation="horizontal"> 

           <!-- Image Icon --> 
           <ImageView 
            android:layout_width="18dp" 
            android:layout_height="18dp" 
            android:src="@drawable/ic_telegram" 
            android:layout_marginRight="2dp"/> 

           <!-- Author Name Label --> 
           <TextView 
            android:layout_height="wrap_content" 
            android:layout_width="match_parent" 
            android:textSize="17sp" 
            android:textColor="@color/teal_text" 
            android:text="@string/recipe_proposition"/> 

          </LinearLayout> 

          <!-- Author Name --> 
          <TextView 
           android:id="@+id/recipe_author_name" 
           android:layout_height="wrap_content" 
           android:layout_width="match_parent" 
           android:textSize="17sp" 
           android:layout_marginLeft="20dp" 
           android:fontFamily="sans-serif-light" 
           android:textColor="@color/white_text" 
           android:text="-"/> 

         </LinearLayout> 

        </TableRow> 

       </TableLayout> 

      </LinearLayout> 

      <!-- Floating Like Button With Counter --> 
      <LinearLayout 
       android:id="@+id/like_counter_layout" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       android:gravity="center" 
       android:padding="8dp" 
       android:layout_marginBottom="-53dp" 
       android:layout_alignBottom="@id/recipe_image" 
       android:layout_alignParentEnd="true"> 

       <!-- Like Floating Button --> 
       <android.support.design.widget.FloatingActionButton 
        android:id="@+id/recipe_like" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:clickable="true" 
        android:src="@drawable/ic_action_like" /> 

       <!-- Like Counter --> 
       <TextView 
        android:id="@+id/recipe_counter" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginTop="3dp" 
        android:textSize="12sp" 
        android:textColor="@color/white_text" 
        android:layout_alignBottom="@id/recipe_like" 
        android:text="0"/> 

      </LinearLayout> 

      <!-- Add To Favourites Floating Button --> 
      <android.support.design.widget.FloatingActionButton 
       android:id="@+id/recipe_add_to_favourites" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:clickable="true" 
       android:layout_marginBottom="-28dp" 
       android:src="@drawable/ic_action_add_to_favourites" 
       android:layout_toLeftOf="@id/like_counter_layout" 
       android:layout_alignBottom="@id/recipe_image"/> 

      <!-- Recipe Description --> 
      <LinearLayout 
       android:id="@+id/layout_description" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       android:background="@color/background_9" 
       android:padding="8dp" 
       android:layout_below="@id/layout_title_info"> 

       <!-- Recipe Description Label--> 
       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:padding="4dp" 
        android:fontFamily="sans-serif-medium" 
        android:textColor="@color/white_text" 
        android:textSize="22sp" 
        android:text="@string/recipe_description" /> 

       <!-- Recipe Description --> 
       <TextView 
        android:id="@+id/recipe_description" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:padding="8dp" 
        android:textSize="17sp" 
        android:fontFamily="sans-serif-light" 
        android:textColor="@color/white_text" 
        android:text="-"/> 

      </LinearLayout> 

      <!-- Comments --> 
      <LinearLayout 
       android:id="@+id/comment_layout" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       android:paddingTop="8dp" 
       android:paddingBottom="8dp" 
       android:background="@color/background_2" 
       android:layout_below="@id/layout_description"> 

       <!-- Comments Label--> 
       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="8dp" 
        android:layout_marginRight="8dp" 
        android:layout_marginBottom="8dp" 
        android:fontFamily="sans-serif-medium" 
        android:textColor="@color/primary_text" 
        android:textSize="22sp" 
        android:text="@string/recipe_comments" /> 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal" 
        android:layout_gravity="center" 
        android:layout_marginLeft="8dp" 
        android:layout_marginRight="8dp"> 

        <!-- New Comment Input --> 
        <EditText 
         android:id="@+id/recipe_new_comment" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:singleLine="true" 
         android:layout_weight="1" 
         android:hint="@string/hint_new_comment" /> 

        <!-- Comment Button --> 
        <ImageButton 
         android:id="@+id/recipe_submit_comment" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_marginLeft="4dp" 
         android:background="@color/background_2" 
         android:tint="@color/icon_1" 
         android:src="@drawable/ic_add_comment"/> 

       </LinearLayout> 

       <!-- Recycler View --> 
       <android.support.v7.widget.RecyclerView 
        android:id="@+id/recipe_recyclerview" 
        android:layout_width="match_parent" 
        android:layout_height="200dp"/> 

       <!-- More Comments Button --> 
       <android.support.v7.widget.AppCompatButton 
        android:id="@+id/recipe_more_comments" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:stateListAnimator="@null" 
        android:theme="@style/AppTheme.ButtonStyle4" 
        android:padding="17dp" 
        android:text="@string/btn_more_comments"/> 

      </LinearLayout> 

     </RelativeLayout> 

    </ScrollView> 

</android.support.design.widget.CoordinatorLayout> 

Я отображая максимум 5 элементов в RecyclerView и Я бы хотел, чтобы он установил его высоту в зависимости от количества элементов, которые были добавлены внутри него. Например, когда я вставил 5 элементов, я бы хотел, чтобы он отображал 3 элемента - пропустить прокрутку coudl. Если я загружу только 1, мне бы хотелось отобразить только 1 без прокрутки. Когда RecyclerView не получит каких-либо элементов, я бы хотел установить его «высота в 0», чтобы он не был видимым. Любые идеи, как достичь этого? Я должен установить высоту manulayy, чтобы показать что-нибудь. Когда элементы не вставлены, RecyclerView показывает пустое пространство, которое я хочу избежать. Есть идеи? Я также попытался удалить RecyclerView и попытаться вставить виды для отдельного LinearLayout, но это не сработало. Вот мой пост об этом, но никто не помог мне:/(Adding custom View to LinearLayout dynamicly does not work)

ответ

0

Я предлагаю вам создать собственный RecyclerView:

public class CustomRecyclerView extends RecyclerView { 

    final int ITEM_HEIGHT = 72; 

    public CustomRecyclerView(Context context) { 
     super(context); 
    } 

    public CustomRecyclerView(Context context, @Nullable AttributeSet attrs) { 
     super(context, attrs); 
    } 

    @Override 
    protected void onMeasure(int widthSpec, int heightSpec) { 
     int itemCount = getAdapter().getItemCount(); 
     if (itemCount <= 3) { 
      super.onMeasure(widthSpec, ITEM_HEIGHT * itemCount); 
     } else if (itemCount < 5) { 
      super.onMeasure(widthSpec, ITEM_HEIGHT * 3); 
     } else { 
      super.onMeasure(widthSpec, ITEM_HEIGHT * 5); 
     } 
    } 
} 
+0

я получаю и ошибки: 'android.view.InflateException: Binary строки XML файл # 505: Ошибка раздувания класса. Создал пользовательский RecyclerView в пакете и использовал его так: ' 'Я изменил его в своей работе. – anton86993

+0

Извините, похоже, должен быть еще один конструктор: http://stackoverflow.com/a/9661176/2121682. Я исправил код, теперь он должен работать. – iamkaan