I am trying to use ItemDecorator to achieve spacing between CardViews inside a RecyclerView. For that I am using as reference this answer but the spacing isn't applied. I Also tried with this answer but no avail.
Here is the XML for the RecyclerView:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.company.name.MyFragment">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerviewId"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
/>
</FrameLayout>
Here is the XML for the CardView:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cardView"
card_view:cardElevation="2dp"
>
<android.support.constraint.ConstraintLayout
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="100dp"
android:layout_height="98dp"
card_view:srcCompat="@mipmap/ic_launcher"
card_view:layout_constraintTop_toTopOf="parent"
card_view:layout_constraintLeft_toLeftOf="parent"/>
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
The code for the item decorators are the ones presented in the consulted answers.
What could be the reason for the spacing not to be applied?
EDIT 1:
Here's the code to assign the item decorator:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
...
myRecyclerView.setLayoutManager(new GridLayoutManager(activity, 2));
myRecyclerView.addItemDecoration(new SpacingGridView(activity, R.dimen.my_spacing), 0);
...
}
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState)
MyAdapter adapter = MyAdapter(getData());
myRecyclerView.setAdapter(adapter);
}