android - Recyclerview 布局未在 fragment 内正确显示

标签 android xml android-fragments android-recyclerview

我在 Fragment 中使用 RecyclerView 填充项目列表。我能够获取列表,但是显示的布局未正确显示。该列表仅包含 5 个项目,并且仅正确显示 4 个项目。

public class Old extends Fragment {

    public Old() {}

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);

        View rootView = inflater.inflate(old_layout, container, false);

        RecyclerView mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerView.setHasFixedSize(true);

        DatabaseData data = new DatabaseData(getActivity());
        List<OldModel> oldModelList = data.getOldData();

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this.getActivity());
        mRecyclerView.setLayoutManager(linearLayoutManager);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        OldAdapter oldAdapter = new OldAdapter(getActivity(), oldModelList);
        mRecyclerView.setAdapter(oldAdapter );

        return rootView;
    }
}

布局xml的old_layout.xml

<android.support.design.widget.CoordinatorLayout
    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:fitsSystemWindows="true"
    tools:context=".Old">

    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/recylerview"
     android:layout_height="wrap_content"
     android:layout_width="match_parent"
     android:layout_below="@+id/appBar"/>
     //also tried android:layout_below="@+id/toolbar"

</android.support.design.widget.CoordinatorLayout>

recyclerview.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/old_layout"
    tools:context=".Old">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical" />


</RelativeLayout>

这是我得到的输出。

SS of Output Error

最佳答案

只是移动

app:layout_behavior="@string/appbar_scrolling_view_behavior" 从 reyclerview.xml 到 include 标签内。

<include layout="@layout/recylerview"
     android:layout_height="wrap_content"
     android:layout_width="match_parent"
     android:layout_below="@+id/appBar"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

关于android - Recyclerview 布局未在 fragment 内正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41779852/

相关文章:

java - 空指针异常 : Attempt to read from field 'int android.app.Fragment.mContainerId' on a null object reference

android - 在 RecyclerView 中使用 ExoPlayer - 官方方式

android - 如何使用自定义主题更改默认文本颜色?

xml - '@comment()' 在 XPath 中有什么作用?

android - 通过 ButtonClick 设置 TextView 时出现 NullPointerException

java - 从 Activity 导航到 TabLayout/ViewPager fragment

java - 如何避免由于 Kotlin 构建错误 : Supertypes of the following classes cannot be resolved 而重新启动 Android Studio

android - Android 布局检查器中的灰色 View 是什么意思?

java - 如何使用 spring mvc 使用 Jaxb 从 Http Post Rest Web 服务中解码 xml?

java - 根据从 XML 文件中解析信息的 vector 绘制 ListField 并使用 Graphics 绘制屏幕