0

我需要在 RecyclerView 上方显示一个标题,但使用下面的代码,标题不是固定的,并且会随着回收器视图滚动。有没有人可以解决他的问题?


fragment_history_detail.xml

[[第一次尝试]]

<RelativeLayout 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:background="?android:attr/colorBackground">

    <RelativeLayout
        android:id="@+id/header_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true">

        <include
            layout="@layout/view_header_history_detail"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/header_container">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView_history_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:layoutManager="LinearLayoutManager"
            tools:listitem="@layout/fragment_history_detail_item_self" />
    </RelativeLayout>
</RelativeLayout>

[[第二次尝试]]

我也尝试使用如下所示的 LinearLayout。但这无济于事。

<LinearLayout 
    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:background="?android:attr/colorBackground"
    android:orientation="vertical">

    <include
        layout="@layout/view_header_history_detail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView_history_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        app:layoutManager="LinearLayoutManager"
        tools:listitem="@layout/fragment_history_detail_item_self" />

</LinearLayout>

[[第三次尝试]]

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 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:background="?android:attr/colorBackground"
    android:orientation="vertical">

    <include
        layout="@layout/view_header_history_detail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <RelativeLayout 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:background="?android:attr/colorBackground">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView_history_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:layoutManager="LinearLayoutManager"
            tools:listitem="@layout/fragment_history_detail_item_self" />
    </RelativeLayout>
</LinearLayout>

view_header_history_detail.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="horizontal">

    <ImageView
        android:id="@+id/imageView_back"
        android:layout_width="60dp"
        android:layout_height="@dimen/title_height"
        android:background="?attr/colorPrimaryDark"
        app:srcCompat="@drawable/vector_expand_left" />

    <TextView
        android:id="@+id/textview_title"
        android:layout_width="match_parent"
        android:layout_height="@dimen/title_height"
        android:background="?attr/colorPrimaryDark"
        android:gravity="center_vertical"
        android:text="History Detail"
        android:textAllCaps="true"
        android:textColor="?attr/colorHeaderText"
        android:textSize="16sp" />

</LinearLayout>
4

4 回答 4

0

如果你想修复布局,那么你可以按照我的想法做一件事......看看下面的代码......

<LinearLayout 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:background="?android:attr/colorBackground"
android:orientation="vertical">

<include
    layout="@layout/view_header_history_detail"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<RelativeLayout 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:background="?android:attr/colorBackground"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView_history_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        app:layoutManager="LinearLayoutManager"
        tools:listitem="@layout/fragment_history_detail_item_self" /></LinearLayout>
于 2017-05-11T07:49:43.210 回答
0

复制以下代码并放入您的xml文件

<RelativeLayout
    android:id="@+id/header_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true">

    <include
        layout="@layout/view_header_history_detail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</RelativeLayout>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/header_container">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView_history_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        app:layoutManager="LinearLayoutManager"
        tools:listitem="@layout/fragment_history_detail_item_self" />
</RelativeLayout>

删除上面的相对布局,或者您可以粘贴相对布局内容

<RelativeLayout
   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:background="?android:attr/colorBackground">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView_history_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        app:layoutManager="LinearLayoutManager"
        tools:listitem="@layout/fragment_history_detail_item_self" />
</RelativeLayout>
于 2017-05-11T06:43:41.683 回答
0

Use LinearLayout instead of RelativeLayout. And ser Orientation to vertical. So that the layout place top to bottom.

So, Your parent layout should be link this ->

<LinearLayout 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:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="?android:attr/colorBackground">

        <!--Your child layouts goes here-->


</LinearLayout>
于 2017-05-11T06:36:43.990 回答
0

试试下面的代码。这个对我有用。

<?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">

<include layout="@layout/view_header_history_detail" />

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerChat"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" />
</LinearLayout>
于 2017-05-11T09:37:47.123 回答