我有和这样的XML文件(问题在于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>
我在RecyclerView中显示最多5个元素,我希望根据其中添加的元素数量来设置它的高度。例如,当我插入5个元素时,我希望它显示3个元素 - 休息coudl向下滚动。如果我只下载1,我只想显示1而不滚动。当RecyclerView无法获得任何elemet时,我想将它的高度设置为0,因此它不会是可见的。任何想法如何实现这一目标?我必须设置高度manulayy来显示任何东西。当没有插入任何项目时,RecyclerView会显示我想要避免的空白空白区域。有任何想法吗?我还尝试删除RecyclerView并尝试插入视图以分离LinearLayout,但这不起作用。这是关于它的帖子,但没有人帮助过我:/(Adding custom View to LinearLayout dynamicly does not work)
答案 0 :(得分: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);
}
}
}