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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章