Jah
Jah

Reputation: 223

Recyclerview layout not showing properly inside a fragment

I'm populating a list of items using RecyclerView inside a Fragment. I'm able to get the list however the layout being displayed is not correctly being showned. The list contains only 5 items and it's only displaying 4 items correctly.

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

Layout xml's 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>

This is the output that I'm getting.

SS of Output Error

Upvotes: 2

Views: 462

Answers (1)

Kartik Sharma
Kartik Sharma

Reputation: 723

Just move

app:layout_behavior="@string/appbar_scrolling_view_behavior" from reyclerview.xml to inside the include tag.

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

Upvotes: 2

Related Questions