简体   繁体   中英

RecyclerView is taking full screen to show one message

i am working on a layout for chat screen in which i am using RecyclerView to show messages sent and receive by user but the problem which i am facing is RecyclerView is showing only one message in whole screen and to see other message i have to scroll down or scroll up. i want to show messages next to eachother vertically. Here is my xml code:-

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/chatparent"
    android:background="@color/background">
    <include layout="@layout/chattoolbar"/>





    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_messages"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:scrollbars="vertical"
        android:scrollbarStyle="outsideOverlay"/>


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="bottom">




   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content">




            <FrameLayout
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:layout_weight="0.80"
                android:background="@color/colorPrimary" />

            <FrameLayout
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:layout_weight="0.20"
                android:background="@color/black"/>

   </LinearLayout>


        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/rl_typing"
            android:background="@drawable/chatbox"
            android:orientation="horizontal"
            android:paddingLeft="5dp"
            android:paddingRight="10dp"
            android:layout_marginStart="15dp"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="2dp"
            >


            <Button
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:background="@drawable/ic_emoticons"
                android:layout_marginTop="8dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:gravity="bottom"
                android:id="@+id/btn_emoji"
                />



            <EditText
                android:layout_width="190dp"
                android:layout_height="wrap_content"
                android:inputType="textMultiLine"
                android:minLines="1"
                android:maxLines="20"
                android:lines="8"
                android:imeActionId="@+id/send"
                android:imeActionLabel="actionSend"
                android:imeOptions="actionSend"
                android:scrollbars="vertical"
                android:textColor="@color/monsoon"
                android:textColorHint="@color/dark_gray"
                android:id="@+id/et_message"
                android:textSize="13sp"
                android:hint=" Type Your Message Here..."
                android:layout_toEndOf="@id/btn_emoji"
                tools:ignore="HardcodedText" />



            <Button
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:background="@drawable/ic_attachment"
                android:layout_marginTop="8dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:gravity="bottom"
                android:id="@+id/btn_attach"
                android:layout_toEndOf="@id/et_message"/>


            <Button
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:background="@drawable/ic_camera"
                android:layout_marginTop="8dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:id="@+id/btn_camera"
                android:layout_toEndOf="@id/btn_attach"/>


        </RelativeLayout>

        <Button
            android:layout_width="30dp"
            android:layout_height="35dp"
            android:id="@+id/btn_send"
            android:layout_alignParentTop="true"
            android:layout_marginTop="7.5dp"
            android:layout_centerHorizontal="true"
            android:layout_marginStart="3dp"
            android:layout_toEndOf="@id/rl_typing"
            android:background="@drawable/ic_send" />




    </RelativeLayout>
</LinearLayout>

Here is the recyclerView_item.xml :-

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="5dp"
    android:paddingTop="5dp"
    android:paddingLeft="10dp">


    <TextView
        android:id="@+id/username"
        style="?android:textAppearanceMedium"
        android:textColor="?android:textColorPrimary"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:textStyle="bold"/>

   <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
       android:orientation="vertical">


    <TextView
        android:id="@+id/txtOther"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16dp"
        android:textColor="@color/black"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:background="@drawable/chat_in"/>

    <TextView
        android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/chat_out"
        android:paddingBottom="5dp"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:paddingTop="5dp"
        android:textColor="@color/white"
        android:textSize="16dp" />

       <TextView
           android:id="@+id/lblDate"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:textSize="12dp"
           android:textColor="@color/colorprimarylight"
           android:textStyle="italic"
           android:padding="5dp"
           />

</LinearLayout>



</LinearLayout>

Here is the screenshot in which you can see how much space it is producing in between two messages. 屏幕截图

设置recyclerview项目的主布局高度“ wrap_content”

In child view of recyclerView set xml root to "wrap_content". If you set a flag match_parent it will take all space and you need to scroll your parent view.

在recyclerView_item.xml中,必须将“主要” LinearLayout的高度和宽度match_parent设置为wrap_content

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM