Android 动态回收视图列表视图下方的线性布局
我有一个动态列表和静态线性布局下面的最后一项。当列表为空时,Android 动态回收视图列表视图下方的线性布局,android,android-layout,recyclerview-layout,Android,Android Layout,Recyclerview Layout,我有一个动态列表和静态线性布局下面的最后一项。当列表为空时,LinearLayout位于页面顶部 每当我将元素添加到列表中时,LinearLayout就会移动到列表最后一项的下方。当列表变长时,静态线性布局应保持在屏幕的末尾。为了更好地理解,我画了一些例子 但每当我添加7元素时,我的线性布局就隐藏在列表下面 <RelativeLayout android:layout_width="match_parent" android:layout_height="
LinearLayout
位于页面顶部
每当我将元素添加到列表中时,LinearLayout
就会移动到列表最后一项的下方。当列表变长时,静态线性布局应保持在屏幕的末尾。为了更好地理解,我画了一些例子
但每当我添加7元素时,我的线性布局就隐藏在列表下面
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/app_margin"
android:id="@+id/group_manage_list_view"
android:paddingBottom="40dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@+id/group_manage_list_view"
>
<Button
android:layout_width="34dp"
android:layout_height="34dp"
android:background="@drawable/ic_add_circle_outline_white_24dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="@dimen/app_margin"
android:backgroundTint="@color/headline_grey"
android:id="@+id/create_group_button_click"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/add_group"
android:layout_marginLeft="@dimen/app_margin"
android:layout_gravity="center_vertical"
/>
</LinearLayout>
</RelativeLayout>
您可以使用android:layout\u weight
来实现这一点,但您必须保持回收视图和线性布局的可见性,如果数组大小大于零,则使其可见,否则不可见,反之亦然
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/group_manage_list_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.8"
android:padding="10dp"
android:paddingBottom="40dp"
android:visibility="gone" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:minHeight="50dp"
android:orientation="horizontal">
<Button
android:id="@+id/create_group_button_click"
android:layout_width="34dp"
android:layout_height="34dp"
android:layout_gravity="top"
android:layout_marginLeft="10dp"
android:background="@drawable/ic_launcher_background"
android:backgroundTint="@color/colorAccent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_marginLeft="10dp"
android:text="add_group" />
</LinearLayout>
</LinearLayout>
我认为您需要使用嵌套滚动视图,下面是示例代码。希望这能对你有所帮助
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Seomthing" />
</LinearLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
如果这有帮助,请投票表决我的答案。
快乐编码..hmm当列表中有例如2项时,这种方法是否有效?当我检查列表大小并使listView可见时,它在屏幕上占用了1.8个空间,线性布局在屏幕底部?是的,expiredmind。当列表有数据时,它将显示在屏幕上,线性布局将显示在底部,如果没有,则仅显示线性布局,您的问题得到解决。这不是问题,此布局仅处理两种情况,当列表为空或列表超过例如6个元素(根据我的相关图纸)时。当列表有3个元素时,线性布局应直接位于最后一项的下方,而不是屏幕的底部。对不起,我的解释很混乱,但我希望你能理解我的意思issue@Expiredmind我将再次检查这个问题,并更新我的答案。我使用了阿姆里什·卡卡迪亚方法,因为他实现了我问题的最接近的解决方案。这种方法几乎奏效。当列表较大时,线性布局不会隐藏,但不幸的是,在屏幕上并不总是可见(例如,如果我有7个项目,我必须向下滚动才能看到线性布局)移除nestedScrollView
然后使RecyclerView
高度匹配父视图
并在RecyclerView
中始终将线性布局
置于屏幕底部