Recyclerview布局未在片段内正确显示

时间:2017-01-21 13:17:51

标签: android xml android-fragments android-recyclerview

我正在RecyclerView内使用Fragment填充项目列表。我能够获取列表但是显示的布局未正确显示。该列表仅包含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

1 个答案:

答案 0 :(得分:2)

移动

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