Recyclerview下面跟随着控件

Recyclerview下面跟随着控件

效果

最后是实现了RecyclerView下面的控件会跟随着内容增加而下滑,并且最后超过屏幕时,下面的控件会粘在屏幕底部

代码

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <android.support.v7.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
        ></android.support.v7.widget.RecyclerView>
        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <EditText
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:hint="Type something here"
                        android:id="@+id/input_text"
                        android:maxLines="2"
                />
                <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
                        android:id="@+id/send"
                        android:text="send"
                />

        </LinearLayout>
</LinearLayout>

注意点

其实为什么会是下面的组件跟随RecyclerView进行变动的呢?
因为我们可以看到最外层LinearLayout的高度android:layout_heightwrap_content,所以,它其实是在适应整个RecyclerView的高度进行变化。

推论

如果把最外层LinearLayout的高度android:layout_height改为match_parent,那么,RecyclerView下面的控件将会一直保持在最底层

代码

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <android.support.v7.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:id="@+id/msg_recycler_view"
        ></android.support.v7.widget.RecyclerView>
        <LinearLayout

                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <EditText
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:hint="Type something here"
                        android:id="@+id/input_text"
                        android:maxLines="2"
                />
                <Button android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/send"
                        android:text="send"
                />

        </LinearLayout>
</LinearLayout>
点赞