The childviews of my recyclerView should fill the complete space, when they are centered, but between the views should be a spcing/gap. All I found so far is this: How to add dividers and spaces between items in RecyclerView? But in this case, the size of the views is reduced, so that I see the view and the spacing when the view is centered.
One approach could be to make the recyclerview display two types of views, the normal and the divider, but that seems overkill for such a simples task.
Fragment:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/outerContainer">
<LinearLayout
android:id="@+id/searchBarContainer"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginLeft="16dp">
</LinearLayout>
<RelativeLayout
android:id="@+id/innerContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/searchBarContainer">
</RelativeLayout>
</RelativeLayout>
Layout for RecyclerView item
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.custom_view.news_feed.NewsFeedListItem
android:id="@+id/newsFeedListItem"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
Setting the adapter
recyclerView = (RecyclerView) LayoutInflater.from(getContext()).inflate(R.layout.recycler_view, null);
innerContainer.addView(recyclerView);
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, true);
layoutManager.setStackFromEnd(true);
recyclerView.addItemDecoration(new VerticalSpaceItemDecoration(VERTICAL_ITEM_SPACE));
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
VerticalSpaceItemDecorationClass
public class VerticalSpaceItemDecoration extends RecyclerView.ItemDecoration {
private final int mVerticalSpaceHeight;
public VerticalSpaceItemDecoration(int mVerticalSpaceHeight) {
this.mVerticalSpaceHeight = mVerticalSpaceHeight;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
RecyclerView.State state) {
outRect.bottom = mVerticalSpaceHeight;
}
}