如何使水平回收视图出现在屏幕的底部?

时间:2017-09-27 03:25:13

标签: android android-layout android-recyclerview

我想建立如下图所示的布局。当我点击按钮时,水平recyclerView向上滑动,并显示在包含按钮的LinearLayout下的屏幕底部。

enter image description here

我希望使用底部工作表。当显示带有recyclerView的底部工作表时,它会覆盖包含LinearLayout的整个button。这是我的布局xml:

<android.support.design.widget.CoordinatorLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:fitsSystemWindows="true">

<LinearLayout
     android:id="@+id/bottomBar"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_alignParentBottom="true"
     android:background="@color/white"
     android:orientation="horizontal"
     android:paddingBottom="5dp"
     android:paddingTop="5dp">       

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@null"
            android:text:"Button"/>     

</LinearLayout>

<android.support.v7.widget.RecyclerView
        android:id="@+id/horizontalRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_anchor="@id/bottomBar"
        android:animateLayoutChanges="false"
        android:scrollbars="horizontal"
   app:layout_behavior="android.support.design.widget.BottomSheetBehavior"/>

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

所以我的问题是,如何让水平recyclerView向上滑动并从屏幕底部显示?我正朝着正确的方向前进吗?或者有更好的方法来实现这个目标?

提前致谢。

3 个答案:

答案 0 :(得分:1)

您可以通过简单的步骤实现这一目标:

首先通过添加此线性布局方向保持垂直。

android:orientation="vertical"

然后在linearLayout内添加按钮下面的循环播放器视图。无论何时你必须隐藏recyclerview,将recyclerview的可见性保持为“消失”,并将可见性保持为“可见”,如下所示。

最初将您的回收商隐藏在xml中,并在xml中添加此行

android:visibility="gone"

当按钮被clciked时,让你的回收者出现在这样的

yourRecycler.setVisibility(View.VISIBLE);

最后你的xml应该是这样的

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<LinearLayout
    android:id="@+id/bottomBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:orientation="vertical"
    android:paddingBottom="5dp"
    android:layout_alignParentBottom="true"
    android:paddingTop="5dp">

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@null"
        android:text="button"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/horizontalRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:animateLayoutChanges="false"
        android:scrollbars="horizontal"
        />

答案 1 :(得分:1)

尝试以下步骤,

首先,像这样定义XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_root"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="3"
    >

    <Button
        android:id="@+id/tv1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="#FFF"
        android:text="hello"
        android:textStyle="bold"
        android:layout_weight="1"
        />

    <RecycleView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:visibility="gone"
        >

    </RecycleView>

</LinearLayout>

其次,最初设置RecycleView可见性gone

第三,在运行时点击Button设置RecycleView可见性visible

答案 2 :(得分:0)

您可以使用此库。

https://github.com/umano/AndroidSlidingUpPanel

在XML中将RecyclerView放入其布局

<com.sothree.slidinguppanel.SlidingUpPanelLayout