无法修复Android上的RecyclerView上方的标题

时间:2017-05-11 06:30:23

标签: android android-layout android-recyclerview recyclerview-layout

我需要在RecyclerView上方显示一个标题,但是使用下面的代码,标题不是固定的,而是使用recycleler视图滚动。 有没有人能解决他的问题?

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 个答案:

答案 0 :(得分:0)

使用LinearLayout代替RelativeLayout。并Orientationvertical。这样布局从上到下排列。

所以,你的父布局应该链接这个 - &gt;

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

答案 1 :(得分: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>

答案 2 :(得分: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>

答案 3 :(得分:0)

尝试以下代码。它对我有用。

directory="$(pwd)/"
jar="BuildTools.jar"
path="${directory}${jar}"
output="/var/www/html/jars"
version=$1

if [[ ( -d "BuildData" ) || ( -d "Spigot" ) || ( -d "CraftBukkit" ) || ( -d "Bukkit" ) || ( -d "work" ) || ( -d "apache-maven-*" ) || ( -f "BuildTools.log.txt" ) ]]; then
    echo "Cleaning up..."
    rm -f -r BuildData/ Spigot/ CraftBukkit/ Bukkit/ work/ apache-maven-*/
    rm -f BuildTools.log.txt done.txt spigot-*.jar craftbukkit-*.jar vanilla-*.jar
fi

if [[ ! -f "${jar}" ]]; then
    echo "Downloading BuildTools..."
    wget https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/artifact/target/BuildTools.jar -O ${jar}
    chmod 775 ${jar}
fi

if [[ ! -z "${version}" ]]; then
    echo "Compiling jars for version ${version}..."
    java -jar ${jar} --rev ${version}
    echo "done" > done.txt
else
    echo "No version specified. Compiling jars for the latest version..."
    java -jar ${jar} --rev latest
    echo "done" > done.txt
fi

if [[ -f "done.txt" ]]; then
    echo "Compilation is done. Re-organizing..."

    for x in $(find ${directory} \( -name "*.jar" \)); do
        name=$(basename "${x}" ".jar")

        if [[ ${name} =~ ^(spigot|craftbukkit|minecraft_server)(-)?(.*)? ]]; then
            fullname="${name}.jar"

            echo "Jar found: ${fullname}, Moving to webserver..."

            if [[ ! -z "${version}" ]]; then
                outpath=${output}/${version}

                if [[ ! -d ${outpath} ]]; then
                    mkdir ${outpath}
                fi

                mv -f ${x} ${outpath}/${fullname}
            else
                outpath=${output}/latest

                if [[ ! -d ${outpath} ]]; then
                    mkdir ${outpath}
                fi

                rm -f -r ${outpath}
                mv -f ${x} ${outpath}/${fullname}
            fi
        fi
    done

    echo "Jars uploaded."
else
    echo "Could not re-organize, the jars hasn't been moved."
fi